0%

Shell编程学习笔记

2020年最后一天,学习了Shell编程,希望对以后的工作能有所帮助。

以下是学习笔记,写在一个shell脚本中,经过测试可以运行,大家可以将它作为一个学习资料,相信如果能静下心来看完,自然也能学会shell编程,如果能逐句敲出来更能加深理解,记忆更加深刻。

练习使用的操作系统版本是:CentOS Linux release 7.5.1804 (core)

Hello

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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/bin/bash

echo "Hello World !"

# 变量
your_name="Frank"
echo "My name is $your_name"
echo ${your_name}

echo -e "必须加-e参数才能使得换行符有效\n"

echo -e "VIM显示行号:set nu\n"

readonly Myurl="https://tacgib.club"
echo "只读变量可以在定义变量的时候在变量名前加readonly,也可以在定义好之后对变量名重新定义(readonly Myurl,注意这里变量名前没有$)"
echo $Myurl

unset your_name
echo -e "\n unset可以删除变量,但不能删除只读变量\n"



## shell字符串
# 单引号里不可以使用变量,使用转义符也不行,但是可以使用成对的单引号用于字符串拼接
str='this is a string ',Hello!''
echo $str

# 双引号里可以包含单引号,可以使用变量
n=1000
str_double="这句话值${n}元:'${str}'"
echo $str_double

## 拼接字符串
your_name="Frank"
greeting="hello,$your_name"
greeting="Hello,${your_name}"
echo $greeting $greeting_1

## 获取字符串长度
string="abcdefg"
echo ${#string}

## 提取子字符串
string="Frank is cool"
echo ${string:0:5} # 输出Frank

## 查找子字符串(返回第一个o的位置)
string="Frank is cool"
echo `expr index "$string" o` # 反引号

# Shell数组
array_name=(1 2 3 4 5 6 7 8 9 10 "Frank")
array_2=(11
12
13
"May")
array_3[0]=100
array_3[1]="yuan"

echo ${array_name[@]} # 所有元素
echo ${array_2[3]} # 第4个元素
echo ${array_3[@]}


# 通过#号进行单行注释
# 通过EOF进行多行注释
:<<EOF
echo "这里注释掉了"
EOF

# Shell传递参数(第0个为正在执行脚本的文件名)
echo "Shell传递参数实践:"
echo "执行的文件名:$0" # 执行的文件名
echo "第1个参数为:$1" # 第一个参数
echo "第2个参数为:$2" # 第二个参数
echo "第3个参数为:$3" # 第三个参数
echo "参数个数为:$#" # 参数个数
echo "单行显示参数:$*" # 单行显示所有参数
echo "脚本执行的进程ID:$$" # 脚本执行的进程ID
echo "字符形式显示所有参数:$@" # 带引号显示所有参数,详见下面的详细演示
echo "Shell使用的当前选项:$-" # Shell使用的当前选项
echo "显示最后命令退出的状态,0表示无错误:$?" # Shell退出的状态

# 双引号中会看出区别,"$*"输出的是一个值,“$@”是多个
echo "-- \$*演示 ---"
for i in "$*"; do
echo $i
done

echo "-- \$@演示 ---"
for i in "$@"; do
echo $i
done

# 通常情况下是一样的
for i in $*; do
echo $i
done

for i in $@; do
echo $i
done

# 数组,数组定义上面已经演示过了
my_array=(A B "C" D)
echo "获取my_array元素(*):${my_array[*]}"
echo "获取my_array元素(@):${my_array[@]}"

echo "获取my_array数组长度:${#my_array[*]}"
echo "获取my_array数组长度:${#my_array[@]}"

# 运算符
echo -e "\n---开始练习运算符啦---\n"
echo "算数运算符:"
val=`expr 2 + 2` # 运算符与操作数之间必须要有空格,否则会当成字符串
echo "两数之和:$val"


echo "a = 10;b = 20"
a=10
b=20

val=`expr $a + $b`
echo "a + b = $val"

val=`expr $a - $b`
echo "a - b = $val"

val=`expr $a \* $b` # *必须加转义字符才可执行
echo "a * b = $val"

val=`expr $a / $b`
echo "a / b = $val"

val=`expr $a % $b`
echo "a % b = $val"

if [ $a == $b ] # 必须按此格式书写,方括号与运算符与值之间必须有空格
then
echo "a = b"
fi
if [ $a != $b ]
then
echo "a != b"
fi

# 关系运算符
echo -e "\n关系运算符:"
a=10
b=20
echo "a = $a , b = $b"

if [ $a -eq $b ];then
echo "a == b"
else
echo "a != b"
fi

if [ $a -ne $b ];then
echo "a != b"
else
echo "a == b"
fi

if [ $a -gt $b ];then
echo "a 大于 b"
else
echo "a 不大于 b"
fi

if [ $a -lt $b ];then
echo "a 小于 b"
else
echo "a 不小于 b"
fi

if [ $a -ge $b ];then
echo "a 大于等于 b"
else
echo "a 不大于等于 b"
fi

if [ $a -le $b ];then
echo "a 小于等于 b"
else
echo "a 不小于等于 b"
fi


# 布尔运算
echo -e "\n布尔运算:"
a=10; b=20
echo "a = $a , b = $b"

# 特别注意,!运算符后面只能跟表达式,跟逻辑变量无效,将表达式的值赋值给变量也不行
if [ ! $a -ne $b ];then
echo "非假即真"
else
echo "非真即假"
fi

if [ ! $a -eq $b ];then
echo "非假既真"
else
echo "非真既假"
fi

if [ $a -eq $b -a $a -ne $b ];then
echo “与逻辑,全真则真”
else
echo "与逻辑,有假则假"
fi

if [ $a -ne $b -a $a -eq 10 ];then
echo "与逻辑,全真则真"
else
echo "与逻辑,有假则假"
fi

if [ $a -eq $b -o $a -ne $b ];then
echo "或逻辑,有真则真"
else
echo "或逻辑,全假则假"
fi

if [ $a -eq $b -o $a -ne $a ];then
echo "或逻辑,有真则真"
else
echo "或逻辑,全假则假"
fi


# 逻辑运算符(没发现跟布尔运算符的与、或有何区别)
# 格式请注意,必须使用
a=10; b=20
echo -e "\n逻辑运算符:\na = $a , b = $b"

if [[ $a -eq $b && $a -ne $b ]];then
echo "逻辑与,全真则真"
else
echo "逻辑与,有假则假"
fi

if [[ $a -eq $a && $a -ne $b ]];then
echo "逻辑与,全真则真"
else
echo "逻辑与,有假则假"
fi

if [[ $a -eq $b || $a -ne $b ]];then
echo "逻辑或,有真则真"
else
echo "逻辑或,全假则假"
fi

if [[ $a -eq $b || $a -ne $a ]];then
echo "逻辑或,有真则真"
else
echo "逻辑或,全假则假"
fi

# 字符串运算符
echo -e "\n字符串运算符:"
a="abcd"; b="efgh"

if [ $a = $b ];then
echo " a = b,均为:$a "
else
echo " a不等于b,a为:$a,b为:$b"
fi

if [ $a = "abcd" ];then
echo " a等于 ”abcd“ "
else
echo " a不等于"abcd",a为:$a"
fi

if [ $a != $b ];then
echo "a不等于b,a为;$a,b为:$b"
else
echo "a等于b,均为:$a"
fi

if [ -z $a ];then
echo "$a,字符串长度为0"
else
echo "$a,字符串长度不为0"
fi

if [ -z "" ];then
echo " \"\",字符串长度为0"
else
echo " \"\",字符串长度不为0"
fi

if [ -n "$a" ];then
echo "$a,字符串长度不为0"
else
echo "$a,字符串长度为0"
fi

if [ $a ];then
echo "$a,字符串不为空"
else
echo "$a,字符串不为空"
fi

# 文件测试运算符
:<<EOF
-b file 文件是否为块设备文件
-c file 文件是否为字符设备文件
-d file 文件是否为目录
-f file 文件是否为普通文件
-g file 文件是否设置了SGID位
-k file 文件是否设置了粘着位(Sticky Bit)
-p file 文件是否有名管道
-u file 文件是否设置了SUID位
-r file 文件是否可读
-w file 文件是否可写
-x file 文件是否可执行
-s file 文件是否为空
-e file 文件/目录是否存在
-S file 文件是否为Socket
-L file 文件是否存在并且是一个Link
EOF

TestPwd="TestFileMod"
# 注意:如果是相对目录引用,目录名前可以加“./”,也可以也可以什么都不加,但是不能只加"/"
# 注意:如果是在root用户下执行脚本,则不论文件权限为何,均具有可读可写可执行权限

file_readonly="${TestPwd}/readonly"
file_writeonly="${TestPwd}/writeonly"
file_exeonly="${TestPwd}/exeonly"

echo $file_readonly
echo $file_writeonly
echo $file_exeonly

if [ -d $TestPwd ];then
echo "The $TestPwd is pwd"
else
echo "The $TestPwd is not pwd"
fi

if [ -d $file_readonly ];then
echo "The $file_readonly is pwd"
else
echo "The $file_readonly is not pwd"
fi

if [ -r $file_readonly ];then
echo "The $file_readonly is readable"
else
echo "The $file_readonly is not readable"
fi

if [ -r $file_writeonly ];then
echo "The $file_writeonly is readable"
else
echo "The $file_writeonly is not readable"
fi

if [ -w $file_writeonly ];then
echo "The $file_writeonly is writable"
else
echo "The $file_writeonly is not writable"
fi

if [ -w $file_readonly ];then
echo "The $file_readonly is writable"
else
echo "The $file_readonly is not writable"
fi

if [ -x $file_exeonly ];then
echo "The $file_exeonly is executable"
else
echo "The $file_exeonly is not executable"
fi

if [ -x $file_readonly ];then
echo "The $file_readonly is executable"
else
echo "The $file_readonly is not executable"
fi


if [ -x $file_writeonly ];then
echo "The $file_writeonly is excutable"
else
echo "The $file_writeonly is not excutable"
fi

if [ -e $file_readonly ];then
echo "The $file_readonly is exist"
else
echo "The $file_readonly is not exist"
fi

if [ -e $file_writeonly ];then
echo "The $file_writeonly is exist"
else
echo "The $file_writeonly is not exist"
fi

if [ -e $file_exeonly ];then
echo "The $file_exeonly is exist"
else
echo "The $file_exeonly is not exist"
fi

# echo命令
echo "echo命令"
echo -e "\necho用了几百遍了,已经是老朋友,但是还是安静地学习一遍,以免漏掉什么\n"

echo "显示普通字符串"
echo 没有引号同样有效 中间留有空格也可以

echo "\"试试转义字符\""
echo \"试试转义字符,不带引号也可以\"

# 显示变量,read命令可以从标准输入中读取一行,并把输入行的值指定给shell变量,一个变量一行
echo -e "请输入名字:\c"
read name;
echo -e "请输入年龄:\c"
read age;
echo -e "请输入性别:\c"
read sex
echo name = $name, age = $age, sex = $sex

echo -e "-e是开启转义的意思\n"
echo -e "\\\n是换行的意思" # 在字符串中显示\n需要用三个\

echo -e "\\\c参数可以不换行,常用于需要从标准输入获取参数的地方,比如,请输入一个整数:\c"
read n

echo -e "这也可以显示为\c"
echo "一行"

echo '用单引号可以原样输出字符串,不会进行转义和取值:$name\"'

echo -e "echo也可以直接显示命令执行结果,如使用date显示时间:\c"
echo `date`
echo `expr 1 + 123 `


# printf命令
printf "开始练习printf命令\n"
# 要手动添加换行符,默认不换行
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg
# 跟C语言一样,%s表示输出字符串,前面的数字是字符串宽度,-表示左对齐,没有则表示右对齐,不足位数空格补齐,超过全部显示,另外还有%c、%d、%f

printf "%d %s\n" 1 "输出整数,双引号格式"
# 建议用双引号,兼容性最好
printf '%d %s \n' 2 "输出小数,单引号格式"
# 单引号时,转义字符要跟前面的格式符隔开
printf %s 不用引号也可以,但是只能用一个格式符,转义字符也不能使用

printf "\n%-4.2f\n" 4.12345

:<<EOF
\a 警告字符,通常为ASCII的BEL字符
\b 后退
\c 抑制(不显示)输出结果中任何结尾的换行字符(只在%b格式指示符控制下的参数字符串中有效),而且,任何留在参数里的字符、任何接下来的参数以及任何留在格式字符串中的字符,都被忽略
\f 换页(formfeed)
\n 换行
\r 回车(Carriage return)
\t 水平制表符
\v 垂直制表符
\\ 一个字面上的反斜杠字符
\ddd 表示1到3位数八进制值的字符。仅在格式字符串中有效
\0ddd 表示1到3位的八进制值字符

EOF

# 流程控制
printf "\n开始练习流程控制:\n"
# if
if [ $(ps -ef | grep -c "ssh") -gt 1 ];then echo "true";fi

# fi else-if else
a=1; b=20
if [ $a == $b ];then
# == 同 -eq
echo "a == b"
elif [ $a -gt $b ];then
echo "a 大于 b"
elif [ $a -lt $b ];then
echo "a 小于 b"
else
echo "没有符合的条件"
fi

# for
for i in 1 2 3 4 5; do echo "序号:$i"; done

for i in This is a string
do
echo "$i"
done

# while
# 使用了let命令,可以不用$
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done

:<<EOF
echo "无限循环,输入Ctrl+D退出"
echo -n "输入你最喜欢的网站名:"
while read FILM
do
echo "是的!$FILM 是个好网站"
done
EOF

# while true; do; echo "";done 或者 for (( ; ; ))都可以表示无限循环

# until循环
# until循环执行一系列命令直到条件为true时停止
a=0
until [ ! $a -lt 10 ];do
echo "until $a"
let "a++"
done

# case
:<<EOF
取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

EOF

echo "请选择A、B、C、D:"
echo -e "您输入选项为: \c"
read xuanxiang
case $xuanxiang in
"A") echo "A选项"
;;
"B") echo "B选项"
;;
"C") echo "C选项"
;;
"D") echo "D选项"
;;
*) echo "输入有误!"
esac

