3

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

2
  • '$?' gives you the status of last command executed, I think you can use that... example - test -x <some file> if [ $? -ne 0 ] then echo "file doesn't exits" exit 0 fi Commented Jul 31, 2017 at 7:51
  • @karan - please accept answer and vote up, if my solution has solved your problem! Commented Aug 1, 2017 at 4:00

4 Answers 4

3

/* 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
Sign up to request clarification or add additional context in comments.

2 Comments

@karan, I updated my answer. The logical is if any one status is not successful, the ultimate operation is not successful.
@karan, the logic is simple, you have to negate over all the logical OR.
2

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.

Comments

1

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.

1 Comment

my solution wouldn't need you to check status of each command but will facilitate you to have a fallback command so you can EITHER echo on screen about failure or success OR log details in a log file for later viewing.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.