if Statementsif ... then Constructtesttest operatorstest Operators for Comparing Numberstest String OperatorstestAre there any questions before I begin?
You can connect to Gradescope to take weekly graded quiz today during the last 15 minutes of the class.
Once you start the quiz you have 15 minutes to finish it.
$ history 5
540 cat output.txt
541 echo "Go Red Sox" > output.txt
542 cat output.txt
543 echo foo
544 history 5
$ !543
echo foo
foo
$ alias ll="ls -l" $ ll total 2 lrwxrwxrwx 1 it244gh man 34 Sep 6 21:09 it244 -> /courses/it244/s19/ghoffman/it244gh drwxr-xr-x 2 it244gh ugrad 512 Oct 27 09:16 work
$ touch foo{1,2,3,4,5}.txt $ ls foo1.txt foo2.txt foo3.txt foo4.txt foo5.txt
$ touch {a,ab,abc}.txt $ ls abc.txt ab.txt a.txt
$ echo ~ /home/ghoffman
$ echo ~it244gh /home/it244gh
$ echo $SHELL /bin/bash $ echo $? 0
$ echo 5 + 4 5 + 4
$ echo $(( 5 + 4 )) 9
$ echo $a $b 5 3$ echo $(($a * $b)) 15
$ echo $(( a * b))
15
$(COMMAND)
$ today=$(date) $ echo $today Tue Oct 25 17:00:07 EDT 2011
$ args.sh $PATH 1 args: [/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/oracle/12.1/client64/bin:/home/ghoffman/bin/shell:/home/ghoffman/bin/python/umb:/home/ghoffman/bin:/home/ghoffman/bin/shell:/home/ghoffman/bin/python/umb] $ IFS=: $ args.sh $PATH 15 args: [/usr/local/sbin] [/usr/local/bin] [/usr/sbin] [/usr/bin] [/sbin] [/bin] [/usr/games] [/usr/local/games] [/snap/bin] [/usr/lib/oracle/12.1/client64/bin] [/home/ghoffman/bin/shell] [/home/ghoffman/bin/python/umb] [/home/ghoffman/bin] [/home/ghoffman/bin/shell] [/home/ghoffman/bin/python/umb]
<(COMMAND)
if Statementsif statementif statement, a certain segment of code is executed ...if statements more complicatedif ... then Constructif ... then construction
if TEST_COMMAND
then
COMMAND_1
COMMAND_2
...
fi
fi is if spelled backwardsif ...then and fi are run
if statement
$ cat foo_in_dir.sh
#! /bin/bash
# prompts for a directory and creates foo.txt there
read -p 'Directory: ' dir
if cd $dir
then
touch foo.txt
fi
if statement then and fithen on the same line as ifthen
if COMMAND; then
COMMAND_1
COMMAND_2
...
fi
then and fiif statement might seem strangecd to go to that directorytestif statement testtest takes a
boolean expression
as an argument
test returns an exit status of 0 if the expression is true ...test is a
built-intest is a kludgetest operatorstest can be used to check for a number of different conditionstest is much more complicatedtest has five different categories of operators
test Operators for Comparing Numbers| Operator | Result |
|---|---|
| -eq | True if the numbers are equal |
| -ne | True if the numbers are not equal |
| -ge | True if the first number is greater than, or equal to, the second |
| -gt | True if the first number is greater than the second, the second |
| -le | True if the first number is less than, or equal to, the second |
| -lt | True if the first number is less than the second, the second |
echo ? to see
the exit status
$a=1; b=2 $ test $a -gt $b; echo $? # is a greater than b? 1 $ test $a -ge $b; echo $? # is a greater than or equal to b? 1 $ test $a -lt $b; echo $? # is a less than b? 0 $ test $a -le $b; echo $? # is a less than or equal to b? 0 $ test $a -eq $b; echo $? # is a equal to b? 1 $ test $a -ne $b; echo $? # is a not equal to b? 0
test String Operatorstest uses different operators with strings| Operator | Result |
|---|---|
| = | True if two strings are the same |
| != | True if two strings are NOT the same |
| -n | True if the string has a length greater than 0 |
| -z | True if the string has a length of 0 |
$ s1=foo; s2=bar $ test $s1 = $s2; echo $? 1 $ test $s1 != $s2; echo $? 0 $ test -n $s2; echo $? 0 $ test -n ""; echo $? 1 $ test -z ""; echo $? 0
$ test "foo" = "FOO"; echo $? 1 $ test "foo" != "FOO"; echo $? 0
test that can be used on files and directories| Operator | Result |
|---|---|
| -d | True if the argument is the name of a directory |
| -e | True if the argument is the name of a file or directory |
| -f | True if the argument is the name of an ordinary file (i.e. not a directory) |
| -s | True if the argument is the name of a file or directory that is not empty |
| -r | True if the argument is the name of a file or directory that is readable |
| -w | True if the argument is the name of a file or directory that is writable |
| -x | True if the argument is the name of a file or directory that is executable |
$ ls -ld dir1 # the directory I will use for testing drwxr-xr-x 4 glenn staff 128 Aug 9 11:24 dir1 $ test -d dir1; echo $? # is it a directory? 0 $ test -e dir1; echo $? # is it a file or a directory? 0 $ test -f dir1; echo $? # is it an ordinary file? 1 $ test -s dir1; echo $? # does the directory have things in it? 0 $ test -r dir1; echo $? # do I have read permission? 0 $ test -w dir1; echo $? # do I have write permission? 0 $ test -x dir1; echo $? # do I have execute permission? 0 $ ls -l foo1.txt # the file I will use for testing -rw-r--r-- 1 glenn staff 46 Aug 9 11:24 foo1.txt $ test -e foo1.txt ; echo $? # is it a file or a directory? 0 $ test -f foo1.txt ; echo $? # is it an ordinary file? 0 $ test -s foo1.txt ; echo $? # does the file have something in it? 0 $ test -r foo1.txt ; echo $? # do I have read permission? 0 $ test -w foo1.txt ; echo $? # do I have write permission? 0 $ test -x foo1.txt ; echo $? # do I have execute permission? 1
test 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 |
| ! | Logical NOT, meaning true becomes false and false becomes tru |
$ test 1 -lt 5 -a 5 -lt 10; echo $? # is 1 less than 5 AND 5 less than 10 0 $ test 5 -lt 1 -a 5 -lt 10; echo $? # is 5 less than 1 AND 5 less than 10 1 $ test 5 -lt 1 -o 5 -lt 10; echo $? # is 5 less than 1 OR 5 less than 10 0 $ test 5 -lt 1; echo $? # is 5 less than one 1 $ test ! 5 -lt 1; echo $? # is 5 NOT less than 1 0
testtest much it will be hard to remember them