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 ex24 directory
mkdir ex24- Go to your ex24 directory
cd ex24Start Up a script Session
Execute the script command, using the --flush option
script --flushUsing test, as [ ] , with Numbers
- Use test with the -eq (equality for numbers) operator
[ 4 -eq 5 ] ; echo $?Since anything other than 0 as an exit status signals failure, this means that the equality test has failed.
Be sure to use a space before and after each [ and ] character.
- Use the -eq operator with the same number on both sides
[ 4 -eq 4 ] ; echo $?Since 0 means success, this means that the expression evaluated to true.
- Use test with the -ne (not equal for numbers) operator
[ 4 -ne 5 ] ; echo $?0 means true.
- Use -ne again
[ 4 -ne 4 ]; echo $?1 means false.
- Use test with the -gt (greater than) operator
[ 4 -gt 5 ] ; echo $? [ 5 -gt 4 ] ; echo $?- Use the -ge (greater than or equal to) operator
[ 5 -ge 4 ] ; echo $? [ 5 -ge 5 ] ; echo $? [ 4 -ge 5 ] ; echo $?- Use the -lt (less than operator)
[ 4 -lt 5 ] ; echo $? [ 5 -lt 4 ] ; echo $?- Use the -le (less than or equal to) operator
[ 4 -le 5 ] ; echo $? [ 4 -le 4 ] ; echo $? [ 5 -le 4 ] ; echo $?Using test with String Comparison Operators
- Use test with the = (string equality) comparison operator
[ "foo" = "foo" ] ; echo $? [ "foo" = "bar" ] ; echo $?- Use test with the != (string not equal) comparison operator
[ "foo" != "bar" ] ; echo $? [ "foo" != "foo" ] ; echo $?Using test with Single String Operators
- Use test with the -n (length greater than 0) string operator
[ -n "foo" ] ; echo $? [ -n "" ] ; echo $?- Use test with the -z (length is 0) string operator
[ -z "" ] ; echo $? [ -z "foo" ] ; echo $?Using test logical operators
- Use test with the -a (logical AND) operator
[ 5 -lt 6 -a "foo" != "bar" ]; echo $? [ 5 -lt 6 -a "foo" != "foo" ]; echo $?- Use test with the -o (logical OR) operator
[ 5 -lt 6 -o "foo" != "foo" ]; echo $? [ 5 -lt 5 -o "foo" != "foo" ]; echo $?- Use test with the ! (logical NOT) operator
[ ! 5 -lt 5 -o "foo" != "foo" ]; echo $? [ ! 5 -lt 6 -o "foo" != "foo" ]; echo $?Using test with File and Directory Operators
- Create the empty file empty.txt
touch empty.txt- Make empty.txt read only
chmod 444 empty.txt- Create the file foo.txt
cat > foo.txt [Enter] foo [Enter] Control d- Make sure this file is not empty
cat foo.txt- Give yourself full privileges to this file
chmod 700 foo.txt- Create the directory dir1
mkdir dir1- Use test with the -d (is it a directory) operator
[ -d dir1 ]; echo $? [ -d foo.txt ]; echo $?- Use test with the -e (it exists) operator
[ -e foo.txt ]; echo $? [ -e bar.txt ]; echo $?- Use test with the -f (is an ordinary file) operator
[ -f foo.txt ]; echo $? [ -f dir1 ]; echo $?- Use test with the -r (exists and is readable) operator
[ -r empty.txt ]; echo $? [ -r xxxx.txt ]; echo $?- Use test with the -s (exists and has a size greater than 0) operator
[ -s foo.txt ]; echo $? [ -s empty.txt ]; echo $?- Use test with the -w (exists and is writeable) operator
[ -w foo.txt ]; echo $? [ -w empty.txt ]; echo $?- Use test with the -x (exists and is executable) operator
[ -x foo.txt ]; echo $? [ -x empty.txt ]; echo $?if ... then ... else ...
- Open up a second login session, while leaving your first login session going.
- You will use this second login session for file editing.
- This is so that your use of nano does not interfere with the script session in your first session.
- Make sure that you keep track of what is to be done in which login window!
- In your SECOND login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your SECOND login window: Using your favorite editor create the script arg_count.sh
#! /bin/bash # # prints the number of arguments given to this script if test $# -eq 0 then echo "You entered no arguments" else echo "You entered $# arguments" fi- In your FIRST login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your FIRST login window: Change the permissions on arg_count.sh to make it executable
chmod 755 arg_count.sh- In your FIRST login window: Test the script several times using bash with the -x option
bash -x arg_count.sh bash -x arg_count.sh foo bash -x arg_count.sh foo bar bash -x arg_count.sh foo bar bletchif ... then ... elif ...
- In your SECOND login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your SECOND login window: Using your favorite editor create the script arg_count_2.sh
#! /bin/bash # # prints the number of arguments given to this script if test $# -eq 0 then echo "You entered no arguments" elif test $# -eq 1 then echo "You entered 1 argument" elif test $# -eq 2 then echo "You entered 2 arguments" elif test $# -gt 2 then echo "You entered more than 2 arguments" fi- In your FIRST login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your FIRST login window: Change the permissions on arg_test.sh to make it executable
chmod 755 arg_count_2.sh- In your FIRST login window: Test the script several times using bash with the -x option
bash -x arg_count_2.sh bash -x arg_count_2.sh foo bash -x arg_count_2.sh foo bar bash -x arg_count_2.sh foo bar bletchSimple for Loop
- In your SECOND login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your SECOND login window: Using your favorite editor, create the script for_1.sh
#! /bin/bash # # demonstrates the for ... loop for arg do echo $arg done- In your FIRST login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your FIRST login window: Make this script executable
chmod 755 for_1.sh- In your FIRST login window: Run this script with an ambiguous file reference
./for_1.sh ~/*The ambiguous file reference ~/* is expanded into a list of all files in your home directoryfor ... in ... Loop
- In your SECOND login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your SECOND login window: Using your favorite editor, create the script for_2.sh
#! /bin/bash # # demonstrates the for ... in ... loop for arg in ~/* do echo $arg doneNotice that here the list created by the expansion of ~/* is written in the script itself, and is not taken from the command line.
- In your FIRST login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your FIRST login window: Make this script executable
chmod 755 for_2.sh- In your FIRST login window: Run this script without arguments
./for_2.shYou get the same results as with for_1.sh.
But here the arguments come expanding the ambigous file reference inside the script itself.Three Expression for Loop
- In your SECOND login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your SECOND login window: Using your favorite editor, create the script for_3.sh
#! /bin/bash # # demonstrates the three expression for loop for (( count=0; count<20; count++ )) do echo $count done- In your FIRST login window: Make sure you are in your ex24 directory
cd ~/it244/ex/ex24- In your FIRST login window: Make this script executable
chmod 755 for_3.sh- In your FIRST login window: Run this script
./for_3.shExit Your script Session
Use the exit command
exitYou should see a confirmation message confirming that the script session is finished, resulting in a file called typescript, and you will probably also noticed that you are back in the original directory, from where you first ran the script command.