简体   繁体   中英

How to handle errors in shell script

I am writing shell script to install my application. I have more number of commands in my script such as copy, unzip, move, if and so on. I want to know the error if any of the commands fails. Also I don't want to send exit codes other than zero.

Order of script installation(root-file.sh):-

./script-to-install-mongodb
./script-to-install-jdk8
./script-to-install-myapplicaiton

Sample script file:-

cp sourceDir destinationDir

unzip filename

if [ true] 
// success code
if

I want to know by using variable or any message if any of my scripts command failed in root-file.sh.

I don't want to write code to check every command status. Sometimes cp or mv command may fail due to invalid directory. At the end of script execution, I want to know all commands were executed successfully or error in it?

Is there a way to do it?

Note: I am using shell script not bash

/* the status of your last command stores in special variable $?, you can define variable for $? doing export var=$? */

unzip filename
export unzipStatus=$?
./script1.sh
export script1Status=$?
if [ !["$unzipStatus" || "$script1Status"]]
     then                  
         echo "Everything successful!"       
     else
         echo "unsuccessful"
fi

Exception handling in linux shell scripting can be done as follows

command || fallback_command

If you have multiple commands then you can do

(command_one && command_two) || fallback_command

Here fallback_command can be an echo or log details in a file etc.

I don't know if you have tried putting set -x on top of your script to see detailed execution.

Well as you are using shell script to achieve this there's not much external tooling. So the default $? should be of help. You may want to check for retrieval value in between the script. The code will look like this:

./script_1
retval=$?
if $retval==0; then
  echo "script_1 successfully executed ..."
  continue
else; 
  echo "script_1 failed with error exit code !"
  break
fi
./script_2

Lemme know if this added any value to your scenario.

Want to give my 2 cents here. Run your shell like this

sh root-file.sh 2> errors.txt

grep patterns from errors.txt

grep -e "root-file.sh: line" -e "script-to-install-mongodb.sh: line" -e "script-to-install-jdk8.sh: line" -e "script-to-install-myapplicaiton.sh: line" errors.txt

Output of above grep command will display commands which had errors in it along with line no. Let say output is:-

test.sh: line 8 : file3: Permission denied

You can just go and check line no.(here it is 8) which had issue. refer this go to line no. in vi .

or this can also be automated: grep specific line from your shell script. grep line with had issue here it is 8 .

head -8 test1.sh |tail -1

hope it helps.

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