Can $? return multiple values?

Hi,
I have a script which does something like the below:

execute_some_script.sh $arg1 $arg2 `exec-some-cmd`
if [ $? -eq 0 ] then;
 do something
else
 do something else
fi

However, during some cases, there is an error saying:

line xxx: [: too many arguments

at the line number which has the if statement. The script which is getting called is actually an expect script. Anyway, I am really confused about how its possible for $? to return multiple values. Can someone please help? :slight_smile:

A single program can't return multiple values, and the shells I'm aware of that support multiple exit status returns(for pipe chains, etc) put it in an array elsewhere. I'm not sure what's going on here. Is this code pasted letter for letter? What shell are you using?

You sure it's not in script.sh or in the `exec-some-cmd`code? Each of those may execute in a different shell process, so it's possible that they have a [ command at the same line number.

If you can, add "set -x" at the top of all shell scripts/commands that are executing; it'll allow you to see exactly what is causing the problem.

Welcome to the forum,
Alister

if [ $? -eq 0 ] then;
 do something
else
 do something else
fi

By the way, the semicolon is in a wrong position.

Can you post the relevant sections of your script?

Thanks for your comments. :slight_smile:

The script is about 1800 lines large. It does call another script to run in the background using:

nohup setup.sh & 2> /dev/null

and then proceeds to do some work. This setup script is much smaller,though. And the line number for which the error is shown is much greater than the number of lines in setup.sh.

Here are the relevant parts of the script which actually gets the error:

        get_info.sh $ip key1 `cat /var/t` >& /dev/null
        if [ $? -ne 0 ]; then
            echo "could not connect"
        else
            echo "done"
        fi

Return values (like $? in shell) from child processes are 8 bit numbers. This is because
the syscall, wait(), truncates a return value from a child process to the lowest 8 bits.

The range for 8 bit numbers is 0->255 (unsigned) or -128 -> 127 (signed).

That is all there is for $? in shell. Nothing else like multiple return values.