Difference between revisions of "CSC231 Bash Tutorial 7"

From dftwiki3
Jump to: navigation, search
Line 257: Line 257:
 
<br />
 
<br />
 
* The '''OR''' operator is '''||''' and the '''AND''' operator is '''&&'''.
 
* The '''OR''' operator is '''||''' and the '''AND''' operator is '''&&'''.
 +
<br />
 +
<br />
 +
{| style="width:100%; background:limegreen"
 +
|-
 +
|
 +
 +
==Challenge #2:==
 +
|}
 +
[[Image:QuestionMark2.jpg|right|120px]]
 +
<br />
 +
Modify the previous script, noticing that "Smith", "Amherst", "Hampshire", and "MtHolyoke" all have suffix '''College''', and therefore one if statement with a then and else block can be used to set the suffix.  You will need other if statements for the towns.
 +
<br />

Revision as of 06:21, 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



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!

The Bash If-Statement


The Bash if-statement looks like this:

 
if  [  someExpression ] ; then
   bash statement
   bash statement
else
   bash statement
   bash statement
fi


Of course, there can be as many bash statements inside the then or else block, including for loops, or other if statements.

  • Make sure to end the if statement with a fi keyword.
  • The else block is not necessary and can be omitted if the logic doesn't call for it.

Testing Numerical Values


  • Create this bash script, make it executable, and run it a couple times:


#! /bin/bash
# if0.sh

#--- get input from user ---
echo -n  "Please enter an integer between 1 and 10 (included): "
read guess

#--- display number back ---
echo "You have entered $guess"


  • Let's modify it and test the value the user enters:


#! /bin/bash
# if0.sh

#--- get input from user ---
echo -n  "Please enter an integer between 1 and 10 (included): "
read guess

#--- test if number entered is below or above 5 ---
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




Challenge #1:

QuestionMark1.jpg


Write a script that prompts the user for an integer number between 0 and 100 (inclusive) and outputs a letter grade, using Moodle's grade table:

Moodle Table
Range Letter Grade

100.00 -93.00

A

92.99 - 90.00

A-

89.99 - 87.00

B+

86.99 - 83.00

B

82.99 - 80.00

B-

79.99 - 77.00

C+

76.99 - 73.00

C

72.99 - 70.00

C-

69.99 - 67.00

D+

66.99 - 60.00

D

59.99 - 0.00

F


  • Note: to test if a quantity is less than or equal to 59.99, for example, you can use less than 60.
  • Note: Feel free to test just the top 3 ranges of numbers. If you can do it for 3, you can do it for the full list!


Testing and Comparing Strings


Bash uses different operators for strings: = for equality, and != for inequality. Here is an example:

#! /bin/bash
# if3.sh

for name in "Smith" "Hampshire" "Umass"  "Amherst" "MtHolyoke" ; do

   if [ "$name" == "Smith" ]; then
       town="Northampton"
       suffix="College"
   fi

   if [ "$name" == "Amherst" ]; then
       town="Amherst"
       suffix="College"
   fi
   if [ "$name" == "Umass" ]; then
       town="Amherst"
       suffix=""
       name="University of Mass."
   fi
   if [ "$name" == "MtHolyoke" ]; then
       town="South Hadley"
       suffix="College"
   fi
   if [ "$name" == "Hampshire" ]; then
       town="Amherst"
       suffix="College"
   fi

   echo "$name $suffix, $town, Massachusetts"

done


  • Try it!
  • You will have noticed that the then-blocks for "Amherst" and "Hampshire" are the same, so we could simplify our code by using a logical OR operator for "Amherst" and "Hampshire". The syntax for logical operators in Bash is a bit weird, though... logical:


if [ "$name" == "Amherst" ] || [ "$name" == "Hampshire" ]; then
       town="Amherst"
       suffix="College"
fi


  • The OR operator is || and the AND operator is &&.



Challenge #2:

QuestionMark2.jpg


Modify the previous script, noticing that "Smith", "Amherst", "Hampshire", and "MtHolyoke" all have suffix College, and therefore one if statement with a then and else block can be used to set the suffix. You will need other if statements for the towns.