test
in Scriptsif ... then ... else ...
Constructif ... then ... elif ...
ConstructThere will be no graded quiz this week.
But there will be one the following week covering the material in the previous 3 Class Quizzes.
sshd
...cron
...cron
when to run itcron
will run the jobcron
job that runs a special shell script$ cat -n backup_tables.sh 1 #!/bin/bash 2 # 3 # backs up all tables in the version2 database 4 5 cd $v2/sql/bak 6 rm *.bak 2> /dev/null 7 dir=$(date +%Y-%m-%d) 8 rm -rf $dir 9 mysql version2 -u root -p$1 < $v2s/scripts/backup.sql 10 mkdir $dir 11 cp *.bak $dir 12 date=$(date +%Y-%m-%d) 13 echo $date ": Table backup complete"
rm *.bak 2> /dev/null
dir=$(date +%Y-%m-%d)
date
command in a subshell ...rm -rf $dir
mysql version2 -u root -p$1 < $v2s/scripts/backup.sql
mkdir $dir
cp *.bak $dir
date=$(date +%Y-%m-%d)
echo $date ": Table backup complete"
if ... then ...
Constructif ... then
statement ...
if COMMAND
then
COMMAND_1
COMMAND_2
...
fi
if
is test
...then
must either be on a separate line from if
...fi
must close the conditional statementthen
and fi
are executed ...if
if
runs without error ...if ... then
statement treats as truetest
test
command evaluates a logical expression ...if
statement, a status code of 0 means true ...test
Operatorstest
has a number of operators
Operator Condition Tested -eq Two numbers are equal -ne Two numbers are not equal -ge The first number is greater than, or equal to, the second -gt The first number is greater than the second -le The first number is less than, or equal to, the second -lt The first number is less than the second
test
uses the different operators when comparing strings
Operator Condition Tested = When placed between strings, are the two strings the same != When placed between strings, are the two strings not the same
test
uses symbols (=) when comparing strings
Operator Condition Tested -z Whether the string given as an argument has a length of 0
Operator Condition Tested -d Whether the argument is a directory -e Whether the argument exits as a file or directory -f Whether the argument is an ordinary file (not a directory) -r Whether the argument exists and is readable -s Whether the argument exists and has a size greater than 0 -w Whether the argument exists and is writable -x Whether the argument exists and is executable
test
uses when evaluating two expressions
Operator Condition Tested -a Logical AND meaning both expressions must be true -o Logical OR meaning either of the two expressions must be true
test
in Scriptstest
in an if
statement
$ if test foo = foo > then > echo "The two strings are equal" > fi The two strings are equal
if
statement ...if
statement ...test
...
if test $number1 -gt $number2
or
if [ $number1 -gt $number2 ]
test
...$ [ 5 -ne 6] -bash: [: missing `]'
test
does not return a value to standard outputtest
returns true or false ...test
returns an exit status of 0 ...test
returns an exit status of 1 ...$ [ 5 -eq 4 ]; echo $? 1 $ [ 5 -ne 4 ]; echo $? 0
Usage: PROGRAM_NAME ARG1 ARG2 ...
Usage: test_dr.sh DIR_NAME
$ cat examples_it244/usage_1.sh #! /bin/bash # this program demonstrates checking for arguments # and printing a usage message when # the expected arguments are not supplied if test $# -eq 0 then echo Usage: $0 STRING exit 1 fi echo Received argument $1 $ examples_it244/usage_1.sh Usage: examples_it244/usage_1.sh STRING $ examples_it244/usage_1.sh foo Received argument foo
basename
basename
takes a pathname as an argument ...
$ basename examples_it244/usage_1.sh
usage_1.sh
$ cat examples_it244/usage_2.sh #! /bin/bash # this program demonstrates checking for arguments # and printing a usage message using basename if test $# -eq 0 then echo Usage: $(basename $0) STRING exit 1 fi echo Received argument $1 $ examples_it244/usage_2.sh Usage: usage_2.sh STRING $ examples_it244/usage_2.sh foo Received argument foo
basename
and command substitution ...if ... then ... else ...
Constructif ... then ... else ...
statement
if COMMAND
then
COMMAND_1
COMMAND_2
....
else
COMMAND_A
COMMAND_B
...
fi
$ cat cat_file.sh #! /bin/bash # # demonstrates the use of the if ... then ... else ... construct if [ $# -eq 0 ] then echo Usage: $(basename $0) filename exit 1 fi if [ -f $1 ] then cat $1 else echo $1 is not a file fi $ ./cat_file.sh Usage: cat_file.sh filename $ ./cat_file.sh lines.txt line 1 line 2 line 3 line 4 line 5 $ ./cat_file.sh foo foo is not a file
if
statement ...
[ -f $1 ]
if ... then ... elif ...
Constructif ... then ... elif ...
construct ...
if COMMAND
then
COMMAND_1
COMMAND_2
...
elif OTHER_COMMAND
then
COMMAND_A
COMMAND_B
...
...
else
COMMAND_N1
COMMAND_N2
...
fi
elif
stands for "else if"elif
must be followed by then
then
must either be on the next line ...else
statement must be ended by a fi
...elif
only requires a single fi
at the end
$ cat if_4.sh
#! /bin/bash
#
# demonstrates the if ... then ... elif ...
construction
echo -n "word 1: "
read word1
echo -n "word 2: "
read word2
echo -n "word 3: "
read word3
if [ $word1 = $word2 -a $word2 = $word3 ]
then
echo "Match: words 1, 2 & 3"
elif [ $word1 = $word2 ]
then
echo "Match: words 1 & 2"
elif [ $word1 = $word3 ]
then
echo "Match: words 1 & 3"
elif [ $word2 = $word3 ]
then
echo "Match: words 2 & 3"
else
echo No match
fi
$ ./if_4.sh
word 1: foo
word 2: bar
word 3: bletch
No match
$ ./if_4.sh
word 1: foo
word 2: foo
word 3: boo
Match: words 1 & 2
bash
with the -x optionbash
to print each command ...$ cat match_three.sh #! /bin/bash # # takes three stings as input and compares them if [ $# -lt 3 ] then echo Usage: $(basename $0) STRING1 STRING2 STRING3 exit 1 fi if [ $1 = $2 -a $2 = $3 ] then echo All arguments match elif [ $1 = $2 ] then echo Arguments 1 and 2 match elif [ $1 = $3 ] then echo Arguments 1 and 3 match elif [ $2 = $3 ] then echo Arguments 2 and 3 match else echo No arguments match fi $ bash -x match_three.sh foo bar foo + '[' 3 -lt 3 ']' + '[' foo = bar -a bar = foo ']' + '[' foo = bar ']' + '[' foo = foo ']' + echo Arguments 1 and 3 match Arguments 1 and 3 match
bash
prints a line from the script ...test
commands ...bash
with the -x option ...bash
takes through your scriptif
constructions certain blocks of commands ...then
, else
, elif
and fi
#! /bin/bash # # responds with the number of the arguments given # to this script if test $# -eq 0 then echo "You entered no arguments" fi if test $# -eq 1 then echo "You entered 1 argument" fi if test $# -eq 2 then echo "You entered 2 arguments" fi if test $# -gt 2 then echo "You entered more than 2 arguments" fi
if ... then
statement ...#! /bin/bash # # responds with the number of the arguments given # to this script if test $# -eq 0 then echo "You entered no arguments" fi if test $# -eq 1 then echo "You entered 1 argument" fi if test $# -eq 2 then echo "You entered 2 arguments" fi if test $# -gt 2 then echo "You entered more than 2 arguments" fi