Shell Programming Blog Shell Programming Blog

Saturday 22 September 2012

how to find out exit status of command or shell script?

Unknown | 11:16 |



By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.

(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.


This value is know as Exit Status.

But how to find out exit status of command or shell script?
Simple, to determine this exit Status you can use $? special variable of shell. For e.g. (This example assumes that unkfile doest not exist on your hard drive)


$ rm unkfile

It will show error as follows
rm: cannot remove `unkowm1file': No such file or directory and after that if you give command


$ echo $?

It will print nonzero value to indicate error. Now give command


$ ls
$ echo $?

It will print 0 to indicate command is successful.


Exercise
Try the following commands and not down the exit status:
$ expr 1 + 3
$ echo $?


$ echo Welcome
$ echo $?

$ wildwest canwork?
$ echo $?

$ date
$ echo $?

$ echon $?
$ echo $?

Shell Built in Variables

Shell Built in VariablesMeaning
$#Number of command line arguments. Useful to test no. of command line args in shell script.
$*All arguments to shell
$@Same as above
$-Option supplied to shell
$$PID of shell
$!PID of last started background process (started with &)

 

0 Comments:

   

Post a Comment

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming