Академический Документы
Профессиональный Документы
Культура Документы
A basic script file will be read by the shell line by line Files are read from the top down
A script like this will likely be a list of commands, one after another When executed, the shell acts just like it would if you typed these commands in one at a time
Copyright 2010, ITT ESI 3
Control commands are those that affect the normal top to bottom flow of a script There are many types of control commands
If statements Loops
For While Until Case
Syntax:
If [ statement ] then
fi
commands
The else code is only run in the case that the original expression is false
Syntax:
commands commands
fi
Copyright 2010, ITT ESI 8
Instead of using else statements that are followed by another if you can use a single command
elif
Syntax:
For loops do a set of commands a set number of times Each time the loop is run, the value of the starting variable is changed
This means we can get different results each time we go through the loop
10
Syntax:
done
commands
Note:
The values can be a list of words or numbers
Each iteration through the loop, the next value will be placed into loopVar
When the loop runs out of values, the script continues beyond done
Copyright 2010, ITT ESI 11
While loops will do a set of commands continuously until its test case turns false Syntax:
while [ expression ] do
done
commands
12
13
Syntax
commands
14
Case Structures allow your script to choose one set of commands to run from an entire list Syntax
case selectionVar in case1)
case2)
commands commands
esac
case3) commands
15
When the value in selectionVar exactly matches the value of a case, the commands directly below the case are executed
When the end of that case is reached, the script jumps to esac (case spelled backwards)
If no case is matched, then the script continues on without running any of the cases
16
The cases can use special characters to catch more possibilities for one case
* - matches any string of characters | - used between two strings to allow the case to run when EITHER string is matched ? matches any SINGLE character
Example:
cat|dog) echo This matches the text cat or dog
Copyright 2010, ITT ESI 17