bash: executing commands and reading exit vals

I have a function that returns a bunch of exit codes, say func1, and in my code I'm trying to execute that function in an if statement. This is the closest I could get.

f1call=`func1 $arg1 $arg2`
if [[ "$f1call" = "0" ]]; then
...
fi

When I run the script the function never gets called. What's the right way to do this?

Here are some other basic questions that I need answered too:

-When do we use double-brackets vs. single-brackets for the if-statement expression?

-Is the exit code that's being returned an integer or string? I ask this because I'm wondering if it should be "[[ $f1call = 0 ]]" as opposed to what I have up there.

Your usage would indicate that

func1()
{
    .....do stuff
    echo "$EXIT_CODE"
}

Anyway that's what func1 has to do for

 f1call=`func1 parm1 parm2 parm3`
 echo "$f1call"

the above code to work.

So does that mean I don't need to put "exit $exit_val", but instead just "echo $exit_val"? Or do I still need that exit statement in my function?

having an exit $exit_val in the function will not have any effect with respect to the function

it is the value that is echoed is caught in the variable.

just the echo statement would do

func1()
{
    .....do stuff
    #echo "$EXIT_CODE"
    return $EXIT_CODE
}

if func1 $arg1 $arg2
then
   # func1 : succes (exit status 0)
else
   # func1 : error
fi

func1 $arg1 $arg2
if [ $? -eq 0 ]
then
   # func1 : succes (exit status 0)
else
   # func1 : error
fi

Jean-Pierre.

An exit inside a function will make the script to exit.

See this.

[/tmp]$ cat try.sh
#! /bin/sh
func1 ()
{
        return 1
}
func2 ()
{
        return 0
}
func3 ()
{
        exit 1
}
func1
status=$?
echo "Expecting 1 -> Got $status"
func2
status=$?
echo "Expecting 0 -> Got $status"
func3
status=$?
echo "Unreachable echo" 
[/tmp]$ ./try.sh
Expecting 1 -> Got 1
Expecting 0 -> Got 0
[/tmp]$ 

Also instead of echo'ing the exit status, the OP can use return. Unless ofcourse he runs the script as

. script

vinog,

then what abt this...

i think i am messing up things really today..

>cat fun.ksh
#! /usr/bin/ksh

function abc {
echo "inside the function "
ty=1
exit $ty
}

echo "before the function abc"
val=`abc`
echo "val is $val"
echo "after the function abc"

exit 0
>output
before the function abc
val is inside the function
after the function abc

It has to do with the Command Substitution i.e. backticks/$(command).

Command substitution invokes a subshell. So it is the subshell that gets affected.

Try invoking the function as is. And not as a command substitution. Then the exit will affect the script as a whole.