CH24: Scripts Loops Flashcards

1
Q

Test []

A

test command returns:
1 if the test fail
0 if the test succeeds

[paul@RHEL4b ~]$ test 10 -gt 55 ; echo $? 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to show TEST[] command results in true / false form

A

$ test 56 -gt 55 && echo true || echo false
true
$ test 6 -gt 55 && echo true || echo false false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

test command can also be written as square brackets

A

$ [ 56 -gt 55 ] && echo true || echo false
true
$ [ 6 -gt 55 ] && echo true || echo false
false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

[ -d foo ]

A

Does the directory foo exist ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

[ -e bar ]

A

Does the file bar exist ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

[ ‘/etc’ = $PWD]

A

Is the string /etc equal to the variable $PWD

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

[ $1 != ‘secret’ ]

A

Is the first parameter different from secret

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

[ 55 -lt $bar ]

A

Is 55 less than the value of $bar ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

[ $foo -ge 1000 ]

A

Is the value of $foo greater or equal to 1000 ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

[ “abc” < $bar ]

A

Does abc sort before the value of $bar ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

[ -f foo ]

A

Is foo a regular file ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

[ -r bar ]

A

Is bar a readable file ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

[ foo -nt bar ]

A

Is file foo newer than file bar ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

[ -o nounset ]

A

Is the shell option nounset set ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Tests can be combined with logical AND and OR.

A

$ [ 66 -gt 55 -a 66 -lt 500 ] && echo true || echo false

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

if then else

A
#!/bin/bash
if [ -f isit.txt ]
    then echo isit.txt exists! 
    else echo isit.txt not found! 
fi

tests whether a file exists, and if the file exists then a proper message is echoed.

17
Q

if then elif

A
#!/bin/bash
count=42
if [ $count -eq  42 ] 
then 
      echo "42 is correct." 
elif [ $count -gt 42 ]
then
      echo "Too much." 
else 
     echo "Not enough." 
fi

nest a new if inside an else with elif

=———————————-

18
Q

for loop

A

for i in 1 2 4
do
echo $i
done

19
Q

for loop with embedded shell

A
#!/bin/ksh
for counter in `seq 1 20`
do
      echo counting from 1 to 20, now at $counter 
      sleep 1
done

-=-=========——————=====———–===————————==
The same example as above can be written without the embedded shell using the bash {from..to} shorthand.

#!/bin/bash
for counter in {1..20} 
do
     echo counting from 1 to 20, now at $counter 
     sleep 1
done
20
Q

for loop uses file globbing

A

kahlan@solexp11$ ls count.ksh go.ksh

kahlan@solexp11$ for file in *.ksh ; do cp $file $file.backup ; done

kahlan@solexp11$ ls count.ksh count.ksh.backup go.ksh go.ksh.backup

21
Q

while loop

A

i=100;
while [ $i -ge 0 ] ; do
echo Counting down, from 100 to 0, now at $i; let i–;
done

22
Q

Endless loops can be made with while true or while : , where the colon is the equivalent of no operation in the Korn and bash shells.

A
#!/bin/ksh 
# endless loop 
while :
do
    echo hello 
    sleep 1 
done
23
Q

until loop

A
let i=100;
until [ $i -le 0 ] ; 
do
    echo Counting down, from 100 to 1, now at $i;
    let i--;
done