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

[Programming] Shell Programming Series(XIII)


[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(XIII)


Shell Programming Series(XIII)

Category
Programming
Summary
Sometimes you might want to test for two or more different conditions and perform a different action, depending on the results. The elif statement accomplishes this.
Body
<TT>elif</TT>

elif is an abbreviation for ?else if?. When elif statements are used, the program will go through the if statement. If it evaluates to true, its actions will be performed and then program flow will jump to the first statement after the end of the if block. If it evaluates to false, the first elif is checked. If it is true, the statements inside it are executed and flow jumps to the end of the if block. If it is false, the second elif statement is evaluated, and so on. Basically, evaluations are done until the program reaches an expression that evaluates to true. If none of the conditions evaluate to true, either nothing happens, or the statement inside else, if present, are executed. Following code uses much of what you have learned up to this point, including if, elif, and else to play a simple number-guessing game.

1. #!/bin/sh
2. # Number guessing game
3. clear
4. guess_count=1 # Initialize the guess counter to 1
5. echo
6. echo ?Number guessing game written in bourne shell script.?
7. echo
8. echo ?n ?Enter upper limit for guess: ?
9. read up_limit
10. rnd_number=` jot ?r 1 1 $up_limit` # Get a random number
11. echo
12. echo ?I've thought of a number between 1 and $up_limit.?
13. echo ?n ?Please guess a number between 1 and $up_limit: ?
14. read guess
15. while true
16. do
17. if [ $guess ?gt $rnd_number ]
18. then
19. echo
20. echo ?Your guess was too high. Please try again.?
21. guess_count=`expr $guess_count + 1`
22. echo ?n ?Please guess a number between 1 and $up_limit: ?
23. read guess
24. elif [$guess ?lt $rnd_number ]
25. then
26. echo
27. echo ?Your guess was too low. Please try again.?
28. guess_count=`expr $guess_count + 1`
29. echo ?n ?Please guess a number between 1 and $up_limit: ?
30. read guess
31. else
32. break
33. fi
34. done
35. echo
36. echo ?Correct!?
37. echo
38. echo ?You guessed the number in $guess_count guesses.?
39. echo
40. exit 0


And a sample run:

Number guessing game written in bourne shell script.

Enter upper limit for guess: 10
I've thought of a number between 1 and 10.

Please guess a number between 1 and 10: 5

Your guess was too low. Please try again.
Please guess a number between 1 and 10: 8

Your guess was too high. Please try again.
Please guess a number between 1 and 10: 7

Correct!

You guessed the number in 3 guesses.


Most of the concepts in this program should be familiar to you by now. Line 3 simply clears the screen. Line 4 initializes a variable ?guess_count? to store the number guesses the player has made, and sets the initial value to 1. Lines 23 and 28 increment the value of the variable guess_count by 1 for each incorrect guess made. Line 10 uses command substitution with the jot command to assign a single random number between 1 and whatever the player entered as the upper limit for the guess to the variable rand_number. Line 15 starts an infinite loop. Line 17 checks to see if the number the player entered is greater than the random number that the program picked. If it is, the player is informed of that, the guess_count variable is incremented by 1, and the player is asked to pick another number. After the player has picked another number, it is stored in the variable ?guess? and the while loop starts over. If the number the player guessed is not greater than the random number the computer picked, the elif statement is evaluated to check if the number is lower. If it is, the player is informed of this, the guess_count variable is incremented by 1, and the player is asked to try again. After the player has entered a new number the while loop starts over. Finally, if neither condition is true, the else statement is executed. The else statement here simply breaks out of the infinite loop, and program flow passes to the first statement after the done statement. The statements at the end of the program simply tell the user they guessed the correct number and then inform the user how many guesses it took them to get correct answer by displaying the value of guess_count.
Like loop tests, if tests also support the logical AND (&amp;&amp;) and logical OR (| |) test to perform conditions if and only if both conditions are true, or if one of them are true.


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


About Us | Privacy Policy | Mission Statement | Help