# 挑出循环
# breakcontinue,跟C语言类似

while :
do
echo -n "输入1至5:"
read n
case $n in
1 | 2 | 3 | 4 | 5 ) echo "输入了$n"
;;
*) echo "输入错误!"
break
;;
esac
done
# 如果break换成continue则当输入不为1至5时仅显示“输入错误”而不会退出循环

# Shell函数
fun(){
echo "乘法运算"
echo -e "请输入第一个参数:\c"
read a
echo -e "请输入第二个参数:\c"
read b
echo -e "您输入了:$a、$b"
return $(( $a * $b ))
}

fun
echo "函数计算结果为:$? !"
# 函数返回值在调用该函数后通过 $? 来获得
# 特别注意:函数试用期必须经过定义

# 函数传参
fun(){
echo "1:$1"
echo "2:$2"
echo "3:$3"
# 第10个参数需要写成:${10}
echo "参数总数:$#"
echo "所有参数:$*"
}
fun 100 200 300 400 500 600 700

:<<EOF
$# 传递到脚本或函数的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
EOF

# 输入输出重定向
:<<EOF
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 之间的内容作为输入。
需要注意的是文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)
EOF

# 输出重定向
# command > file,重写
# command >> file,追加

who > user1
echo "你好!" >> user1

# 输入重定向
# command < file,从文件中读取内容

wc -l users > users
wc -l < users


# 文件包含
# . ./XXX.sh
# source ./XXX.sh
# 被包含的sh文件不需要可执行权限

秋风木叶
2020-12-31
有您的赞赏,我会更加有动力!