2. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 1
3. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 2
4. stdin, stdout, stderr
C 从舒亢亟仄 仗仂亠仂仄 于磶舒仆 仗仂仂从亳 于于仂亟舒于于仂亟舒 舒亶仍仂于亠
亟e从亳仗仂 stdin, stdout, stderr.
亳仄亠. 仂仄亠舒 舒亶仍仂于 亟e从亳仗仂仂于 亳 仂亶于.
1 echo / dev / std*
2 echo / dev / std* | xargs t n 1 readlink
3 lsof ap $BASHPID d 0 ,1 ,2
Linux administration course. 2
11. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 6
12. 仆仂于仆亠 从仂仆从亳亳 亟仍 亳从仍仂于
for 亳从仍 仗仂仄仂舒 (foreach), 亳从仍 仂 亠亳从仂仄
while, until 亳从仍 仗亠亟仍仂于亳亠仄
break, continue 亟仂仂仆亶 于仂亟, 仗仂仗从 亳亠舒亳亳
Linux administration course. 6
13. 丶亳从仍 for. 亳仄亠.
亠亶于亳亠 仆舒亟 舒亶仍舒仄亳.
1 f o r file in *
2 do md5sum $file
3 done
亠亠亳仍亠仆亳亠 仍亠仄亠仆仂于.
1 f o r planet in Mars Earth Mercury Saturn
2 do ssh $planet uname a > $planet
3 done
Linux administration course. 7
14. 丶亳从仍 for. 丶亳仂于舒 仗仂仍亠亟仂于舒亠仍仆仂.
亳仄亠 亞亠仆亠舒亳亳.
1 f o r num in 1 2 3 4 5 6 7 8 9 10 # 仗仂仂亠
仗亠亠亳仍亠仆亳亠
2 do ping 1 9 2 . 1 6 8 . 1 0 . $num
3 done
4
5 f o r num in $ ( seq 1 10) # 亞亠仆亠舒亳 亳亰 于仆亠仆亠亶
从仂仄舒仆亟
6 do ping 1 9 2 . 1 6 8 . 1 0 . $num
7 done
8
9 f o r num in { 1 . . 1 0 } # 亞亠仆亠舒亳 于仂亠仆仆仄亳
亠亟于舒仄亳
10 do ping 1 9 2 . 1 6 8 . 1 0 . $num
11 done
Linux administration course. 8
15. 丶亳从仍 for. 丶亳仂于舒 仗仂仍亠亟仂于舒亠仍仆仂.
亳仄亠亳亠从亳亶 仂仄舒 (Clike)
1 f o r ( ( i =1; i <11; i ++) )
2 do
3 echo $i
4 done
亠从仂仍从仂 仗亠亠仄亠仆仆
1 f o r ( ( a =1 , b =1; a <= LIMIT ; a ++ , b ++) )
2 do
3 echo n $a$b
4 done
Linux administration course. 9
16. 丶亳从仍 for. 仂亟 亳亰 亳从仍舒.
亠 于舒亢亠仆亳. 亠亰仍舒 仗仂 仄仂仍舒仆亳 1.
1 f o r ( ( i =1; ; i ++) )
2 do
3 echo $i
4 [ $ i eq 10 ] && break
5 done
Linux administration course. 10
18. 亳仄亠 亳仗仂仍亰仂于舒仆亳 while
亠亠弍亳舒亠仄 舒亞仄亠仆.
1 while [ [ n $1 ] ]
2 do
3 echo $1
4 s h i f t # rename the p o s i t i o n a l parameters
$N +1 , $N+2 . . . to $1 , $2 (N=1)
5 done
Linux administration course. 12
20. 亠亠仆舒仗舒于仍亠仆亳亠 亳亰 亳从仍舒.
舒亠.
1 while IFS= : read name pass uid guid comment
home shell ; do
2 echo $name $uid $guid $home $shell
3 done < / etc / passwd
仂仂仆仂亠 亠仆亳亠 亳亰 舒亶仍舒.
1 while IFS= read r line ; do
2 printf %s n $ l i n e
3 done < $ f i l e
Linux administration course. 14
21. 仆亠仆亳亠 从仂仄舒仆亟.
舒仂于亠 仂仗亠舒亳亳 舒亶仍舒仄亳 舒仆舒仍仂亞 亳从仍仂于
仂仄舒仆亟舒 find
1 find . name *. c exec stat { } ;
仂仄舒仆亟舒 xargs
1 echo / dev / std* | xargs n1 readlink
Linux administration course. 15
22. 丶亳从仍 until. 亳仄亠.
亢亳亟舒亠仄 仂 仗仂仍亠 仗亠亠亰舒亞亰从亳.
1 u n t i l ping q c 3 $host 1 >/ dev / null 2>&1 &&
nc z $host 22
2 do
3 sleep 1
4 echo unavailable ;
5 done
6
Linux administration course. 16
23. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 17
26. 亳仄亠 (仆舒舒仍仂)
1 # ! / bin / bash
2
3 function help {
4 echo 仗仂仍亰仂于舒仆亳亠 : $0 < string >
5 e x i t 1
6 }
7
8 f1 ( ) {
9 echo 亰于舒仆舒 仆从亳 $FUNCNAME $# 舒亞仄亠仆舒仄亳
10 }
11
12 f2 ( ) {
13 while read str ; do
14 echo $ { FUNCNAME } : 仗仂亳舒仆舒 仂从舒 : $str
15 done
16 }
Linux administration course. 19
27. 亳仄亠 (仂从仂仆舒仆亳亠)
1 [ $# eq 0 ] && help
2
3 f1 $@
4
5 { f o r ( ( i =0; i <5; i ++) ) ; do
6 echo $@
7 done } | f2
8
9 e x i t
Linux administration course. 20
28. 仂从舒仍仆亠 仗亠亠仄亠仆仆亠
亠亠仄亠仆仆亠 仂弍磦仍亠仆仆亠 仗亠亳从仂仄 local 于亳亟仆 仂仍从仂
于仆亳 仂弍磦仍亠仆仆仂亞仂 弍仍仂从舒.
1 # ! / bin / bash
2
3 VAR= t e s t
4 f1 ( ) {
5 l o c a l VAR=test1
6 echo Function $ {
FUNCNAME } : VAR=$VAR
7 }
8 f2 ( ) {
9 VAR=test2
10 echo Function $ {
FUNCNAME } : VAR=$VAR
11 }
1 echo Before f1 : VAR=$VAR
2 f1
3 echo Before f2 : VAR=$VAR
4 f2
5 echo After f2 : VAR=$VAR
6
7 e x i t
Linux administration course. 21
29. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 22
35. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 25
37. Command chain example
It only takes three commands to install Gentoo
Linux administration course. 26
38. Command chain example
It only takes three commands to install Gentoo
1 cfdisk / dev / hda && mkfs . xfs / dev / hda1 &&
mount / dev / hda1 / mnt / gentoo / && chroot / mnt
/ gentoo / && envupdate && . / etc / profile &&
emerge sync && cd / usr / portage && scripts /
bootsrap . sh && emerge system && emerge vim
&& vi / etc / fstab && emerge gentoodev
sources && cd / usr / src / linux && make
menuconfig && make install modules_install
&& emerge gnome mozillafirefox openoffice
&& emerge grub && cp / boot / grub / grub . conf .
sample / boot / grub / grub . conf && vi / boot /
grub / grub . conf && grub && init 6
Linux administration course. 26
39. Command chain example
It only takes three commands to install Gentoo
1 cfdisk / dev / hda && mkfs . xfs / dev / hda1 &&
mount / dev / hda1 / mnt / gentoo / && chroot / mnt
/ gentoo / && envupdate && . / etc / profile &&
emerge sync && cd / usr / portage && scripts /
bootsrap . sh && emerge system && emerge vim
&& vi / etc / fstab && emerge gentoodev
sources && cd / usr / src / linux && make
menuconfig && make install modules_install
&& emerge gnome mozillafirefox openoffice
&& emerge grub && cp / boot / grub / grub . conf .
sample / boot / grub / grub . conf && vi / boot /
grub / grub . conf && grub && init 6
That is the first one
Linux administration course. 26
40. 丕仍仂于仆亠 仂仗亠舒仂 && ||
仗仂仍仆亳 从仂仄舒仆亟:
1 cd video && echo Exit code $? Is OK . I am
watching video .
2 mkdir video # 亰舒仗亳 仗亠亟亟 从仂仄舒仆亟 亠亠
舒亰
Linux administration course. 27
41. 丕仍仂于仆亠 仂仗亠舒仂 && ||
仗仂仍仆亳 从仂仄舒仆亟:
1 cd video && echo Exit code $? Is OK . I am
watching video .
2 mkdir video # 亰舒仗亳 仗亠亟亟 从仂仄舒仆亟 亠亠
舒亰
弌亳仆舒从亳:
command1 && command2
O弍仂仍仂从舒 于仗仂仍仆磳 command2 亠仍亳 仗亠仆仂 于仗仂仍仆亳仍舒
command1. 仂亞亳亠从舒 仂仗亠舒亳 (AND)
Linux administration course. 27
42. 丕仍仂于仆亠 仂仗亠舒仂 && ||
仗仂仍仆亳 从仂仄舒仆亟:
1 cd video && echo Exit code $? Is OK . I am
watching video .
2 mkdir video # 亰舒仗亳 仗亠亟亟 从仂仄舒仆亟 亠亠
舒亰
弌亳仆舒从亳:
command1 && command2
O弍仂仍仂从舒 于仗仂仍仆磳 command2 亠仍亳 仗亠仆仂 于仗仂仍仆亳仍舒
command1. 仂亞亳亠从舒 仂仗亠舒亳 (AND)
仗仂仍仆亳 从仂仄舒仆亟:
Linux administration course. 27
43. 丕仍仂于仆亠 仂仗亠舒仂 && ||
仗仂仍仆亳 从仂仄舒仆亟:
1 cd video && echo Exit code $? Is OK . I am
watching video .
2 mkdir video # 亰舒仗亳 仗亠亟亟 从仂仄舒仆亟 亠亠
舒亰
弌亳仆舒从亳:
command1 && command2
O弍仂仍仂从舒 于仗仂仍仆磳 command2 亠仍亳 仗亠仆仂 于仗仂仍仆亳仍舒
command1. 仂亞亳亠从舒 仂仗亠舒亳 (AND)
仗仂仍仆亳 从仂仄舒仆亟:
1 cd video | | echo Exit code $ ? . Video is not
available
2 rmdir video # 丕亟舒仍亳仄 亳 仗仂于仂亳仄 亠亠 舒亰 仗亠于
从仂仄舒仆亟 .
Linux administration course. 27
44. 丕仍仂于仆亠 仂仗亠舒仂 && ||
仗仂仍仆亳 从仂仄舒仆亟:
1 cd video && echo Exit code $? Is OK . I am
watching video .
2 mkdir video # 亰舒仗亳 仗亠亟亟 从仂仄舒仆亟 亠亠
舒亰
弌亳仆舒从亳:
command1 && command2
O弍仂仍仂从舒 于仗仂仍仆磳 command2 亠仍亳 仗亠仆仂 于仗仂仍仆亳仍舒
command1. 仂亞亳亠从舒 仂仗亠舒亳 (AND)
仗仂仍仆亳 从仂仄舒仆亟:
1 cd video | | echo Exit code $ ? . Video is not
available
2 rmdir video # 丕亟舒仍亳仄 亳 仗仂于仂亳仄 亠亠 舒亰 仗亠于
从仂仄舒仆亟 .
Linux administration course. 27
45. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 28
46. 从仍ム亠仆亳亠 亠亢亳仄舒 仂仍舒亟从亳 I
仗仂仍亰亠 从仂仄舒仆亟舒 set 亟仍 舒从亳于亳亰舒亳亳 舒亰仍亳仆
亠亢亳仄仂于 舒弍仂 bash.
从仍ム亠仆亳亠 亠亢亳仄舒 仗仂亳亰于仂亟亳 仗仂仄仂 ,
舒 仂从仍ム亠仆亳亠 +.
Linux administration course. 28
47. 从仍ム亠仆亳亠 亠亢亳仄舒 仂仍舒亟从亳 II
亳仄亠 于从仍ム亠仆亳 亠亢亳仄舒 仂仍舒亟从亳.
1 bash x . / script . sh # 仂仗亳 从仂仄舒仆亟仆仂亶 仂从亳
2
3 # ! / bin / bash x # 于仆亳 从亳仗舒 仂仗亳亠亶
4 [ . . script . . ]
5
6 # ! / usr / bin / env bash
7 set x # 于仆亳 从亳仗舒 从仂仄舒仆亟仂亶
8
9 # ! / usr / bin / env bash
10 [ . . irrelevant code . . ]
11 set x # 于从仍ム舒亠仄 仂仍舒亟从 仆舒 舒 从仂亟舒
12 [ . . relevant code . . ]
13 set +x # 于从仍ム舒亠仄 仂仍舒亟从
14 [ . . irrelevant code . . ]
Linux administration course. 29
48. 仍ム亳 仂仍舒亟从亳 从仂仄舒仆亟 set I
set -v 于于仂亟 仆舒 从舒仆 亳仗仂仍仆磳仄仂亶 仂从亳
1 set v
2 echo $HOME
set -x 于于仂亟 仆舒 从舒仆 亳仗仂仍仆磳仄仂亶 仂从亳
舒于仂仄舒亳亠从仂亶 仗仂亟舒仆仂于从仂亶 亰仆舒亠仆亳亶 仗亠亠仄亠仆仆
1 set x
2 echo $HOME
set -n 仗仂于亠从舒 亳仆舒从亳舒 从亳仗舒
1 bash n t e s t . sh
Linux administration course. 30
49. 仍ム亳 仂仍舒亟从亳 从仂仄舒仆亟 set II
set -f 仂从仍ム亠仆亳亠 亞亠仆亠舒亳亳 亳仄亠仆 舒亶仍仂于
亳仗仂仍亰仂于舒仆亳亠仄 仄亠舒亳仄于仂仍仂于 (globbing)
1 echo ~ / . *
2 set f
3 echo ~ / . *
Linux administration course. 31
50. 仍ム亳 仂仍舒亟从亳 从仂仄舒仆亟 set III
set -e 仂舒仆仂于从舒 于仗仂仍仆亠仆亳 从亳仗舒, 亠仍亳 从舒从舒仍亳弍仂
从仂仄舒仆亟舒 于仂亰于舒磳 errorstatus 仆亠 舒于仆亶 0
1 # ! / bin / bash
2
3 set e
4 f a l s e | | true
5 echo Working
6 f a l s e
7 echo Still working
Linux administration course. 32
52. 弌亠从 于亰仂于仂于
caller [N] 仆从亳 于于仂亟亳 仆舒 从舒仆 仆仂仄亠 仂从亳,
亳仄亠仆舒 仆从亳亳 亳 舒亶仍舒 于亰于舒ム亠亞仂 从亳仗舒
1 # ! / bin / bash
2 backtrace ( ) {
3 echo Backtrace :
4 f o r ( ( i=$SHLVL ; i >=0; i)) ; do
5 c a l l e r $i
6 done
7 }
8 f1 ( ) { echo Function $FUNCNAME at $LINENO &&
backtrace ; }
9 f2 ( ) { f1 ; }
10 f2
Linux administration course. 34
53. 丐亳仗亳仆亠 仂亳弍从亳 于 仍仂于仆仂仄 于舒亢亠仆亳亳
舒仗亳舒 从亳仗 仗仆亳仄舒ム亳亶 仂亟亳仆 舒亞仄亠仆 亳
仂仗亠亟亠仍ム亳亶 舒亶仍 仍亳 仂. 仍亳 舒亶仍 于于仂亟亳 仆舒 从舒仆
仂从 regular file detected 于 仍舒亠 仗亠舒, 亠亟于舒亳亠仍仆仂
仂亰亟舒: 舒亶仍 c 亳仄亠仆舒仄亳 file, my file
check.sh 亳仗舒于亳
1 filename=$1
2
3 [f $filename ] && echo regular f i l e detected
| | echo not regular f i l e
4 e x i t # to run code below comment i t
5 # or v a r i a n t with i f
6 i f [f $filename ] ; then
7 echo re g ular f i l e detected
8 else
9 echo not regular f i l e
Linux administration course. 35
54. 弌仂仂弍亠仆亳 仂弍 仂亳弍从舒
亳仄亠.
1 check . sh : line 4 : [f : command not found
2
3 check . sh : line 4 : [ : missing ]
4
5 . / check . sh # i n c o r r e c t code l o g i c . No
arguments .
6 re g ular f i l e detected
7
8 . / check . sh : l i n e 5 : [ : my: binary operator
expected
9
10 . / check . sh : l i n e 112: syntax e r r o r : unexpected
end of f i l e
11
12 bash : t e s t : too many argumentsLinux administration course. 36
55. 1 Manipulate the different I/O streams with I/O Redirection
2 Repeat operations with loops.
3 Group pieces of code.
4 Expansions cont.
5 if alternatives.
6 Debugging scripts.
7 Command types.
Linux administration course. 37
57. 舒亟舒仆亳亠. 亳亟 从仂仄舒仆亟 于 仂弍仂仍仂从亠
仂仄舒仆亟舒 type 仂仂弍舒亢舒亠 亳仗 从仂仄舒仆亟. 仗仂仍仆亳仄 亠亠 亟仍
舒亰仍亳仆 从仂仄舒仆亟.
1 type type cd help a l i a s read
2 type dmesg rm
3 type i f
4 type a ls
5 type a echo pwd t e s t
于舒 亳仗舒 从仂仄舒仆亟. 仂仆仂 仆舒仄 仆舒亟仂? 仍亳 舒亰仆亳舒?
Linux administration course. 38
58. 舒亟舒仆亳亠. 亳亟 从仂仄舒仆亟 于 仂弍仂仍仂从亠
仂仄舒仆亟舒 type 仂仂弍舒亢舒亠 亳仗 从仂仄舒仆亟. 仗仂仍仆亳仄 亠亠 亟仍
舒亰仍亳仆 从仂仄舒仆亟.
1 type type cd help a l i a s read
2 type dmesg rm
3 type i f
4 type a ls
5 type a echo pwd t e s t
于舒 亳仗舒 从仂仄舒仆亟. 仂仆仂 仆舒仄 仆舒亟仂? 仍亳 舒亰仆亳舒?
仂亠仆仆亠 亳 于仆亠仆亳亠 从仂仄舒仆亟
于亠亞亟舒 仗亳于ム 于 亳仆亠仗亠舒仂亠, 于仆亠仆亳 仄仂亢亠 亳
仆亠 弍 仆舒 亟亳从亠
仂亟仆仂仂弍舒亰仆亶 亳仆舒从亳 仆舒 舒亰仆 仗仍舒仂仄舒
(仗亠亠仆仂亳仄仂 从亳仗仂于)
从舒从 仗舒于亳仍仂 于仗仂仍仆ム 弍亠亠, .从. 从仂亟 仆舒仂亟亳 于
仗舒仄亳
亠 亠亟于舒, 仂弍 于从仍ム亳 于仂亠仆仆亠 从仂仄舒仆亟,Linux administration course. 38
59. 丕仍仂于仆亠 仂仗亠舒仂: case
1 case $ v a r i a b l e
in
2 pattern1 )
command1
3
command2
4 ; ;
5 pattern2 |
pattern3 )
6
command3
7
command4
8 ; ;
9 esac
Linux administration course. 39
60. 丕仍仂于仆亠 仂仗亠舒仂: case
1 case $ v a r i a b l e
in
2 pattern1 )
command1
3
command2
4 ; ;
5 pattern2 |
pattern3 )
6
command3
7
command4
8 ; ;
9 esac
亳仄亠:
1 # ! / bin / bash
2
3 cmd=$1 ; var=$2
4 case $cmd in
5 print|echo )
6 echo 丕舒 , 仗亠舒舒亠仄 ! ; ;
7 abc *| xyz *)
8 echo 弌舒仆仆舒 从仂仄舒仆亟舒 $cmd
; ;
9 *)
10 echo 亊 舒从亳 从仂仄舒仆亟 仆亠
亰仆舒 : $cmd 1>&2
11 e x i t 1 ; ;
12 esac
13 [ ! z $var ] && echo
仗亠亠仄亠仆仆舒 $var = $var
14 e x i t 0Linux administration course. 39