. 1 the Linux Programming the shell
2 Contents
3 What is. 1 Shell
. 4 implementation Shell scripts 1
. 5 first: Absolute or relative path to enter the script Path 1
. 6 second: bash or sh + script. 1
. 7 Shell variables 2
. 8 variable 2 is defined
. 9 returns the value of the command to the variable 2
10 Shell the special variable 3
. 11 $ * $ @ and distinction. 3
12 is the operator. 3
13 is for loop. 4
14 the while loop. 5
15 Case statement. 5
16 Read command. 6
. 17 IF Analyzing. 6
18 is IF example 7
. 19 8 determines statements
20 common determination condition. 8
21 is Shell custom function 9
22 script debugging 9
23 sed command 10
24- sed options 10
25 sed function 10
26 awk command 11
27 awk command Format 11
28
29
30 what is Shell
31 • Shell is a user interface to interact with the kernel, the most popular shell shell called the bash
32 • shell is a programming language < interpreted programming language > , i.e., a shell script
33 is • a system there may be a plurality of shell, can etc / shells installed in the system shell command to view by cat /, different the shell may command syntax is not the same support
34 implementation shell scripts
35 first: absolute path or a relative path of the script input
36 first gives permission + x
37 [ /root/helloWorld.sh
38 is ./helloWorld.sh
39Alternatively, without imparting + x permissions, and interpreted by the interpreter
40 SH helloworld.sh
41 is a second: bash or sh + script
42 is SH /root/helloWorld.sh
43 is SH helloWorld.sh
44 is Shell variables
45 • the Linux Shell variables into the "system variables" and "user-defined variables" system variables that can be viewed through the set command
46 • system variables: $ HOME, $ PWD, $ SHELL, $ uSER , etc.
47 displays all the current shell variables: SET
48 define variables
49 • variable = value (= ABC e.g. the STR)
50 • no space on both sides of the equal sign
51 is • generally customary variable name to upper case
52 is • double and single quotation marks are different, only the double quotes off space intended single quotes will release all the special characters intended
53 is • the STR = "Hello World"
54 is • A. 9 =
55 • A revocation of the unset variables A
56 is• readonly B = 2 declare a static variable B = 2, can not unset
57 • Export variable names to variables to a global environment variables, available to other shell program uses
58 to return value to the variable commands
59 • A = `LS -la` backticks, running inside the command, and the results returned to the variable a
60 • a = $ (LS -la) is equivalent to backticks
61 Shell in the special variable
62 • $? represents a command to exit the state
63 • $$ indicates the current process number
64- • $ 0 represents the current name of the script
65 • $ n represents the position of the input parameters n (n represents a number, n> = 1)
66 • $ # is the number of parameters, commonly used in cycle
67 • $ * and $ @ have said the argument list 68 $ * and $ @ difference
69 • $ * and $ @ have said all of the parameters passed to a function or script, not double quotes "" when included, are the $ 1 $ 2 ... $ n All parameters in the form of output
70• when they are in double quotes, "" included, "$ *" resets all of the parameters as a whole, the "" output in the form of all parameters; "$ 1 $ 2 ... $ n" separated will each parameter to "$ @ in the form of $ 1 "" $ 2 "... " $ n " output of all parameters
71 operators
72 L format: expr m + n or $ ((m + n)) Note between expr operator include a space
73 is L, such as computing (2 +3 ) × 4 value of
74 1. the step calculates
75 S 2 + expr = `3`
76 expr $ S \. 4 *
77 2.
Complete the calculation step 78 expr expr 2 + ``. 3 \. 4 *
79 echo expr `\` expr. 3 + 2 \ `\ * 4`
80 or
81 $ (((. 3 + 2) *. 4))
82 for loop
83 • The first:
84 for N 2. 3. 1 in
85 do
86 echo $ N
87 DONE
88 或
89 for N in 1 2 3; do echo $N; done
90 或
91 for N in {1..3}; do echo $N; done
92 •第二种:
93 for ((i = 0; i <= 5; i++))
94 do
95 echo "welcome $i times"
96 done
97 或
98 for ((i = 0; i <= 5; i++)); do echo "welcome $i times"; done
99 while循环
100 •第一种
101 while expression
102 do
103 command
104 …
105 done
106• The second
107 I. 1 =
108 the while ((I < =. 3))
109 do
110 echo $ I
111 the let I ++
112 DONE
113 Case statement
114 • format
115 Case. 1 in $
1 16 Start)
117 echo "Starting"
1 18 ;;
119 STOP)
120 echo "STOPPING"
121 ;;
122 *)
123 echo "the Usage: Start {|} STOP"
124 Esac
125
126 Read command
127lread -p (precautionary statements) -n (number of characters) -t (latency) 128 Read -p "Please INPUT your name:" NAME
129 Example of use:
130.
131 is IF Analyzing
132 • syntax
133 IF for condition Condition 134 the then 135 statements 136 [elif for condition Condition 137 . .. the then statements] 138 [the else 139 statements] 140 Fi
141 is IF examples
142 ! # / bin / the bash
143 Read -p "Please INPUT your name:" nAME
144 #printf '% S \ n-' NAME $
145 IF [$ NAME = the root]
146 the then
147 echo "Hello $ {NAME}, available for purchase!"
148 elif [$ NAME = itcast]
149 the then
150 echo "Hello $ {NAME}, available for purchase!"
151 the else
152 echo "SB, GET OUT here Wallpaper!"
153 Fi
154 judge sentences
155 • [condition] (note that there must be a space before and after the condition)
156 # non-empty returns true, use $? verification (0 is true, > 1 is false)
157 [itcast]
158 # null return false
159 []
160 L [for condition Condition] && echo echo || NotOk the OK
161 Condition is detected, the statement following the
162 common condition determination
163 = string comparison
164 is -LT-less than
165 -le equal to less than
166 -eq equal to
167 is -gt greater than
168 -ge than or equal to
169 -ne not equal to
170. -R & lt have read permissions
171 -w have the authority to write
172 -x have permission to execute
173 -f file exists and is a regular file
174 -s file exists and is not empty
175 -d file exists and is a directory
176 -b file exists and is a block device
177 -L file exists and is a link
178 Shell-defined function
179 L syntax
180 [ [function] funname [()]
181 {
182 Action;
183 [return int;]
184 }
185 function Start () / function Start / Start ()
186 • Note
187 1. Must be in place before calling the function, declare the function, shell script is run line by line.
Not like other languages, like the first pre-compiled
188 2. The function returns a value, can only be obtained through the $ system variable, you can display plus:? Return return
If not, the result will be the last command run, as the return value. followed by the return value of n-(0-255)
189 script debugger
190 • SH -vx helloWorld.sh
191 • set in a script or increase the -X-
192 Sed command
193 • Sed name is: Stream EDitor stream editor i.e., a good text processing tools,
Command is a pipeline itself, processing, storing the row currently being processed in a temporary buffer,
Followed by treatment with the contents of the buffer sed command, the processing is completed, the contents of the buffer sent to the screen.
Then the next line. It is a unit of processing, data lines can be replaced, delete, add, and other select specific job.
194 sed options
195 • -n: Use quiet (silent) mode. In general usage of sed,
所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,
则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
196 •-e :直接在命令列模式上进行 sed 的动作编辑;
197 •-i :直接修改读取的文件内容,而不是输出到终端。
198 sed function
199 •a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)
200 •d :删除,因为是删除啊,所以 d 后面通常不接任何内容
201 •i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行)
202 •p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行
203 •s :取代,可以直接进行取代的工作!通常这个 s 的动作可以搭配正规表示法!
例如 1,20s/old/new/g
204 awk命令
205 •AWK是一种优良的文本处理工具。其名称得自于
它的创始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan
姓氏的首个字母,AWK 提供了极其强大的功能:可以进行样式装入、流控制、
数学运算符、进程控制语句甚至于内置的变量和函数。
它具备了一个完整的语言所应具有的几乎所有精美特性。
实际上 AWK 的确拥有自己的语言:AWK 程序设计语言,
三位创建者已将它正式定义为“样式扫描和处理语言”。
它允许您创建简短的程序,这些程序读取输入文件、为数据排序、处理数据、
对输入执行计算以及生成报表,还有无数其他的功能。
206 awk命令格式
207 awk 'pattern1 {action1} pattern2 {action2} ...' filename