Conditional statement Flashcards

1
Q

primary expression:

if

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

combining expression:

if

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

checks for the existence of a file

A

!/bin/bash

anny ~> cat msgcheck.sh

echo “This scripts checks the existence of the messages file.”

echo “Checking…” if [-f /var/log/messages]

then echo “/var/log/messages exists.”

fi

echo echo “…done.”

anny ~> ./msgcheck.sh

This scripts checks the existence of the messages file.

Checking…

/var/log/messages exists.

…done.

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

Checking shell options

A

These lines will print a message if the noclobber #option is set:

if [-o noclobber]

then echo “Your files are protected against accidental overwriting using redirection.”

fi

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

Testing exit status

A

anny ~> if [$? -eq 0]

More input> then echo ‘That was a good job!’

More input> fi

That was a good job!

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

numerical comparisons

A

anny > num=wc -l work.txt

anny > echo $num 201

anny > if [“$num” -gt “150”] More input> then echo ; echo “you’ve worked hard enough for today.” More input> echo ; fi

you’ve worked hard enough for today.

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

String comparisons-Standard

A

if [”$(whoami)” != ‘root’]; then

echo “You have no permission to run $0 as non-root user.”

exit 1;

fi

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

String comparison-Shortened

A

[”$(whoami)” != ‘root’] && ( echo you are using a non-privileged account; exit 1 )

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

String comparison-Regular Expression

A

anny > gender=”female”

anny > if [[“$gender” == f*]]

More input> then echo “Pleasure to meet you, Madame.”; fi

Pleasure to meet you, Madame.

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

String comparison- Programmer

A

test “$(whoami)” != ‘root’ && (echo you are using a non-privileged account; exit 1)

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

if/then/else constructs

A

freddy scripts> gender=”male”

freddy scripts> if [[“$gender” == “f*”]]

More input> then echo “Pleasure to meet you, Madame.”

More input> else echo “How come the lady hasn’t got a drink yet?”

More input> fi How come the lady hasn’t got a drink yet?

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

Single Bracket v.s. Double Bracket

A

[[ prevents word splitting of variable values. eg. VAR=”var with spaces”, you do not need to double quote $VAR in a test - eventhough using quotes remains a good habit.

[[ prevents pathname expansion, so literal strings with wildcards do not try to expand to filenames.

Using [[, == and != interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: [[“value” == val*]].

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

Checking command line arguments

A

!/bin/bash

anny ~> cat weight.sh

This script prints a message about your weight if # you give it your

weight in kilos and height in centimeters. weight=”$1”

height=”$2”

idealweight=$[$height - 110]

if [$weight -le $idealweight] ; then

echo “You should eat a bit more fat.”

else

echo “You should eat a bit more fruit.”

fi

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

Testing the number of arguments

A

!/bin/bash

anny ~> cat weight.sh

This script prints a message about your weight if #you give it your

weight in kilos and height in centimeters.

if [! $# == 2]; then

echo “Usage: $0 weight_in_kilos length_in_centimeters”

exit

fi

weight=”$1”

height=”$2”

idealweight=$[$height - 110]

if [$weight -le $idealweight] ; then

echo “You should eat a bit more fat.”

else

echo “You should eat a bit more fruit.”

fi

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

Testing that a file exists

A

!/bin/bash

This script gives information about a file.

FILENAME=”$1”

echo “Properties for $FILENAME:”

if [-f $FILENAME]; then

echo “Size is $(ls -lh $FILENAME | awk ‘{ print $5 }’)”

echo “Type is $(file $FILENAME | cut -d”:” -f2 -)”

echo “Inode number is $(ls -i $FILENAME | cut -d” “ -f1 -)”

echo “$(df -h $FILENAME | grep -v Mounted | awk ‘{ print “On”,$1”, \

which is mounted as the”,$6,”partition.”}’)”

else

echo “File does not exist.”

fi

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

if/then/elif/else constructs

A

!/bin/bash

anny /etc/cron.daily> cat disktest.sh

This script does a very simple test for checking disk # space.

space=df -h | awk '{print $5}' | grep % | grep -v Use | sort -n | tail -1 | cut -d "%" -f1 - alertvalue=”80”

if [“$space” -ge “$alertvalue”]; then

echo “At least one of my disks is nearly full!” | mail -s “daily diskcheck” root

else

echo “Disk space normal” | mail -s “daily diskcheck” root

fi

17
Q

Boolean operations

A
18
Q

exit statement and if

A
19
Q

case statement

A
20
Q
A