Difference between revisions of "CSC231 Bash Tutorial 7"
(→Testing Numerical Values) |
|||
Line 54: | Line 54: | ||
read guess | read guess | ||
− | if [ $guess -le 5 ] ; then | + | if [ "$guess" -le "5" ] ; then |
echo "You aim low!" | echo "You aim low!" | ||
else | else | ||
Line 69: | Line 69: | ||
::* -eq (equal to) | ::* -eq (equal to) | ||
::* -ne (not equal to) | ::* -ne (not equal to) | ||
+ | <br /> | ||
+ | ==Nested If-Statements== | ||
+ | <br /> | ||
+ | * Of course, if Bash supports ''if statements'', it will support '''nested''' ''if statements''. | ||
+ | * Try this new version of the script: | ||
+ | <br /> | ||
+ | ::<source lang="bash"> | ||
+ | #! /bin/bash | ||
+ | # if2.sh | ||
+ | |||
+ | #--- get user input --- | ||
+ | echo -n "Please enter an integer between 1 and 10 (included): " | ||
+ | read guess | ||
+ | |||
+ | #--- test if below or above 5 --- | ||
+ | if [ "$guess" -le "5" ] ; then | ||
+ | |||
+ | #--- compute even/odd property of guess | ||
+ | x=$( expr $guess % 2 ) | ||
+ | |||
+ | #--- test if guess is odd or even --- | ||
+ | if [ $x -eq 0 ] ; then | ||
+ | echo "low even number" | ||
+ | else | ||
+ | echo "low odd number" | ||
+ | fi | ||
+ | |||
+ | else | ||
+ | echo "Above average!" | ||
+ | fi | ||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> |
Revision as of 05:45, 31 March 2017
--D. Thiebaut (talk) 06:15, 31 March 2017 (EDT)
This lab deals with if-statements in bash scripts. If statements in Bash work the same way they work in Python and Java: if somebooleanexpression then dothis else dothat.
|
Reference
- This Bash tutorial heavily uses material presented in the excellent set of pages "Ryan's Tutorials" at http://ryanstutorials.net/bash-scripting-tutorial/. Do not hesitate to study these tutorials if you need additional information.
Backing up your files
Just in case you mess up and erase files in your account by mistake, you will make an archive of all your files and save it in your instructor's account:
cd tar -czvf backup2.tgz * rsubmit backup backup2.tgz
That's it! An archive of all your file should now be saved and available in case of accident!
Testing Numerical Values
- Create this bash script, make it executable, and run it a couple times:
#! /bin/bash # if0.sh echo -n "Please enter an integer between 1 and 10 (included): " read guess echo "You have entered $guess"
- Let's modify it and test the value the user enters:
#! /bin/bash # if0.sh echo -n "Please enter an integer between 1 and 10 (included): " read guess if [ "$guess" -le "5" ] ; then echo "You aim low!" else echo "Above average!" fi
- Make sure you have spaces around the brackets and around the operator inside the brackets!
- The "-le" operator stands for "less than or equal to." Bash supports the following operators for comparing numbers:
- -le (less than or equal to)
- -lt (less than )
- -ge (greater than or equal to)
- -gt (greater than)
- -eq (equal to)
- -ne (not equal to)
Nested If-Statements
- Of course, if Bash supports if statements, it will support nested if statements.
- Try this new version of the script:
#! /bin/bash # if2.sh #--- get user input --- echo -n "Please enter an integer between 1 and 10 (included): " read guess #--- test if below or above 5 --- if [ "$guess" -le "5" ] ; then #--- compute even/odd property of guess x=$( expr $guess % 2 ) #--- test if guess is odd or even --- if [ $x -eq 0 ] ; then echo "low even number" else echo "low odd number" fi else echo "Above average!" fi