CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Programming] Shell Programming Series(XVI)


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author System On 2007-04-29 10:01:48




View and vote on the article here: Shell Programming Series(XVI)


Shell Programming Series(XVI)

Category
Programming
Summary
Exit Status

Most programs in UNIX (as far as you know, we use FreeBSD) systems return an exit status when they terminate. The exit status is usually 0 if successful, and some number other than 0 if something went wrong. Some programs will return
Body
The exit status of the last program that ran is stored in the magic variable ?$??. Here are a couple of examples:

bash$ ls > /dev/null
bash$ echo $?
0
bash$

bash$ ls -2 /dev/null
ls: illegal option -- 2
usage: ls [-ABCFGHLPRTWabcdfgiklnoqrstu1] [file ?]
bash$ echo $?
1
bash$


The first example will set the magic variable $? to 0, as shown (output of the ls command was redirected to /dev/null for brevity in the example). The second example, however, supplies an illegal option to ls. The command fails with an error message, and $? is set to 1.
You can use this exit status to make automatic decisions in your shell programs. The previous logical AND/OR example already introduced this to an extent. It showed how the success or failure of one command can determine whether the next one is executed or not. The logical AND/OR statement used in the previous section could also have been written as an if statement like this:

if tar cvfz backup.tar.gz documents/2002/*
then
rm ?r documents/2002
else
echo ?Archive operation failed?
fi


This example combines the two examples from the previous section of series. The if statement reads the exit status of the tar command. If the exit status is 0, the then statements are performed, and the directory that was archived is removed. If the exit status is some number other than 0, the then statements are not performed, and the else statements are performed instead.
Another useful application of this property is with the test command. We've already seen how to use the test command with mathematical expressions. Another useful application of test is to check for the existence of AND/OR properties of a file. For example:

if [ -f program.conf ]
then
: # do nothing
else
touch program.conf
done


This example checks for the existence of the file ?program.conf?. If the file exists, a 0 is returned, and the then statement is executed. In this case, the then statement does nothing. If the file does not exist, a 1 is returned, and the else statement is performed, which uses the touch command to create the file.
The etst command can test for more than just the existence of a file. It can also test the attributes and type of file. Following is a list of all the options to the test command and what they mean:

-f The file exists, and is a normal file
-d The file exists, and is a directory
-s The file exists, and its size is greater than zero
-c The file exists, and is a character special file
-b The file exists, and is a block special file
-r The file exists, and is readable
-w The file exists, and is writable
-x The file exists, and is executable


This article was imported from zZine. (original author: ismail)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02014 seconds


About Us | Privacy Policy | Mission Statement | Help