I have a bash script file to process something.
control.sh :
c_command="echo 'Hello'; echo ', Stack Overflow';exit 9";
$c_command;
stat=$?;
if [ $stat -eq 0 ];then
echo 'Do something....'
elif [ $stat -eq 9 ];then
echo 'The system will be rebooting...';
else
echo "..."
fi
I use the variable $c_command to execute the command, And I get the exit code is 0 not 9. If I paste it in command line is work perfect, But when I run in script is very bad.
Generally, when I execute "exit" command in script, then the script will be exit. And I just want the command done not the script and get the exit code from the command executed.
I solved this problem:
I put the code in $c_command to a script file and run it.
c_command="echo 'Hello'; echo ', Stack Overflow';exit 9";
echo "${c_command}" > tmp.sh;
/bin/bash tmp.sh;
stat=$?;
if [ $stat -eq 0 ];then
echo 'Do something....'
elif [ $stat -eq 9 ];then
echo 'The system will be rebooting...';
else
echo "..."
fi
$c_command
run fine and there is an exit status of 0. You find that the whole script terminates instead? Are you actually running the script that you pasted here? – Michael Hoffman Apr 12 at 3:29