CH24: Scripts Loops Flashcards
Test []
test command returns:
1 if the test fail
0 if the test succeeds
[paul@RHEL4b ~]$ test 10 -gt 55 ; echo $? 1
how to show TEST[] command results in true / false form
$ test 56 -gt 55 && echo true || echo false
true
$ test 6 -gt 55 && echo true || echo false false
test command can also be written as square brackets
$ [ 56 -gt 55 ] && echo true || echo false
true
$ [ 6 -gt 55 ] && echo true || echo false
false
[ -d foo ]
Does the directory foo exist ?
[ -e bar ]
Does the file bar exist ?
[ ‘/etc’ = $PWD]
Is the string /etc equal to the variable $PWD
[ $1 != ‘secret’ ]
Is the first parameter different from secret
[ 55 -lt $bar ]
Is 55 less than the value of $bar ?
[ $foo -ge 1000 ]
Is the value of $foo greater or equal to 1000 ?
[ “abc” < $bar ]
Does abc sort before the value of $bar ?
[ -f foo ]
Is foo a regular file ?
[ -r bar ]
Is bar a readable file ?
[ foo -nt bar ]
Is file foo newer than file bar ?
[ -o nounset ]
Is the shell option nounset set ?
Tests can be combined with logical AND and OR.
$ [ 66 -gt 55 -a 66 -lt 500 ] && echo true || echo false
true