Hi,
I have a few old software setup scripts written as shell scripts which contains 100s of lines of shell commands for setup & configuration of our software (name of software not relevant for this topic). The script in general does lot of mkdir, chmod, chgrp, awk, cat kind of command executions.
These setup scripts lack one basic feature: if an error occurs, the scripts don't exit and keep continuing. I am now trying to introduce this feature in an existing script.
I wish to add at the end of (probably) each statement in the shell script a function like "stop_on_error", which basically checks $? and exits if non-zero,
but I also wish to capture what was the command that failed.
How can I do this ?
An example would be:
Code:
setup.sh
------------
#/bin/sh
echo "setup"
mkdir "sas"
chgrp -R sasgrp sas
..
..
userlist=`awk ..`
..
..
echo $userlist
I intend to add "; stop_on_error;" on each line. Here is the modified script:
Code:
setup.sh
-----------
#/bin/sh
stop_on_error () {
if [ "$?" = "0" ]; then exit 1
}
echo "setup" ; stop_on_error;
mkdir "sas" ; stop_on_error;
chgrp -R sasgrp sas ; stop_on_error;
..
..
userlist=`awk ..` ; stop_on_error;
..
..
echo $userlist ; stop_on_error;
When a command above actually fails (e.g say chgrp), then I want to print that command as well, e.g: Ideal output would be:
Code:
$ ./setup.sh
setup
<user message> command chgrp -R sasgrp sas failed
Any ideas ? Any ideas of how I can approach the problem itself in a better way ?
I am using AIX 5.3