When I read this answer about $? another question comes to mind.
Is there any best practice for how to use $? in bash?
----
Let's have a example:
We have a linear script and I we would like to know that all the command was executed ok.
Do you think it is ok to call a small function (let's call it "did_it_work"),
to check the error code and break if it's not.
#!/bin/bash
function did_it_work {
code=$1
if [ "$code" -ne "0" ]
then
echo "Error failure: code $code "
exit 1
fi
}
dir=some/path
mkdir -p $dir
did_it_work $?
cd $dir
did_it_work $?
run_some_command
did_it_work $?
This approach of course means that I have to manually solve the problem if there is any and rerun the script.
Do you think this is a good idea or is there some other best practice to do this?
/Thanks
Asked by Johan
(4671 rep)
Mar 7, 2011, 04:35 PM
Last activity: May 12, 2025, 09:51 PM
Last activity: May 12, 2025, 09:51 PM