Download presentation
Presentation is loading. Please wait.
1
Shell Programming (ch 10)
IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
2
Vim Commands vim filename Add contents: i/a Back to command mode: ESC
Save the file: :w Delete: x Quit: :q
3
Control Flow The order of executing commands if..then
4
Control Flow The order of executing commands if..then Example
~it244/it244/scripts/if1
5
Control Flow The order of executing commands if..then
Built-in utility test man test Example (-eq) ~it244/it244/scripts/chkargs $#: the number of arguments exit 1: terminate with a status code ‘1’
6
Control Flow The order of executing commands if..then Read arguments
Example ~it244/it244/scripts/read3args
7
Control Flow The order of executing commands if..then
test (-f) and arguments Example ~it244/it244/scripts/is_ordfile Other options (Table 10-1)
8
Control Flow The order of executing commands if..then
test ([ ]) and arguments Example ~it244/it244/scripts/chkargs2 White spaces [ $# -eq 0 ] Output error message: echo … … 1>&2
9
Control Flow The order of executing commands if..then..else
10
Control Flow The order of executing commands if..then..else
Read arguments Example ( shift ) ~it244/it244/scripts/shift2args Example ( ) ~it244/it244/scripts/allargs
11
Control Flow The order of executing commands if..then..else
Optional arguments Example ~it244/it244/scripts/out
12
Control Structure if..then..elif
13
Control Structure if..then..elif 1 & 2 & 3, 1 & 2, 1 & 3, 2 & 3,
Example: Compare 3 input words and find the match (word1, word2, word3) 1 & 2 & 3, 1 & 2, 1 & 3, 2 & 3, no match
14
Control Structure if..then..elif
Example: Compare 3 input words and find the match (word1, word2, word3) if word1=word2 =word3 then … elif word1=word2 elif word1=word3 elif word2=word3 else fi 1 & 2 & 3 1 & 2 1 & 3 2 & 2 No match
15
Control Structure if..then..elif
Example: Compare 3 input words and find the match (word1, word2, word3) Combine two conditions test condition1 -a condition2 test condition1 -o condition2 ~it244/it244/scripts/if3
16
Control Structure for loop for loop-index in argument-list do commands
done Example ~it244/it244/scripts/fruit ~it244/it244/scripts/dirfiles
17
Control Structure for loop A range for argument-list Example
{1..10}, {a..z} for loop-index in range do commands done Example ~it244/it244/scripts/for_range ~it244/it244/scripts/for_range2
18
Control Structure for loop Repeat operations
Example ~it244/it244/scripts/for_print Nested loops ~it244/it244/scripts/for_print2
19
Control Structure for loop for loop-index (in $@) do commands done
Example ~it244/it244/scripts/for_test
20
Control Structure while loop while test-command do commands done
21
Control Structure while loop while test-command do commands done
Example: ~it244/it244/scripts/count Arithmetic evaluation: ((a=b+2)) ~it244/it244/scripts/for_while_print
22
Control Structure until loop until test-command do commands done
23
Control Structure until loop until test-command do commands done
Example: ~it244/it244/scripts/guessnumber ~it244/it244/scripts/guessnumber2
24
Control Structure break and continue break: jump out of the loop
continue: proceed to “done” Example: ~it244/it244/scripts/brk
25
Control Structure select select varname [in arg …] do commands done
Example ~it244/it244/scripts/select1
26
Control Structure select select varname [in arg …] do commands done
PS3: select prompt $REPLY: index of the selected item Terminate: exit, break Example: ~it244/it244/scripts/select2
27
Parameters and Variables
Special parameters $#: number of arguments $1-$n: arguments $0: name of the script Example: ~it244/it244/scripts/abc ./abc ~it244/it244/scripts/abc basename: ~it244/it244/scripts/abc2 basename ~it244/it244/scripts/abc
28
Parameters and Variables
Special parameters $$: PID number echo $$ ps Example: ~it244/it244/scripts/id2 ./id2
29
Parameters and Variables
Special parameters $?: exit status ls; echo $? ls xyz; echo $? cat yyy; echo $? Example: ~it244/it244/scripts/es ./es echo $?
30
Parameters and Variables
Initialize command-line arguments set arg1 arg2 … ~it244/it244/scripts/set_it date ~it244/it244/scripts/dateset
31
Some More Examples Get file sizes ~/it244/it244/scripts/filesize
$ls -l -rwxr-xr-x 1 it244 libuuid :09 abc -rwxr-xr-x 1 it244 libuuid :10 abc2 -rwxr-xr-x 1 it244 libuuid :49 bb1 -rwxr-xr-x 1 it244 libuuid :49 bb2 … … … for i in * do set -- $(ls -l "$i") echo $i: $5 bytes done
32
File Descriptors An index number of a open file
3 system file descriptors 0(std input), 1(std output), 2(std err) Create a file descriptor exec n > outfile exec m < infile exec n<>file exec n>>outfile
33
File Descriptors Duplicate a file descriptor Close a file descriptor
exec n<&m exec n>&m Close a file descriptor exec n<&- Example: ~it244/it244/scripts/exec_read ~it244/it244/scripts/exec_write
34
File Descriptors Example: ~it244/it244/scripts/exec_read
while read <&3 myline do echo "*** $myline ***" done File descriptor is “3” ($1 is the first argument) &3 represents the input file in the program (a pointer) “read <&3 myline” read a line from the file and assign it to variable “myline” (pointer &3 moves to the next line)
35
File Descriptors Example: ~it244/it244/scripts/exec_read
while read <&3 myline do echo "*** $myline ***" done Command as a condition True: if the exit status is 0 (successfully executed) False: if the exit status > 0 (sth went wrong) “read” gets an error when EOF is reached
36
Built-in Commands Do not fork a new process “read” user input
type cat echo who if “read” user input read firstline echo “$firstline” ~it244/it244/scripts/read1 ~it244/it244/scripts/read1_no_quote type in this is *
37
Built-in Commands “read” user input
“REPLY”: default variable for “read” ~it244/it244/scripts/read1a “-p” option: with prompt read -p “Enter a command: ” cmd ~it244/it244/scripts/read2 “read” multiple variables ~it244/it244/scripts/read3 type in this is sth else
38
Built-in Commands “read” user input “read” from files
read line1 < names echo $line1 read line2 < names echo $line2 (read line1; echo $line1; read line2; echo $line2) < names
39
Built-in Commands “read” user input “read” from files
~it244/it244/scripts/readfile ./readfile < names -u option exec 3<names read –u3 line1 (read <&3 line1) echo $line1 read –u3 line2 echo $line2 exec 3<&-
40
Built-in Commands “trap” : catch a signal
Signal: a report about a condition (Table 10-5) kill -l, trap -l, man 7 signal trap [‘commands’] [signal] ‘’ is not required but highly suggested Example: ~it244/it244/scripts/inter
41
Built-in Commands “trap” : catch a signal
trap ‘’ signal: ignore the signal Example 2: CTRL+C, 20: CTRL+Z ~it244/it244/scripts/locktty stty (input secret/passwd) ~it244/it244/scripts/locktty2 Multiple traps ~it244/it244/scripts/cleanfile
42
Built-in Commands “kill” : abort a process kill [-signal] PID
The KILL signal (9) kill -9 %1
43
Expressions Arithmetic evaluation a=10; b=5; let a=a*10+b; echo $a
No path expansion (*) ((a=a*10+b)) let “a=a+1” b=a*b ((a=a+1, b=a*b))
44
Expressions Logic expression (( )), [[ ]]
In [[]], must precede a variable name with $ >, <, -lt, -gt, … && (and), || (or) ~it244/it244/scripts/age2 String comparison [[ “aa” < “bbb” ]] [[ artist = a* ]]; echo $? [[ a* = artist ]]; echo $?
45
Expressions Arithmetic operators +,-,*,/,% var++, var--, ++var, --var
a=5;b=3;echo $((a%b)) var++, var--, ++var, --var echo $((a++)); echo $a echo $((--a)); echo $a echo $((a+++3)) echo $((a++---b)) ~it244/it244/scripts/printstar
46
Expressions Logical operators true, false !, ==, != &&, ||
true; echo $?; false; echo $? !, ==, != ((1==1)); echo $?; echo $((1==1)) ((1!=1)); echo $?; echo $((1!=1)) &&, || true && false; echo $? true || false; echo $? ls –l testop && cat testop; echo $? ls –l testop || cat testop; echo $?
47
Expressions Logical operators &&, || Short-circuiting: Example
Cond1 && Cond2: does not evaluate Cond2 if Cond1 is false Cond1 || Cond2: does not evaluate Cond2 if Cond1 is true Example mkdir bkup && cp testop bkup rm bkup/* mkdir bkup && cp testop bkup; ls bkup mkdir bkup || echo “mkdir failed” a=1;b=2;echo $((a||b++));echo $b
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.