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

[Programming] Shell Programming Series(XII)


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

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




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


Shell Programming Series(XII)

Category
Programming
Summary
Conditional statements execute if and only if a certain condition or conditions are true. They generally come in three forms: if statements, case statements, and logical AND/OR statements.
Body
if Statements

if statements test numerical expressions. If the condition is true the statements inside the if block are executed. If the statement is false, one of two things can happen:
? Nothing. The statements inside the if block are not executed, and the program continues as if they were not even there.
? If an else statement is included inside the if block, these statements will be executed if the condition is false. In other words, this can be used to write ?Do this if the condition is true, or do this if the condition is false, but do not do both? type of controls in programs.
For example, the following program uses an if statement to test the number of command-line arguments given to the program. If the command-line arguments are 1 or more, the program performs the operations inside the then block. If no command-line arguments were supplied, the program exits without doing anything.

1. #!/bin/sh
2. #ifprog: Demonstrate one way to use if statements
3. if [ $# -ge 1 ]
4. then
5. echo ?You supplied $# command-line arguments.?
6. fi
7. echo
8. echo ?Program exiting??
9. echo
10. exit 0


And here are two sample runs:

Run 1:

bash$ ./ifprog file1 file2 file3
You supplied 3 command-line arguments.

Program exiting?
bash$

Run 2:

bash$ ./ifprog

Program exiting?
bash$


The if statement in line 3 checks to see whether the number of command-line arguments supplied is one or greater. If it is, the statements between then and fi are executed. If it isn't, the statements inside the if block are skipped and the program jumps to the first statement past fi, which in this case simply informs the user that the program is exiting.
We could make this program more user-friendly by including an else statement that tells the user how to properly use the program rather than simply having it exit without doing anything as it currently does. Following shows the revised example:

1. #!/bin/sh
2. #ifprog: Demonstrate one way to use if statements
3. if [ $# -ge 1 ]
4. then
5. echo ?You supplied $# command-line arguments.?
6. else
7. echo ?Usage: $0 file1 file2??
8. echo
9. exit1
10. fi
11. echo
12. echo ?Program exiting??
13. echo
14. exit 0


Now, if the number of command-line arguments is less than 1, the program will perform the else statements and give the user a usage message. Notice line 9, which tells the program to exit immediately and sets the exit status to 1 (which indicates the program terminated with errors). Statements 10-14 will never get executed if the program runs the else statements.
The then part of the if statement is required. The else part is optional. As you have seen, the then part of the if statement performs an action only if the expression tested by if evaluates to true. Sometimes, however, you might want to perform an action only if the expression evaluates to false and to do nothing if the expression evaluates to true. You can do this by using a colon as a placeholder. For example:

if [ $myvar ?gt 5 ]
then
: # Do nothing and continue after the end of if block
else
# Statements to execute if condition is false go here.
fi



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.01815 seconds


About Us | Privacy Policy | Mission Statement | Help