-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell learn note.txt
More file actions
297 lines (250 loc) · 10.7 KB
/
shell learn note.txt
File metadata and controls
297 lines (250 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
set命令有时间去看看,还有echo
1.if ... elif ... else ... fi 语句
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi
2.case esac语句
case $aNum in
1) echo 'You select 1'
;;
2) echo 'You select 2'
;;
3) echo 'You select 3'
;;
4) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac
3.for
for 变量 in 列表
do
command1
command2
...
commandN
done
for ((i=1;i<=10;i++))
do
echo "num is $i"
done
4.while
while command
do
Statement(s) to be executed if command is true
done
死循环 : 冒号:内建空指令,返回值为0
while :; do
echo looping...
done
5.until循环:
until command
do
Statement(s) to be executed until command is true
done
6.自定义函数:
function_name () {
list of commands
[ return value ]
}
fuction_name () {
command1
....
}
7.执行结果赋值给变量
curdir=$(pwd)
curdir=`pw`
8.$(()) 做数学运算用
$(( a+b*c ))
`expr 4 \* 5`
$[ 4 * 5 ]
** 次方运算 2**3=8
((num=0xff)) <==> ((num=16#ff)
((num=32#ffff))
((num=64#abc_))
((num=2#11111111))
echo "obase=64;123456"|bc
echo "obase=10;ibase=2;10000" | bc
echo "10 + 1" | bc
echo "1.212*3" | bc
echo "scale=2;3/8" | bc
echo "10^10" | bc
echo "sqrt(100)" | bc
9.数组
(1) array=(var1 var2 var3 ... varN)
(2) array=([0]=var1 [1]=var2 [2]=var3 ... [n]=varN)
(3) array[0]=var1
arrya[1]=var2
...
array[n]=varN
echo ${array[2]}
删除数据元素
unset SEASON[2]
计算数组的长度:
${#array}
${#array[0]}
计算数组元素个数:
${#array[@]} 或者 ${#array[*]}
遍历数组:
filename=(`ls`)
for var in ${filename[@]};do
echo $var
done
数组的提取:
array=( [0]=one [1]=two [2]=three [3]=four )
${array[@]:1} # two three four,除掉第一个元素后所有元素,那么${array[@]:0}表示所有元素
${array[@]:0:2} # one two
${array[@]:1:2} # two three
数组元素替换
a[3]=100<=> ${a[@]/3/100}
10.shell的特殊变量
ignoreeof
用来禁止使用ctrl+d来退出shell
noclobber
noclobber变量可以在重定向输出时保护已存在的文件,防止被意外地覆盖。
noglob
设置noglob变量后,shell将不扩展文件名中一些特殊的字符或字符串
ls answer?输出结果为answer?
$ set –o noclobber // 使noclobber变量开
$ set +o noclobber // 使noclobber变量关
; : 在前一个命令结束时,而忽略其返回值,继续执行下一个命令。
&& :在前一个命令结束时,若返回值为 true,继续执行下一个命令。
|| :在前一个命令结束时,若返回值为 false,继续执行下一个命令。
!: 执行 history 列表中的命令 ex. !72
file=/dir1/dir2/dir3/my.file.txt (同样适用于数组)
${file#*/}: 拿掉第一条 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}: 拿掉最后一条 / 及其左边的字符串:my.file.txt
${file#*.}: 拿掉第一个 . 及其左边的字符串:file.txt
${file##*.}: 拿掉最后一个 . 及其左边的字符串:txt
${file%/*}: 拿掉最后条 / 及其右边的字符串:/dir1/dir2/dir3
${file%%/*}: 拿掉第一条 / 及其右边的字符串:(空值)
${file%.*}: 拿掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}: 拿掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
${array[@] /o/m}#第一个匹配到的,会被删除
${array[@] //o/m} #所有匹配到的,都会被删除
${array[@] //o/}#没有指定替换子串,则删除匹配到的子符
${array[@] /#o/k} #替换字符串前端子串 ex:one
${array[@] /%o/k} #替换字符串后端子串 ex:two
记忆的方法为:
# 是去掉左边(在鉴盘上 # 在 $ 之左边)
% 是去掉右边(在鉴盘上 % 在 $ 之右边)
单一符号是最小匹配﹔两个符号是最大匹配。
${file%?}:删除最后一个字符
${file:0:5}:提取最左边的 5 个字节:/dir1
${file:5:5}:提取第 5 个字节右边的连续 5 个字节:/dir2
${var} 变量本来的值
${var:-word} 如果变量 var 为空或已被删除(unset),那么返回 word,但不改变 var 的值。
${var-word} 如果变量 var 为空,那么返回 word,但不改变 var 的值。
有没有:的区别就是var是否被set过
${var:=word} 如果变量 var 为空或已被删除(unset),那么返回 word,并将 var 的值设置为 word。
${var:+word} 如果变量 var 被定义,那么返回 word,但不改变 var 的值。
${var:?message} 如果变量 var 为空或已被删除(unset),那么将消息 message 送到标准错误输出,可以用来检测变量 var 是否可以被正常赋值。若此替换出现在Shell脚本中,那么脚本将停止运行。
$0 当前脚本的文件名
$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。
$# 传递给脚本或函数的参数个数。
$* 传递给脚本或函数的所有参数。 ("a b c")
$@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,("a" "b" "c")
$? 上个命令的退出状态,或函数的返回值。
$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。
-eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 true。
-ne 检测两个数是否相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
-gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。
-lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。
-ge 检测左边的数是否大等于右边的,如果是,则返回 true。 [ $a -ge $b ] 返回 false。
-le 检测左边的数是否小于等于右边的,如果是,则返回 true。 [ $a -le $b ] 返回 true。
! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
-o 或运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a 与运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false。
= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
-n 检测字符串长度是否为0,不为0返回 true。 [ -n $a ] 返回 true。
str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
-p file 检测文件是否是具名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。
command >file 将输出重定向到 file。
command <file 将输入重定向到 file。
command >>file 将输出以追加的方式重定向到 file。
n > file 将文件描述符为 n 的文件重定向到 file。
n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。
n >& m 将输出文件 m 和 n 合并。
n <& m 将输入文件 m 和 n 合并。
<< tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。
ex:
/usr/bin/ftp -i -n << FTP
open 192.168.100.9
user test test
cd /DER/Firmware/Beta
mkdir $der_dir
cd $der_dir
mput der*
bye
FTP
11:
linux系统除了提供位置参数还提供内置参数,内置参数如下:
$# ----传递给程序的总的参数数目
$? ----上一个代码或者shell程序在shell中退出的情况,如果正常退出则返回0,反之为非0值。
$* ----传递给程序的所有参数组成的字符串。
$n ----表示第几个参数,$1 表示第一个参数,$2 表示第二个参数 ... $0 ----当前程序的名称
$@----以"参数1" "参数2" ... 形式保存所有参数
$$ ----本程序的(进程ID号)PID
$! ----上一个命令的PID
12:
expect:
spawn ssh root@10.1.50.225 "nohup sh /root/deploy.sh ///&"
set timeout 300
expect "root@10.1.50.225's password:"
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof
13:
反引号齐本身就对\进行了转义,保留了齐本身意思,如果我们想在反引号中起到\的特殊意义,我们必须使用2个\来进行表示。
所以我们可以简单的想象成反引号中: \\ = \
$()中则不需要考虑\的问题,与我们平常使用的一样:\ = \
题外话: 反引号是老的用法,$()是新的用法,不管是在学习测试中,还是在实际工作中,$()的用法都是被推荐的。
14:
#!/bin/bash
while echo "display current netconfig:"
do
select vi in "ifconfig -a" "hosts" "netmasks" "quit"
do
case $vi in
"ifconfig -a")
/sbin/ifconfig -a;;
"hosts")
more hosts;;
"netmasks")
more netmasks;;
"quit")
exit 0;;
*)
continue;;
esac
break
done
done
15:
echo $'\t\102\t'
输出tab+英文字母B+tab