Difference between revisions of "The exit status of a command"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
 
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Each Linux command returns a status when it terminates normally or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sys admin.
+
{{navigation
* Every Linux command executed by the shell script or user has an exit status.
+
|previous=Multilevel if-then-else
 +
|next=Conditional execution}}
 +
 
 +
Each Linux command returns a status when it terminates normally or abnormally. You can use value of exit status in the shell script to display an error message or take some sort of action. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sysadmin.
 +
==Exit Status==
 +
* Every Linux command executed by the shell script or user, has an exit status.
 
* The exit status is an integer number.
 
* The exit status is an integer number.
 
* The Linux man pages stats the exit statuses of each command.
 
* The Linux man pages stats the exit statuses of each command.
 
* 0 exit status means the command was successful without any errors.
 
* 0 exit status means the command was successful without any errors.
 
* A non-zero (1-255 values) exit status means command was failure.
 
* A non-zero (1-255 values) exit status means command was failure.
* You can use special shell variable called ? to get previously executed command. To print ? variable use the echo command (<code>echo $?</code>
+
* You can use special shell variable called <kbd>'''$?'''</kbd> to get the exit status of the previously executed command. To print <kbd>'''$?'''</kbd> variable use the [[echo command]]:
 +
<syntaxhighlight lang="bash" >echo $?
 +
date  # run date command
 +
echo $? # print exit status
 +
foobar123 # not a valid command
 +
echo $? # print exit status</syntaxhighlight>
 +
 
 +
==How Do I See Exit Status Of The Command?==
 +
Type the following command:
 +
<syntaxhighlight lang="bash" >date</syntaxhighlight>
 +
To view exist status of date command, enter:
 +
<syntaxhighlight lang="bash" >echo $?</syntaxhighlight>
 +
Sample Output:
 +
<pre>0</pre>
 +
Try non-existence command
 +
<syntaxhighlight lang="bash" >date1
 +
echo $?
 +
ls /eeteec
 +
echo $?</syntaxhighlight>
 +
Sample Output:
 +
<pre>2</pre>
 +
According to ls man page - ''exit status is 0 if OK, 1 if minor problems, 2 if serious trouble''.
 +
===How Do I Store Exit Status Of The Command In a Shell Variable?===
 +
Assign $? to a shell variable:
 +
<syntaxhighlight lang="bash" >ls -l /tmp
 +
status=$?
 +
echo "ls command exit stats - $status"</syntaxhighlight>
 +
====Exit Status Shell Script Example====
 +
A simple shell script to locate username (finduser.sh)
 +
<syntaxhighlight lang="bash" >#!/bin/bash
 +
# set var
 +
PASSWD_FILE=/etc/passwd
 +
 
 +
# get user name
 +
read -p "Enter a user name : " username
 +
 
 +
# try to locate username in in /etc/passwd
 +
grep "^$username" $PASSWD_FILE > /dev/null
  
 +
# store exit status of grep
 +
# if found grep will return 0 exit stauts
 +
# if not found, grep will return a nonzero exit stauts
 +
status=$?
  
 +
if test $status -eq 0
 +
then
 +
echo "User '$username' found in $PASSWD_FILE file."
 +
else
 +
echo "User '$username' not found in $PASSWD_FILE file."
 +
fi</syntaxhighlight>
 +
Save and close the file. Run it as follows:
 +
<syntaxhighlight lang="bash" >chmod +x finduser.sh
 +
./finduser.sh</syntaxhighlight>
 +
Sample Outputs:
 +
<pre>Enter a user name : vivek
 +
User 'vivek' found in /etc/passwd file.
 +
</pre>
 +
Run it again:
 +
<syntaxhighlight lang="bash" >chmod +x finduser.sh
 +
./finduser.sh</syntaxhighlight>
 +
Sample Outputs:
 +
<pre>Enter a user name : tommy
 +
User 'tommy' not found in /etc/passwd file.
 +
</pre>
 +
You can combine the grep and if command in a single statement as follows:
 +
<syntaxhighlight lang="bash" >if grep "^$username:" /etc/passwd >/dev/null
 +
then
 +
echo "User '$username' found in $PASSWD_FILE file."
 +
else
 +
echo "User '$username' not found in $PASSWD_FILE file."
 +
fi</syntaxhighlight>
 +
Notice that standard output from grep command is ignored by sending it to [[/dev/null]].
 +
== See also ==
 +
* [https://www.cyberciti.biz/faq/bash-get-exit-code-of-command/ Bash get exit code of command on a Linux / Unix]
 +
* [https://www.cyberciti.biz/faq/bourne-shell-exit-status-examples/ Bourne Shell Exit Status Examples]
 
[[Category:Conditional Execution]]
 
[[Category:Conditional Execution]]
 +
{{navigation
 +
|previous=Multilevel if-then-else
 +
|next=Conditional execution}}

Latest revision as of 20:19, 3 December 2018

← Multilevel if-then-elseHomeConditional execution →

Each Linux command returns a status when it terminates normally or abnormally. You can use value of exit status in the shell script to display an error message or take some sort of action. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sysadmin.

Exit Status

  • Every Linux command executed by the shell script or user, has an exit status.
  • The exit status is an integer number.
  • The Linux man pages stats the exit statuses of each command.
  • 0 exit status means the command was successful without any errors.
  • A non-zero (1-255 values) exit status means command was failure.
  • You can use special shell variable called $? to get the exit status of the previously executed command. To print $? variable use the echo command:
echo $?
date  # run date command
echo $? # print exit status
foobar123 # not a valid command
echo $? # print exit status

How Do I See Exit Status Of The Command?

Type the following command:

date

To view exist status of date command, enter:

echo $?

Sample Output:

0

Try non-existence command

date1
echo $?
ls /eeteec
echo $?

Sample Output:

2

According to ls man page - exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.

How Do I Store Exit Status Of The Command In a Shell Variable?

Assign $? to a shell variable:

ls -l /tmp
status=$?
echo "ls command exit stats - $status"

Exit Status Shell Script Example

A simple shell script to locate username (finduser.sh)

#!/bin/bash
# set var 
PASSWD_FILE=/etc/passwd

# get user name
read -p "Enter a user name : " username

# try to locate username in in /etc/passwd
grep "^$username" $PASSWD_FILE > /dev/null

# store exit status of grep
# if found grep will return 0 exit stauts
# if not found, grep will return a nonzero exit stauts
status=$?

if test $status -eq 0
then
	echo "User '$username' found in $PASSWD_FILE file."
else
	echo "User '$username' not found in $PASSWD_FILE file."
fi

Save and close the file. Run it as follows:

chmod +x finduser.sh
./finduser.sh

Sample Outputs:

Enter a user name : vivek
User 'vivek' found in /etc/passwd file.

Run it again:

chmod +x finduser.sh
./finduser.sh

Sample Outputs:

Enter a user name : tommy
User 'tommy' not found in /etc/passwd file.

You can combine the grep and if command in a single statement as follows:

if grep "^$username:" /etc/passwd >/dev/null
then
	echo "User '$username' found in $PASSWD_FILE file."
else
	echo "User '$username' not found in $PASSWD_FILE file."
fi

Notice that standard output from grep command is ignored by sending it to /dev/null.

See also

← Multilevel if-then-elseHomeConditional execution →