shell脚本笔记(脚本)

关于作者

周李杰,最后一批90后,现居湖北武汉

孤独患者,懒癌中期,无脑幻想者。但却人畜无害,无需远离。

兴趣众多,然无一精通。正努力成为一个有趣的人。

1
2
> print("👆以上是不完整的我") //请我吃大餐将会了解更多
>
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
# ----------------猜数字-----------------
#!/bin/bash
num=50

read -p "请输入你所猜到的数字:" user_num
if [ $num -eq $user_num ];then
echo "哇瑟!好厉害!"
else
echo "你猜错了,哈哈哈"
fi


#---------------计算年龄-------------------
#!/bin/bash
age=100

read -p "请输入您今年的年龄:" user_age
echo $user_age |egrep '^[0-9]+$' &>/dev/null
if [ $? -ne 0 ];then
echo "您输入的有误,请重新执行并且输入1-100之前任意数值!"
else
if [ $user_age -lt $age ]; then
echo "您还可以活" $[ $age - $user_age ] "岁。"
else
echo "您输入的有误,请重新执行并且输入1-100之前任意数值!"
fi
fi


#--------输入三个数值,自动取出最大值与最小值----------
#!/bin/bash
for i in $(seq 3);do
read -p "请输入一个数字:" num
echo $num |egrep '^[0-9]+$' &>/dev/null
if [ $? -ne 0 ];then
echo "请输入一个整数"
exit
fi

if [ $i -eq 1 ];then
max=$num
min=$num
fi

if [ $num -gt $max ];then
max=$num
fi

if [ $num -lt $min ];then
min=$num
fi
done

echo "最大的数字为: "$max
echo "最小的数字为: "$min


#------成绩:90上优秀、80上良好、70上中等、60上及格、60下差-----------
#!/bin/bash

read -p "请输入您的成绩:" user_chengji
echo $user_chengji |egrep '^[0-9]+$' &>/dev/null
if [ $? -ne 0 ];then
echo "请输入一个整数"
exit
fi
if [ $user_chengji -gt 100 ]; then
echo "请输入0-100以内的数值"
exit
fi
if [ $user_chengji -ge 90 ]; then
echo "您的成绩为优秀"
elif [ $user_chengji -ge 80 ]; then
echo "您的成绩为良好"
elif [ $user_chengji -ge 70 ]; then
echo "您的成绩为中等"
elif [ $user_chengji -ge 60 ]; then
echo "您的成绩为及格"
elif [ $user_chengji -le 60 ]; then
echo "您的成绩为差"
fi


#--------case语法---------
#!/bin/bash
case $1 in
"start"|"Start"|"START")
/usr/local/httpd/apachectl start
;;
"stop"|"STOP")
/usr/local/httpd/apachectl stop
;;
"restart")
/usr/local/httpd/apachectl stop
/usr/local/httpd/apachectl start
;;
"status")
count=$(ps -ef | egrep -v "grep | $0" | grep -c httpd &> /dev/null)
if [ $count -ne 0 ];then
echo "httpd is running."
else
echo "httpd is stopped."
fi
;;
*)
echo "Usage:service httpd
start|stop|restart|status"
;;
esac



cp httpd /etc/init.d/
chmod +x /etc/init.d/httpd


#!/bin/bash
read -p "请输入你的分数:" user_num
echo $user_num | egrep '^[0-9]+$' &>/dev/null
if [ $? -ne 0 ]; then
echo "请正确地输入您的分数!"
exit
fi

case $user_num in
-gt100)
echo "请正确输入您的分数!"
;;
)
echo "您的成绩为优秀!"
;;
*)
;;
esac


#取出/etc/passwd的用户以及UID
#!/bin/bash
Fa=/etc/passwd
for (( i=1;i<=$(cat $Fa | wc -l);i++ ));do
user_name=$(awk -F ':' "NR==$i {print $ 1}" $Fa)
user_UID=$(awk -F ':' "NR==$i {print $ 3}" $Fa)

echo "hello!$user_name,you UID is $user_UID."
done
echo "一共有$(cat $Fa | wc -l)个用户."


