Outline. Shell Programming - continued. Testing conditions Conditional statements Loop statements

Outline  Shell Programming - continued – Testing conditions – Conditional statements – Loop statements 3/14/2011 7:09:45 PM unix-lect12-shell-cond...
Author: Sabina Ford
2 downloads 0 Views 323KB Size
Outline 

Shell Programming - continued – Testing conditions – Conditional statements – Loop statements

3/14/2011 7:09:45 PM

unix-lect12-shell-cond-loop.ppt

1

Conditional Statements 

We often need to take different actions depending on user input, status of a command, status of a file, and so on – In order to perform conditional dependent actions, we need conditional statements – For conditional statements, we first need to be able to test conditions

3/14/2011 7:09:46 PM

unix-lect12-shell-cond-loop.ppt

2

Testing Conditions 

The two general forms of testing are: test or [ ] – The latter method is easier to read • Note a space before and after the bracket is required

– A condition can be reversed with a ! before the condition (this is the same as not condition) [ ! ] – A ‘:’ command in place of condition always returns true 3/14/2011 7:09:47 PM

unix-lect12-shell-cond-loop.ppt

3

Testing File Attributes 

To test if a file/directory is readable [ -r prog.txt ] [ -r $1.c ]



To test if a file/directory is writable [ -w specialfile.txt ] Note that a directory is writable only if the permission has both w and x bits



To test if a file/directory is executable [ -x prog4.sh ]



To test if a file exists and it is a regular file (e.g., not a directory) [ -f temp.text ]



To test if it is a directory [ -d cop3353 ]



Testing for the negation - use ! (eg. not writeable) [ ! -w nochange.txt ]

3/14/2011 7:09:47 PM

unix-lect12-shell-cond-loop.ppt

4

List of File Testing in “man bash” Not required for exams except the ones given on the previous slide

3/14/2011 7:09:48 PM

unix-lect12-shell-cond-loop.ppt

5

List of File Testing in “man bash” – cont.

Not required on the exam (but you may need some for homework assignment #4) For example, Problem 6 in homework assignment #4 asks you to make a copy of a file if the source file has been updated or the target file does not exist - This can be done using if [ targetfile –ot sourcefile ] then # copy a file fi

3/14/2011 7:09:49 PM

unix-lect12-shell-cond-loop.ppt

6

Numeric Tests 

The following operators can be used for numeric tests: { -eq, -ne, -gt, -ge, -lt, -le }



Examples [ [ [ [

$1 $1 $# $#

–lt –gt -eq -lt

3/14/2011 7:09:49 PM

$2 ] 0 ] 2 ] 3 ]

unix-lect12-shell-cond-loop.ppt

7

Testing Strings 

[ -z string ] – True if the string is empty



[ -n string ] or [ string ] – True if the string is non-zero



[ string1 = string2 ] or [ string1 == string2 ] – True if the two strings are the same



[ string1 != string2 ] – True if the two strings are different

3/14/2011 7:09:50 PM

unix-lect12-shell-cond-loop.ppt

8

Testing Strings – cont. 

[ string1 < string2 ] – True if string1 is lexicographically before string2



[ string1 > string2 ] – True if string1 is lexicographically after string2



Similarly – [ string1 >= string2 ] • True if string1 is lexicographically after string2 or the same

– [ string1

Suggest Documents