简体   繁体   中英

retrieve error code from a command launched within a bash script

Ok I'm kind of new to bash scripting [the advanced stuff] and I need a little help. I don't even know exactly how to phrase this so I'll just explain what I am doing and what I need to know about it. in my script I run a ./configure and I need to be able to catch if there was an error in the configure and react accordingly within the bash script.

the code is:

function dobuild {
echo -e "\e[1;35;40mExecuting Bootstrap and Configure\e[0m"
cd /devel/xbmc
if [ $Debug = "1" ];
then
#either outputs to screen or nulls output
./bootstrap >/dev/null
/usr/bin/auto-apt run ./configure --prefix=/usr --enable-gl --enable-vdpau --enable-crystalhd --enable-rtmp --enable-libbluray  >/dev/null
else
./bootstrap
/usr/bin/auto-apt run ./configure --prefix=/usr --enable-gl --enable-vdpau --enable-crystalhd --enable-rtmp --enable-libbluray
fi
}

and say the configure returns an error 1 or 2 how do I trap that and act on it?

TIA

After the execution of every shell command it's return value, a number between 0 and 255, is available in the shell variable ? . You can get the value of this variable by prefixing it with the $ operator.

You have to be a little careful with ? , because it is reset by every command, even a test . For example:

some_command
if (( $? != 0 ))
then
   echo "Error detected! $?" >&2
fi

Gives: Error detected! 0 Error detected! 0 because ? was reset by the test condition. It is probably best to store ? in another variable if you are going to use it later, which includes doing more than one test on it .

To do a numeric test in bash use the (( ... )) numeric test construct:

some_command
result=$?
if (( $result == 0 ))
then
   echo "it worked!"
elif (( $result == 1 ))
then
    echo "Error 1 detected!" >&2
elif (( $result == 2 ))
then
    echo "Error 2 detected!" >&2
else
    echo "Some other error was detected: $result" >&2
fi

Alternatively use a case statement.

After the execution of a command, the returned value is stored in the shell variable $?. So you would have to match that with the return values of success and failure

if [ $? == 1 ]
then
    #do something
else
    #do something else
fi

The other answers about $? are great (though be careful about assuming values other than 0 and not-0 - different commands. or different versions of the same command may fail with different values), but if you just need to act on success or failure immediately, you can simplify things:

if command ; then
    # success code here
else
    # failure code here
fi

Or if you only want to act on failure, here's a hack for older shells (the colon is a null command but it satisfies the then clause):

if command ; then : 
else
    # failure code here
fi

But in modern shells like bash this is better:

if ! command ; then   # use the ! (not) operator 
    # failure code here
fi

And, if you only need to do simple things, you can use the "short circuit" operators:

   command1 && command2_if_command1_succeeds
   command1 || command2_if_command1_fails

Those only work for single commands, stringing more && and || on them doesn't do what you might think in most cases so most people avoid that. However, you can do multiple commands if you group them:

   command1 && { command2; command3; command4; }

That can get hard to read so it's best to keep it simple if you use it all:

   command1 || { echo "Error, command1 failed!" >&2; exit 1; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM