CISC3130 Spring 2013 Fordham Univ.
1
Bash Scripting: control structures CISC3130 Spring 2013 Fordham - - PowerPoint PPT Presentation
Bash Scripting: control structures CISC3130 Spring 2013 Fordham Univ. 1 Roadmap Shell variables Shell Arithmetic Control Structures Test, condition, logic operations Selection: if statements Loop: while, until, for
1
2
3
4
Example:
[zhang@storm ~]$ x=1 [zhang@storm ~]$ export x [zhang@storm ~]$ bash [zhang@storm ~]$ echo $x 1
5
6
printf "The arguments were %s\n" "$*"
lpr "$@" Print each
7
8
9
10
11
12
13
rm `ls *.o` ## same as rm *.o
time1=$(date); echo $times1 ## set the output of date to variable times
14
15
16
From highest precedence to lowest Relational operators (<, <=, …) produces a numeric result that acts as a truth value
17
18
19
20
21
22
23
Compound condition: combine above using ! (negation), && (and), ||
if !grep pattern myfile > /dev/null …
24
Convention: command/program returns a zero when it succeeds and
If this is what you want, do so explicitly using
25
26
27
28
29
30
Numerical tests work on integers only.
-d file: true if the file is a directory -e file: true if the file exists -f file: true if the file is a regular file. -s file: true if the file has nonzero size
31
32
33
34
35
36
37
Can be repeated…
38
39
40
41
42
43
44
45
46
47
48
49
command1 && command2 //if command1 fails, command2 will
command1 || command2 //if command1 succeeds, command2
50
51
52
53
55
56
57
58
test if string is null Lazy evaluation of && and ||
59
60
61