#!/bin/bash
man=$1
min=$2
if [ $# -eq 2 ];then
echo "请输入两个或以上数字。"
exit
fi

for i in $* ; do
echo $i | egrep '^[0-9]+$' &> /dev/null
if [ $? -ne 0 ];then
echo "无效,请输入数字,特殊字符以及字母等均无效"
exit
fi
if [ $man -lt $i ];then
man=$i
fi
if [ $min -gt $i ];then
min=$i
fi
done

echo "$man"
echo "$min"


#!/bin/bash
for i in {0..100};do
echo $i
done


#!/bin/bash
man=$1
min=$2
[ $# -eq 2 ] && echo "请输入两个或以上的数字!" && exit
for i in $*;do
echo $i | egrep '^[0-9]+$' &> /dev/null
[ $? -ne 0 ] && echo "请输入数字,字母、特殊字符等均无效!" && exit
[ $man -lt $i ] && man=$i
[ $man -gt $i ] && min=$i
done
echo "$man"
echo "$min"


#!/bin/bash

for i in $(seq 100);do
if [ $[ $i % 2 ] -eq 1 ];then
echo $i
echo ------
fi
done


#!/bin/bash

for i in $(seq 1 2 100);do
echo $i
echo -------
done


#!/bin/bash

for i in $(seq 100);do
if [ $[ $i % 2 ] -ne 2 ];then
echo $i
echo ------
fi
done


#!/bin/bash

for i in $(seq 2 2 100);do
echo $i
echo ------
done


#!/bin/bash

for i in $(seq 1 100);do
if [ $i -lt 50 -o $i -gt 70 ];then
echo $i
echo -----
fi
done


#!/bin/bash

for i in $(seq 1 10);do
if [ $i -gt 5 ];then
echo $i
echo ------
fi
done

break [num]
中止循环
break语句后面的内容不会被执行

continue [num]
提前进入下一轮循环
continue语句后面不会被执行


#!/bin/bash

for i in $(seq 1 100);do
if [ $i -lt 50 -o $i -gt 70 ];then
echo $i
echo ----
fi
done

#!/bin/bash
#变量
name=tom
passwd=123321

#输入用户名以及密码

for (( i=1;i>=1;i++ ));do
read -p "请输入用户名:" user_name
read -p "请输入用户名:" user_passwd
if [ $i -gt 3 ];then
echo "您已三次输入有误,系统默认锁定账户,寻找管理员解锁!”
exit
fi
if [ $user_name != $name ] && [ $passwd != $user_passwd ];then
echo "您的用户名或者密码有误,请重试!"
else
echo "hello $user_name.欢迎来到这里!"
fi
done


# while循环
#!/bin/bash

i=1
while [ $i -le 10 ];do
echo $i
ehco -----
((i++))
done


#!/bin/bash

i=1
while [ $i -le 10 ];do
echo $i
echo ----
((i+=2))
done


#!/bin/bash
i=1

while [ $i -lt 10 ];do
if [ $[ $i % 2 ] -eq 0 ];then
echo $i
fi
((i++))
done


#!/bin/bash

i=0

while [ $i -lt 10 ];do
((i++))
if [ $i -lt 5 -o $i -gt 7 ];then
echo $i
echo ----
fi
done


#!/bin/bash

i=1
while [ $i -lt 10 ];do
((i++))
if [ $i -ge 5 ];then
echo $i
echo ----
fi
done


#!/bin/bash
i=1
while :;do
echo $i
echo -----
((i++))
done


#!/bin/bash

while read line;do
echo $line
echo -------------------------
done < /etc/passwd


#! /bin/bash
Fa=/etc/passwd

while read line;do
user_name=$(echo $line | awk -F ':' 'NR==1 { print $1 }')
user_UID=$(echo $line | awk -F ':' 'NR==1 { print $3 }')
echo “hello! $user_name 您的UID是 $user_UID
echo -------------------------
done < $Fa


#!/bin/bash

i=0
num=$[$RANDOM %100]

while :;do
((i++))
read -p "请输入一个数字" user_num
echo $user_num | egrep '^[0-9]+$' &> /dev/null
if [ $? -ne 0 ];then
echo "您输入的不是数字!请重试!"
exit
fi
if [ $user_num -gt $num ];then
echo "您输入的数字大了!"
elif [ $user_num -lt $num ];then
echo "您输入的数字小了!"
else
ehco "您输入正确!"
echo "您一共回答了$i次."
exit
fi
done

#!/bin/bash
flag=ture
i=3

while $flag;do
if [ $i -le 50 ];then
echo $i
fi
else
flag=false
let i++
done



#用for循环写个九九乘法表

#!/bin/bash

for i in $(seq 9);do
for j in $(seq $i);do
echo -en "${j}x${i}=$[$i * $j]\t"
done
echo
done


until循环猜数字随机


#!/bg=false
i=0
num=$[$RANDOM % 100]

until $flag;do
read -p "请输入所猜的数字:" user_num
((i++))
echo $user_num | egrep '^[0-9]+$' &>/dev/null
if [ $? -ne 0 ];then
echo "您输入的不是数字!"
break
fi
if [ $user_num -gt $num ];then
echo "您输入的数字大了!"
elif [ $user_num -lt $num ];then
echo "您输入的数字小了!"
else
echo "猜对了!"
echo "您一共回答了${i}次!"
flag=true
continue
fi
done
打赏
  • © 2019-2020 Li Jie
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

喜欢就打赏吧~

支付宝
微信