Log in to users
- Log in to a Windows machine or your own laptop
Enter your Unix username as the account name. Then enter your password.
- Run the ssh client
Use the values in the table below to connect to users.
ssh configuration parameter value protocol ssh ssh version 2 ssh port 22 host users.cs.umb.edu user name your Unix username Authentication method password
- Enter your password
A dialog box will appear on the screen into which you must type the password for your Unix account.
Directory for this Exercise
- Go to your ex directory
cd ~/it244/ex- Create an ex25 directory
mkdir ex25- Go to your ex25 directory
cd ex25while Loops
- Using your favorite editor, create the script count_to_five.sh
#! /bin/bash # # prints the numbers from 1 to 5 number=1 while [ $number -lt 6 ] do echo $number (( number += 1 )) done- Make this script executable
chmod 755 count_to_five.sh- Run this script
./count_to_five.shPractice using the debugging feature
- Run count_to_five.sh using the debugging feature
bash -x count_to_five.shNotice that each line of code is printed as it is executed, with the current value of the variable substituted for the variable name.until Loop
- Using your favorite editor, create the script count_until.sh
#! /bin/bash # # counts from 1 to its argument, then stops number=1 until [ $number -gt $1 ] do echo "$number" (( number += 1)) doneNotice that this script uses the -gt, greater than, option, as opposed to the -lt, less than, option employed in the while loop script above.
- Make this script executable
chmod 755 count_until.sh- Run this script
./count_until.sh 10continue
- Using nano, or your favorite editor, create the script continue.sh
#! /bin/bash # # demonstrates how continue works total=0 for number in 1 2 3 4 5 6 7 8 9 10 do if [ $number -eq 2 -o $number -eq 4 -o $number -eq 6 -o $number -eq 8 ] then continue fi echo Adding $number to $total (( total += $number )) done echo echo total: $total- Make this script executable
chmod 755 continue.sh- Run the script continue.sh
./continue.shNotice that the echo command is not run for the values of 2, 4, 6 and 8.
Instead, the loop progresses to the next value of the loop variable.break
- Using nano, or your favorite editor, create the script break.sh
#! /bin/bash # # demonstrates how break works for f in * do if [ -x $filename ] then echo First executable file: $f break fi done- Make this script executable
chmod 755 break.sh- Run the script break.sh
./break.shThe program examines all the files in your current directory.
If it finds an executable file, it prints the name of that file and quits.case
- Using nano, or your favorite editor, create the script case.sh
#! /bin/bash # # demonstrates how the case structure works echo -n "Enter A, B, or C: " read letter case $letter in A|a) echo You entered A or a ;; B|b) echo You entered B or b ;; C|c) echo You entered C or c ;; *) echo You did not enter A, B, or C ;; esac echo Exiting program- Make this script executable
chmod 755 case.sh- Run the program several times
./case.shselect
- Using nano, or an editor of your choice, create select.sh
#! /bin/bash # # demonstrates how the select structure works PS3="Choose your fruit: " select fruit in apple banana blueberry orange STOP do if [ $fruit = "" ] then echo Invalid entry continue elif [ $fruit = STOP ] then echo About to leave break fi echo You chose $fruit echo That is choice number $REPLY done echo Exiting program- Make this script executable
chmod 755 select.sh- Run this program choosing several different fruits
./select.sh