cd ~/it244/ex
mkdir ex23
cd ex23
test, as [ ], with Numberstest 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.
[ 4 -eq 4 ] ; echo $?
Since 0 means success, this means that the expression evaluated to true.
test with the -ne (not equal for numbers) operator
[ 4 -ne 5 ] ; echo $?
0 means true.
[ 4 -ne 4 ]; echo $?
1 means false.
test with the -gt (greater than) operator
[ 4 -gt 5 ] ; echo $?
[ 5 -gt 4 ] ; echo $?
[ 5 -ge 4 ] ; echo $?
[ 5 -ge 5 ] ; echo $?
[ 4 -ge 5 ] ; echo $?
[ 4 -lt 5 ] ; echo $?
[ 5 -lt 4 ] ; echo $?
[ 4 -le 5 ] ; echo $?
[ 4 -le 4 ] ; echo $?
[ 5 -le 4 ] ; echo $?
test with String Comparison Operatorstest with the = (string equality) comparison operator
[ "foo" = "foo" ] ; echo $?
[ "foo" = "bar" ] ; echo $?
test with the != (string not equal) comparison operator
[ "foo" != "bar" ] ; echo $?
[ "foo" != "foo" ] ; echo $?
test with Single String Operatorstest with the -n (length greater than 0) string operator
[ -n "foo" ] ; echo $?
[ -n "" ] ; echo $?
test with the -z (length is 0) string operator
[ -z "" ] ; echo $?
[ -z "foo" ] ; echo $?
test logical operatorstest with the -a (logical AND) operator
[ 5 -lt 6 -a "foo" != "bar" ]; echo $?
[ 5 -lt 6 -a "foo" != "foo" ]; echo $?
test with the -o (logical OR) operator
[ 5 -lt 6 -o "foo" != "foo" ]; echo $?
[ 5 -lt 5 -o "foo" != "foo" ]; echo $?
test with the ! (logical NOT) operator
[ ! 5 -lt 5 -o "foo" != "foo" ]; echo $?
[ ! 5 -lt 6 -o "foo" != "foo" ]; echo $?
test with File and Directory Operators
touch empty.txt
chmod 444 empty.txt
cat > foo.txt [Enter]
foo [Enter]
Control d
cat foo.txt
chmod 700 foo.txt
mkdir dir1
test with the -d (is it a directory) operator
[ -d dir1 ]; echo $?
[ -d foo.txt ]; echo $?
test with the -e (it exists) operator
[ -e foo.txt ]; echo $?
[ -e bar.txt ]; echo $?
test with the -f (is an ordinary file) operator
[ -f foo.txt ]; echo $?
[ -f dir1 ]; echo $?
test with the -r (exists and is readable) operator
[ -r empty.txt ]; echo $?
[ -r xxxx.txt ]; echo $?
test with the -s (exists and has a size greater than 0) operator
[ -s foo.txt ]; echo $?
[ -s empty.txt ]; echo $?
test with the -w (exists and is writeable) operator
[ -w foo.txt ]; echo $?
[ -w empty.txt ]; echo $?
test with the -x (exists and is executable) operator
[ -x foo.txt ]; echo $?
[ -x empty.txt ]; echo $?
if ... then ... else ...
cd ~/it244/ex/ex23
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 bletch
if ... then ... elif ...
cd ~/it244/ex/ex23
chmod 755 arg_count_2.sh
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 bletch
~tsoro/it244_test/ex23.sh
When the script asks if you are ready for more, hit Return or Enter.