Shell编程进阶篇(完结)
1.1 for循环语句
<p>
<span style="font-size: 12px;"> 它跟其他的循环,如while循环,最大的不同,是它拥有一个循环计数器,或是循环变数。这使得for循环能够知道在迭代过程中的执行顺序。</span>
</p>
<h3>
<span id="111_shellfor">1.1.1 shell中的for循环</span>
</h3>
<p>
<span style="font-size: 12px;"> shell中的for 循环与在c中不同,它包含三种形式:第一种结构是列表for 循环;第二种结构就是不带列表的for循环;第三种就类似于C语言。</span>
</p>
<p>
<strong>① </strong><strong>列表for</strong><strong>循环(</strong><em>常用</em><strong>)</strong>
</p>
<div class="cnblogs_code">
<pre>#!/bin/<span style="color: #000000;">bash
for i in 取值列表 do 循环主体/命令 done
<p>
</p>
<p>
<strong>② </strong><strong>不带列表for</strong><strong>循环(</strong><strong>示例)</strong>
</p>
<div>
<div class="cnblogs_code">
<pre>#!/bin/<span style="color: #000000;">absh
echo "惨绿少年的博客是:"
for i
do
echo "$i"
done
<p class="ad">
脚本执行结果
</p>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">for</span>]# <span style="color: #0000ff;">sh</span> for2.<span style="color: #0000ff;">sh</span> http:<span style="color: #008000;">//</span><span style="color: #008000;">blog.znix.top</span>
惨绿少年的博客是: http://blog.znix.top
<p>
</p>
</div>
</td>
<td style="text-align: left;" valign="top" width="47%">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212151446347-1775013299.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</td>
③ 类似C****语言的风格(_这种用法常在__C__语语言中使_用)
编写类似C__语言风格脚本
脚本执行过程
1.1.2 不同语言的For循环
Shell中的两种样式
** PHP**
** VB**
’循环语句 Next i
//5050 for _ in 1…100{ x += 1 } print(x) // 100 var box = [1,2,3,4,5] for i in box{ print(i) } / 1 2 3 4 5 / —
1.2 for循环相关练习题
1.2.1 【练习题1】批量生成随机字符文件名案例
使用for循环在/clsn目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串clsn,名称示例如下:
脚本内容
<div id="cnblogs_code_open_bf2aa7b7-51ee-41ec-9a51-edc228388a0c" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn <span style="color: #0000ff;">for</span>]# <span style="color: #0000ff;">cat</span> make_file.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: make_file.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-08 11:01:19
9 # Description:
10 #############################################################
11
12 [ -d /clsn ] || mkdir -p /clsn
13 rpm -qa |grep pwgen &>/dev/null
14 if [ $? -eq 1 ]
15 then
16 yum install pwgen -y &>/dev/null
17 fi
18
19 cd /clsn &&
20 for i in {1..10}
21 do
22 #File_Name=uuidgen |<span style="color: #0000ff;">tr</span> <span style="color: #800000;">"</span><span style="color: #800000;">0-9-</span><span style="color: #800000;">"</span> <span style="color: #800000;">"</span><span style="color: #800000;">a-z</span><span style="color: #800000;">"</span>|<span style="color: #0000ff;">cut</span> -c <span style="color: #800080;">1</span>-<span style="color: #800080;">10</span><span style="color: #000000;">
23 File_Name2=pwgen -1A0 <span style="color: #800080;">10</span><span style="color: #000000;">
24 touch ${File_Name2}_clsn.html
25 done
<p>
<span class="cnblogs_code_collapse">View Code 批量生成随机字符文件名</span></div> </div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">for</span>]# <span style="color: #0000ff;">ls</span> -l /<span style="color: #000000;">clsn
-rw-r–r– 1 root root 12月 8 19:41 aegheiriek_clsn.html -rw-r–r– 1 root root 12月 8 19:41 aifohxique_clsn.html -rw-r–r– 1 root root 12月 8 19:41 caexahween_clsn.html -rw-r–r– 1 root root 12月 8 19:41 ciefaivaib_clsn.html -rw-r–r– 1 root root 12月 8 19:41 eixongooph_clsn.html -rw-r–r– 1 root root 12月 8 19:41 foozaivedo_clsn.html -rw-r–r– 1 root root 12月 8 19:41 ireeteethu_clsn.html -rw-r–r– 1 root root 12月 8 19:41 ohmeebivae_clsn.html -rw-r–r– 1 root root 12月 8 19:41 oiceehahth_clsn.html -rw-r–r– 1 root root 12月 8 19:41 sheewaehoo_clsn.html
<h3>
<span id="122_2">1.2.2 【练习题2】批量改名特殊案例</span>
</h3>
<p>
【练习题1】中结果文件名中的clsn字符串全部改成znix(最好用for循环实现),并且将扩展名html全部改成大写。
</p>
<div>
<div class="cnblogs_code">
<pre>jpvirsnjld_clsn.html ===> jpvirsnjld_znix.HTML</pre>
</div>
</div>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('0949881c-dd1b-4522-927e-0745e2596b22')">
<img id="code_img_closed_0949881c-dd1b-4522-927e-0745e2596b22" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_0949881c-dd1b-4522-927e-0745e2596b22" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('0949881c-dd1b-4522-927e-0745e2596b22',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_0949881c-dd1b-4522-927e-0745e2596b22" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn for2]# <span style="color: #0000ff;">cat</span> rename_file.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: rename_file.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-08 11:31:56
9 # Description:
10 #############################################################
11
12 cd /clsn &&
13 File_name=<span style="color: #0000ff;">ls</span> |<span style="color: #0000ff;">sed</span> -r <span style="color: #800000;">'</span><span style="color: #800000;">s#(.*)_clsn.html#\1#g</span><span style="color: #800000;">'</span><span style="color: #000000;">
14
15 for i in $File_name
16 do
17 if [ -f ${i}_clsn.html ]
18 then
19 mv ${i}_clsn.html ${i}_znix.HTML
20 else
21 echo "文件修改完成."
22 exit
23 fi
24 done
<p>
<span class="cnblogs_code_collapse">View Code 批量改名</span></div> </div>
<p>
查看结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn for2]# <span style="color: #0000ff;">ls</span> /clsn/<span style="color: #000000;">
aeyaesughi_znix.HTML caireasipi_znix.HTML uahahnieli_znix.HTML aifaepheeb_znix.HTML eathixoong_znix.HTML zalipageen_znix.HTML akuipheeye_znix.HTML ietoothaik_znix.HTML apheikieno_znix.HTML lachohtaif_znix.HTML
<h4>
<span id="1221nbsp"><strong>1.2.2.1 </strong><strong>批量改名其他方式</strong></span>
</h4>
<p>
rename 方式(最方便,专业改名)
</p>
<div>
<div class="cnblogs_code">
<pre>rename txt jpg *</pre>
</div>
</div>
<p>
<em> 非 for </em><em>循环方式批量改名</em>(使用sed命令进行拼接,然后交给bash执行)
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">ls</span> *jpg|<span style="color: #0000ff;">sed</span> -r <span style="color: #800000;">'</span><span style="color: #800000;">s#(.*).jpg#mv & \1.mp4#</span><span style="color: #800000;">'</span>|bash</pre>
</div>
</div>
<h3>
<span id="123_3">1.2.3 【练习题3】批量创建特殊要求用户案例</span>
</h3>
<p>
批量创建10个系统帐号clsn01-clsn10并设置密码(密码为随机数,要求字符和数字等混合)。
</p>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('d9dd3963-a1b5-4ae8-83e6-4939d3d74038')">
<img id="code_img_closed_d9dd3963-a1b5-4ae8-83e6-4939d3d74038" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_d9dd3963-a1b5-4ae8-83e6-4939d3d74038" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('d9dd3963-a1b5-4ae8-83e6-4939d3d74038',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_d9dd3963-a1b5-4ae8-83e6-4939d3d74038" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn for2]# <span style="color: #0000ff;">cat</span> add_user.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: add_user.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-08 11:52:21
9 # Description:
10 #############################################################
11
12 Passwd_File=/tmp/uuidgen
.txt
13 >$Passwd_File
14 chmod 400 $Passwd_File
15
16 for i in clsn{01..10}
17 do
18 userdel -r "$i" &>/dev/null
19 id $i &>/dev/null
20 if [ $? -ne ]
21 then
22 useradd $i
23 PassWd=uuidgen
24 echo $PassWd |passwd –stdin $i &>/dev/null
25 echo "用户名:$i 密码:$PassWd" »$Passwd_File
26 echo -e "\033[32m $i 用户创建成功!\033[0m"
27 else
28 echo "$i 用户已存在"
29 fi
30 if [ "$i" == "clsn10" ]
31 then
32 echo "用户密码请查看文件 $Passwd_File"
33 fi
34 done
<p>
<span class="cnblogs_code_collapse">View Code 批量创建特殊要求用户</span></div> </div>
<p>
查看成的密码文件
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn for2]# <span style="color: #0000ff;">cat</span> /tmp/3e5c18d9-f878-4d06-931e-<span style="color: #000000;">5bbcc810c3dc.txt
用户名:clsn01 密码:3d4644d0-9cf4-49db-8928-1a8346972c32 用户名:clsn02 密码:70798c3a-c8e3-42a0-9942-d4011ce4b4b3 用户名:clsn03 密码:db2a0f1d-2e49-44f5-a5b2-69b352b30120 用户名:clsn04 密码:62d2e0c6-b755-4b00-ad2d-c98f9ca9f258 用户名:clsn05 密码:eaa3471b-d04f-4d7c-8b7e-3d75172a483b 用户名:clsn06 密码:fb260a11-cd47-4b97-ab49-0cae7a755262 用户名:clsn07 密码:16ee7a1f-8aac-4537-b1aa-7fc75beb8754 用户名:clsn08 密码:0dde8823-b97d-4c88-9258-3a68a3b53eba 用户名:clsn09 密码:daf14ec4-ba9f-4593-9773-1557fdf605dc 用户名:clsn10 密码:6f1b452c-00b2-44a1-9f43-5f658d3a9124
<p>
脚本执行过程:
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212152432035-1172352234.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<h4>
<span id="1231nbsp_shell"><strong>1.2.3.1 </strong><strong>批量创建用户并设置随机密码(不使用shell</strong><strong>循环)</strong></span>
</h4>
<p>
方法一
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">echo</span> user{<span style="color: #800080;">1</span>..<span style="color: #800080;">20</span>}|<span style="color: #0000ff;">xargs</span> -n1|<span style="color: #0000ff;">sed</span> -r <span style="color: #800000;">'</span><span style="color: #800000;">s#(.*)#useradd \1 \&\& echo \1 >>/tmp/passwd.txt \&\& echo $RANDOM |md5sum |cut -c 1-5>>/tmp/passwd.txt \&\& echo `tail -1 /tmp/passwd.txt`|passwd --stdin \1#g</span><span style="color: #800000;">'</span>|bash</pre>
</div>
</div>
<p>
方法二
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">echo</span> user{<span style="color: #800080;">1</span>..<span style="color: #800080;">20</span>}|<span style="color: #0000ff;">xargs</span> -n1|<span style="color: #0000ff;">sed</span> -r <span style="color: #800000;">'</span><span style="color: #800000;">s#(.*)#useradd \1 \&\& pass=`echo $RANDOM |md5sum |cut -c 1-5` \&\& echo $pass |passwd --stdin \1 \&\& echo \1 $pass>>/tmp/user_passwd.txt#g</span><span style="color: #800000;">'</span>|bash</pre>
</div>
</div>
<p>
方法三
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">echo</span> user{<span style="color: #800080;">1</span>..<span style="color: #800080;">20</span>}|<span style="color: #0000ff;">xargs</span> -n1|<span style="color: #0000ff;">sed</span> -r <span style="color: #800000;">'</span><span style="color: #800000;">s#(.*)#useradd \1 \&\& pass=`echo $RANDOM |md5sum |cut -c 1-5` \&\& echo \1:$pass>>/tmp/user_passwd.txt \&\& chpasswd</tmp/user_passwd.txt#g</span><span style="color: #800000;">'</span>|bash</pre>
</div>
</div>
<h3>
<span id="124_4">1.2.4 【练习题4】扫描网络内存活主机案例</span>
</h3>
<p>
写一个Shell脚本,判断10.0.0.0/24网络里,当前在线的IP有哪些?
</p>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('d5ef8c09-6b8b-4edc-a01b-7be825c90340')">
<img id="code_img_closed_d5ef8c09-6b8b-4edc-a01b-7be825c90340" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_d5ef8c09-6b8b-4edc-a01b-7be825c90340" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('d5ef8c09-6b8b-4edc-a01b-7be825c90340',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_d5ef8c09-6b8b-4edc-a01b-7be825c90340" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn <span style="color: #0000ff;">for</span>]# <span style="color: #0000ff;">cat</span> scan_ip2.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: scan_ip.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-07 21:58:47
9 # Description:
10 #############################################################
11
12 Ip_File=/tmp/scan_ip.txt
13 >$Ip_File
14
15 for i in 10.0..{1..254}
16 do
17 ping -c 1 -W 1 $i &>/dev/null &&
18 if [ $? -eq ] ;then
19 echo "存活主机: $i" &»$Ip_File
20 fi &
21 done
22 echo "使用 cat $Ip_File 查看扫描结果"
<p>
<span class="cnblogs_code_collapse">View Code 扫描网络内存活主机</span></div> </div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">for</span>]# <span style="color: #0000ff;">time</span> <span style="color: #0000ff;">sh</span> scan_ip2.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
使用 cat /tmp/scan_ip.txt 查看扫描结果
real 0m0.290s user 0m0.001s sys 0m0.039s
[root@clsn for]# cat /tmp/scan_ip.txt 存活主机: 10.0.0.180 存活主机: 10.0.0.254
<h3>
<span id="125_5">1.2.5 【练习题5】筛选符合长度的单词案例</span>
</h3>
<p>
利用bash for循环打印下面这句话中字母数不大于6的单词(某企业面试真题)。
</p>
<div>
<p class="a4">
I am clsn Welcome to my blog http://blog.znix.top
</p>
</div>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('46dd8086-576a-40ab-a4bd-3009b7f76456')">
<img id="code_img_closed_46dd8086-576a-40ab-a4bd-3009b7f76456" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_46dd8086-576a-40ab-a4bd-3009b7f76456" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('46dd8086-576a-40ab-a4bd-3009b7f76456',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_46dd8086-576a-40ab-a4bd-3009b7f76456" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn <span style="color: #0000ff;">for</span>]# vim changdu.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: changdu.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-07 22:36:48
9 # Description:
10 #############################################################
11
12 Word=’I am clsn Welcome to my blog http://blog.znix.top’
13
14 for i in $Word
15 do
16 #[ ${#i} -le 6 ] && echo $i #子串方法
17 a=<span style="color: #0000ff;">echo</span> $i |<span style="color: #0000ff;">wc</span> -<span style="color: #000000;">L
18 if [ $a -le 6 ]
19 then
20 echo $i
21 fi
22 done
<p>
<span class="cnblogs_code_collapse">View Code 筛选符合长度的单词</span></div> </div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">for</span>]# <span style="color: #0000ff;">sh</span> changdu.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
I am clsn to my blog
<p>
方法二:
</p>
<div>
<div class="cnblogs_code">
<pre>read -p <span style="color: #800000;">"</span><span style="color: #800000;">请输入要判断的语句:</span><span style="color: #800000;">"</span><span style="color: #000000;"> a
set – $a for i in "$@" do if [ ${#i} -le 6 ];then echo "$i" fi done
<p>
由 <strong><a href="https://home.cnblogs.com/u/1233234%20%20" target="_blank">https://home.cnblogs.com/u/1233234 </a> @</strong><strong>贰佰 </strong><strong>提供</strong>
</p>
</div>
<p>
<strong>使用expr </strong><strong>计算字符串长度</strong>
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# <span style="color: #0000ff;">expr</span> length <span style="color: #800000;">'</span><span style="color: #800000;">111</span><span style="color: #800000;">'</span>
3
<h3>
<span id="126_6RANDOM">1.2.6 【练习题6】破解RANDOM随机数案例</span>
</h3>
<p>
已知下面的字符串是通过RANDOM随机数变量md5sum后,再截取一部分连续字符串的结果,请破解这些字符串对应的使用md5sum处理前的RANDOM对应的数字?
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #800080;"> 21029299</span><span style="color: #000000;">
00205d1c a3da1677 1f6d12dd 890684b
<p>
脚本内容
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('ab034c27-f9aa-403c-97ce-6f1247bc74c5')">
<img id="code_img_closed_ab034c27-f9aa-403c-97ce-6f1247bc74c5" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_ab034c27-f9aa-403c-97ce-6f1247bc74c5" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('ab034c27-f9aa-403c-97ce-6f1247bc74c5',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_ab034c27-f9aa-403c-97ce-6f1247bc74c5" class="cnblogs_code_hide">
<pre>[root@clsn <span style="color: #0000ff;">for</span>]# vim pojie.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: pojie.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-07 22:41:01
Description:
#############################################################
md5File=/tmp/Randow_Md5.txt Md5_Word="21029299 00205d1c a3da1677 1f6d12dd 890684b"
if [ ! -f $md5File ]
then
>$md5File
for i in {..32767}
do
echo <span style="color: #0000ff;">echo</span> $i |md5sum
$i » $md5File
done
else
for num in $Md5_Word
do
grep $num $md5File
done
fi
<p>
<span class="cnblogs_code_collapse">View Code 破解RANDOM随机数</span></div> </div>
<p>
脚本执行结果
</p>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">for</span>]# <span style="color: #0000ff;">sh</span> pojie.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
2102929901ee1aa769d0f479d7d78b05 - 25667 00205d1cbbeb97738ad5bbdde2a6793d - 1346 a3da1677501d9e4700ed867c5f33538a - 25345 1f6d12dd61b5c7523f038a7b966413d9 - 7041 890684ba3685395c782547daf296935f - 10082
<h3>
<span id="127_7">1.2.7 【练习题7】博客园博文爬虫案例</span>
</h3>
<p>
获取博客园(惨绿少年)博客列表倒序排序考试题
</p>
<p>
需求如下:
</p>
<p>
请把https://www.cnblogs.com/clsn/地址中的所有博文,按照时间倒序列表如下:
</p>
<div>
<p class="a4">
2017年12月8日 Shell编程基础篇-下
</p>
<p class="a4">
http://www.cnblogs.com/clsn/p/8006210.html
</p>
<p class="a4">
2017年12月7日 memcached 缓存数据库应用实践
</p>
<p class="a4">
http://www.cnblogs.com/clsn/p/7999510.html
</p>
</div>
<p>
<strong>高级要求:</strong>
</p>
<p>
生成html页面,并设置超链接。
</p>
<p>
结果如改网页展示:<a href="/wp-content/themes/clsn-003/inc/go.php?url=http://www.cnblogs.com/clsn/p/8007232.html" target="_blank">http://www.cnblogs.com/clsn/p/8007232.html</a>
</p>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('239e9124-8caf-4865-a33c-d8f40a6729e3')">
<img id="code_img_closed_239e9124-8caf-4865-a33c-d8f40a6729e3" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_239e9124-8caf-4865-a33c-d8f40a6729e3" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('239e9124-8caf-4865-a33c-d8f40a6729e3',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_239e9124-8caf-4865-a33c-d8f40a6729e3" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn htmp]# <span style="color: #0000ff;">cat</span> clsn_blog.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash 3 ############################################################# 4 # File Name: clsn_blog.sh 5 # Version: V1. 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-08 21:19:12 9 # Description: 10 ############################################################# 11 12 Uri=’http://www.cnblogs.com/clsn/default.html?page=’ 13 14 clsn_Html=/tmp/html/clsn.html 15 mkdir -p /tmp/html/ 16 >$clsn_Html 17 18 for i in {1..6} 19 do 20 curl -s $Uri$i |grep -A 5 ’ImageLink’ |sed ’s#<.div.># #g’|sed ’s#–##g’ » $clsn_Html 21 echo ’’ »$clsn_Html 22 done
<p>
<span class="cnblogs_code_collapse">View Code 博客园博文爬虫案例</span></div> </div>
<p>
脚本成网页文件
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212152934207-743020833.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<h4>
<span id="1271nbsp_51CTO"><strong>1.2.7.1 </strong><strong>51CTO</strong><strong>博客爬虫案例</strong></span>
</h4>
<p>
脚本内容
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('2dfa2e63-5e0e-4040-a4fb-e85847395827')">
<img id="code_img_closed_2dfa2e63-5e0e-4040-a4fb-e85847395827" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_2dfa2e63-5e0e-4040-a4fb-e85847395827" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('2dfa2e63-5e0e-4040-a4fb-e85847395827',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_2dfa2e63-5e0e-4040-a4fb-e85847395827" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn html]# <span style="color: #0000ff;">cat</span> oldboy_blog.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: oldboy_blog.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-08 22:30:57
9 # Description:
10 #############################################################
11
12 Uri=’http://blog.51cto.com/oldboy/p’
13 Blog_Index_File=/tmp/html/oldboy_blog.html
14 mkdir -p /tmp/html
15 > /tmp/html/oldboy_blog.html
16
17 for i in {1..29}
18 do
19 curl -s $Uri$i |grep -A 5 ’发布于’ |
20 sed ’/^.zan fr./,+2d’|
21 sed ’s#^–$##g’|
22 sed ’s#<a.*fl">发布于:##g’|
23 sed ’s#<sp.*an>##g’ »
24 $Blog_Index_File
25 echo ’’ »$Blog_Index_File
26 done
输入/输出格式规范: -f, –from-code=名称 原始文本编码 -t, –to-code=名称 输出编码
信息: -l, –list 列举所有已知的字符集
输出控制: -c 从输出中忽略无效的字符 -o, –output=FILE 输出文件 -s, –silent 关闭警告 –verbose 打印进度信息
-?, –help 给出该系统求助列表 –usage 给出简要的用法信息 -V, –version 打印程序版本号
长选项的强制或可选参数对对应的短选项也是强制或可选的
</div>
<p class="ad">
常用:
</p>
</div>
<tr>
<td valign="top" width="61%">
<p>
<span style="font-size: 13px;">在编程语言中,while循环(英语:<em>while loop</em>)是一种控制流程的陈述。利用一个返回结果为布林值(<em>Boolean</em>)的表达式作为循环条件,当这个表达式的返回值为“真”(<em>true</em>)时,则反复执行循环体内的程式码;若表达式的返回值为“假”(<em>false</em>),则不再执行循环体内的代码,继续执行循环体下面的代码。</span>
</p>
<p>
<span style="font-size: 13px;"> 因为while循环在区块内代码被执行之前,先检查陈述是否成立,因此这种控制流程通常被称为是一种前测试循环(<em>pre-test loop</em>)。相对而言do while循环,是在循环区块执行结束之后,再去检查陈述是否成立,被称为是后测试循环。</span>
</p>
</td>
<td width="38%">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212153109941-268328166.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<p>
</p>
</td>
</tr>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="a">
<span lang="EN-US">usleep </span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">单位</span> <span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">微秒</span> <em><span lang="EN-US">usleep 1000000 </span></em><em><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">休息</span><span lang="EN-US">1s</span></em>
</p>
<p class="a">
<span lang="EN-US">1</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">微秒等于百万分之一秒(</span><span lang="EN-US">10</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">的负</span><span lang="EN-US">6</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">次方秒)</span>
</p>
</div>
real 0m0.102s user 0m0.001s sys 0m0.000s
</div>
<div id="cnblogs_code_open_fcc7b82e-1436-4e49-9254-9a5d374fb626" class="cnblogs_code_hide">
<pre><span style="color: #008080;">1</span> 使用while循环实现水果菜单的重复使用</pre>
</div>
<p>
<span class="cnblogs_code_collapse">View Code 使用while循环实现水果菜单的重复使用</span></div>
</div>
<p>
脚本执行过程
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212153500801-1405731648.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<p>
<em><span style="text-decoration: underline;">示例2</span></em><em><span style="text-decoration: underline;">:计算1-100</span></em><em><span style="text-decoration: underline;">的和</span></em>
</p>
<p>
方法一 (bc命令实现)
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">echo</span> `<span style="color: #0000ff;">seq</span> -s + <span style="color: #800080;">1</span> <span style="color: #800080;">100</span>`|bc</pre>
</div>
</div>
<p>
方法二(while循环方法)
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">while</span>]# <span style="color: #0000ff;">cat</span> jishan.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: jishan.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-09 15:18:44
Description:
#############################################################
i=1
while [ "$i" -le 100 ] do ((b=b+i)) ((i++)) done echo $b
</div>
</div>
<p>
<em><span style="text-decoration: underline;">示例3</span></em><em><span style="text-decoration: underline;">:实现类似手机通讯计费功能</span></em>
</p>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('df7f21e7-d3b3-4c64-9124-0aa487b975ca')">
<img id="code_img_closed_df7f21e7-d3b3-4c64-9124-0aa487b975ca" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_df7f21e7-d3b3-4c64-9124-0aa487b975ca" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('df7f21e7-d3b3-4c64-9124-0aa487b975ca',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_df7f21e7-d3b3-4c64-9124-0aa487b975ca" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]# <span style="color: #0000ff;">cat</span> <span style="color: #0000ff;">while</span>/shouji.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash 3 ############################################################# 4 # File Name: shouji.sh 5 # Version: V1. 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-09 15:56:09 9 # Description: 10 ############################################################# 11 sum=1000 12 i=15 13 14 15 while [ $sum -ge 15 ] 16 do 17 cat<<EOF 18 ================= 19 1.发短信 20 2.查余额 21 3.账户充值 22 4.退出 23 ================= 24 EOF 25 read -p "你要做什么呢?" Some 26 case "$Some" in 27 1) 28 sum=$((sum-i)) 29 read -p "请输入发送短信的内容:" 30 read -p "请输入收信人:" 31 sleep 0.3 32 echo "发送成功." 33 echo "您当前余额为$sum" 34 ;; 35 2) 36 echo "您当前余额为$sum" 37 ;; 38 3) 39 read -p "请输入你要充值的金额:" ChongZhi 40 sum=$((sum+ChongZhi)) 41 echo "充值成功,当前余额为$sum" 42 ;; 43 4) 44 exit 45 ;; 46 *) 47 echo "输入有误!" 48 exit 2 49 esac 50 done 51 52 echo "余额不足,请及时充值!"
</div>
<p>
<span class="cnblogs_code_collapse">View Code :实现类似手机通讯计费功能</span></div>
</div>
<h2>
<span id="14">1.4 获取取文件中的行,单词和字符</span>
</h2>
<h3>
<span id="141">1.4.1 迭代获取文件中的每一行</span>
</h3>
<p>
方法一
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">while</span><span style="color: #000000;"> read line;
do echo $line; done < file.txt
</div>
</div>
<p>
方法二
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">cat</span> <span style="color: #0000ff;">file</span>.txt|<span style="color: #0000ff;">while</span><span style="color: #000000;"> read line
do echo $line done
</div>
</div>
<p>
方法三
</p>
<div>
<div class="cnblogs_code">
<pre>exec < <span style="color: #0000ff;">file</span><span style="color: #000000;">.txt
while read line; do echo line; done
</div>
</div>
<h3>
<span id="142">1.4.2 迭代获取每一个单词</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">for</span> word <span style="color: #0000ff;">in</span><span style="color: #000000;"> $line;
do echo $word; done
</div>
</div>
<h3>
<span id="143">1.4.3 迭代获取每一个字符</span>
</h3>
<div>
<div class="cnblogs_code">
<pre>word=<span style="color: #000000;">participate
for ((i=;i<${#word};i++)) do echo ${word:1:1}; done
</div>
</div>
<h3>
<span id="144">1.4.4 同时获取取文件中的行,单词和字符脚本</span>
</h3>
<p>
脚本内容
</p>
<div>
<div class="cnblogs_code">
<pre>#!/bin/<span style="color: #000000;">bash
n=1 while read i do echo "第${n}行 $i" m=1 for x in $i do echo "第${m}个单词 $x" echo $x|grep -o . ((m++)) done ((n++)) done < $1
</div>
</div>
<p>
脚本执行结果:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('d6b73795-c420-4e62-ae42-7e7d6732cfa4')">
<img id="code_img_closed_d6b73795-c420-4e62-ae42-7e7d6732cfa4" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_d6b73795-c420-4e62-ae42-7e7d6732cfa4" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('d6b73795-c420-4e62-ae42-7e7d6732cfa4',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_d6b73795-c420-4e62-ae42-7e7d6732cfa4" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]# <span style="color: #0000ff;">sh</span> lunxun.<span style="color: #0000ff;">sh</span> /etc/<span style="color: #000000;">hosts
2 第1行 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 3 第1个单词 127.0.0.1 4 1 5 2 6 7 7 . 8 9 . 10 11 . 12 1 13 第2个单词 localhost 14 l 15 o 16 c 17 a 18 l 19 h 20 o 21 s 22 t 23 第3个单词 localhost.localdomain 24 l 25 o 26 c 27 a 28 l 29 h 30 o 31 s 32 t 33 . 34 l 35 o 36 c 37 a 38 l 39 d 40 o 41 m 42 a 43 i 44 n 45 第4个单词 localhost4 46 l 47 o 48 c 49 a 50 l 51 h 52 o 53 s 54 t 55 4 56 第5个单词 localhost4.localdomain4 57 l 58 o 59 c 60 a 61 l 62 h 63 o 64 s 65 t 66 4 67 . 68 l 69 o 70 c 71 a 72 l 73 d 74 o 75 m 76 a 77 i 78 n 79 4 80 第2行 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 81 第1个单词 ::1 82 : 83 : 84 1 85 第2个单词 localhost 86 l 87 o 88 c 89 a 90 l 91 h 92 o 93 s 94 t 95 第3个单词 localhost.localdomain 96 l 97 o 98 c 99 a 100 l 101 h 102 o 103 s 104 t 105 . 106 l 107 o 108 c 109 a 110 l 111 d 112 o 113 m 114 a 115 i 116 n 117 第4个单词 localhost6 118 l 119 o 120 c 121 a 122 l 123 h 124 o 125 s 126 t 127 6 128 第5个单词 localhost6.localdomain6 129 l 130 o 131 c 132 a 133 l 134 h 135 o 136 s 137 t 138 6 139 . 140 l 141 o 142 c 143 a 144 l 145 d 146 o 147 m 148 a 149 i 150 n 151 6 152 第3行 10.0.0.1 mirrors.aliyuncs.com mirrors.aliyun.com 153 第1个单词 10.0.0.1 154 1 155 156 . 157 158 . 159 160 . 161 1 162 第2个单词 mirrors.aliyuncs.com 163 m 164 i 165 r 166 r 167 o 168 r 169 s 170 . 171 a 172 l 173 i 174 y 175 u 176 n 177 c 178 s 179 . 180 c 181 o 182 m 183 第3个单词 mirrors.aliyun.com 184 m 185 i 186 r 187 r 188 o 189 r 190 s 191 . 192 a 193 l 194 i 195 y 196 u 197 n 198 . 199 c 200 o 201 m 202 第4行 10.0.0.180 clsn 203 第1个单词 10.0.0.180 204 1 205 206 . 207 208 . 209 210 . 211 1 212 8 213 214 第2个单词 clsn 215 c 216 l 217 s 218 n
</div>
<p>
<span class="cnblogs_code_collapse">View Code 1.4.4 同时获取取文件中的行,单词和字符脚本 执行结果</span></div>
</div>
<h3>
<span id="145_eval">1.4.5 eval 命令用法</span>
</h3>
<div>
<div class="cnblogs_code">
<pre>[root@clsn ~]# clsn=<span style="color: #800080;">6</span><span style="color: #000000;">
[root@clsn ~]# echo {1..$clsn} {1..6} [root@clsn ~]# eval echo {1..$clsn} 1 2 3 4 5 6
</div>
</div>
<p>
eval 命令的说明
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn ~<span style="color: #000000;">]# help eval
eval: eval [参数 …] 将参数作为 shell 命令执行。
将 ARGs 合成一个字符串,用结果作为 shell 的输入,
并且执行得到的命令。
退出状态:
以命令的状态退出,或者在命令为空的情况下返回成功。</span></pre>
</div>
</div>
<h2>
<span id="15_break_continue_exit_return">1.5 break continue exit return</span>
</h2>
<p>
条件与循环控制及程序返回值命令表
</p>
<table class="MsoTable15Grid4Accent1" style="width: 100%; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width: 17.58%; border-top-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-top-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left-color: #5b9bd5; border-right: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="17%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 5;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">命令</span></strong>
</p>
</td>
<td style="width: 82.42%; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; border-top-color: #5b9bd5; border-right-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="82%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">说明</span></strong>
</p>
</td>
</tr>
<tr>
<td style="width: 17.58%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="17%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 68;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">break n </span></strong>
</p>
</td>
<td style="width: 82.42%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="82%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">如果省略</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">,则表示跳出整个循环,</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">表示跳出循环的层数</span>
</p>
</td>
</tr>
<tr>
<td style="width: 17.58%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="17%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 4;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">continue n</span></strong>
</p>
</td>
<td style="width: 82.42%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" valign="top" width="82%">
<p class="MsoNormal" style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">如果省略</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">,则表示跳过本次循环,忽略本次循环的剩余代码,进人循环的下一次循环。</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US"> n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">表示退到第</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">层继续循环</span>
</p>
</td>
</tr>
<tr>
<td style="width: 17.58%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="17%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 68;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">exit n</span></strong>
</p>
</td>
<td style="width: 82.42%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="82%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">退出当前</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">Shell</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">程序,</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">为上一次程序执行的状态返回值。</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">也可以省略,在下一个</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">Shell</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">里可通过</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">"$?"</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">接收</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">exit n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">值</span>
</p>
</td>
</tr>
<tr>
<td style="width: 17.58%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="17%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 4;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">return n</span></strong>
</p>
</td>
<td style="width: 82.42%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" valign="top" width="82%">
<p class="MsoNormal" style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">用于在函数里作为函数的返回值,以判断函数执行是否正确。在下一个</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">Shell</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">里可通过</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">"$?"</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">接收</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">exit n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">值</span>
</p>
</td>
</tr>
</table>
<div class="cnblogs_code">
<pre><span style="color: #000000;">简单来说即:<br />break 跳出循环
continue 跳出本次循环 exit 退出脚本 return 与 exit 相同,在函数中使用
</div>
<h3>
<span id="151_break">1.5.1 break命令说明</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@clsn scripts]# help break
break: break [n] 退出 for、while、或 until 循环
退出一个 FOR、WHILE 或 UNTIL 循环。如果指定了N,则打破N重
循环
退出状态:
退出状态为0除非 N 不大于或等于 </span><span style="color: #800080;">1</span>。</pre>
</div>
</div>
<p>
测试脚本内容
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('e7c804ca-389a-4b45-8240-8d6e6cc5be06')">
<img id="code_img_closed_e7c804ca-389a-4b45-8240-8d6e6cc5be06" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_e7c804ca-389a-4b45-8240-8d6e6cc5be06" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('e7c804ca-389a-4b45-8240-8d6e6cc5be06',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_e7c804ca-389a-4b45-8240-8d6e6cc5be06" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> #!/bin/<span style="color: #000000;">bash
2 3 for i in {1..5} 4 5 do 6 7 if [ $i -eq 3 ] 8 9 then 10 11 break 12 13 fi 14 15 echo $i 16 17 done 18 19 echo "ok"
</div>
<p>
<span class="cnblogs_code_collapse">View Code break 测试脚本</span></div>
</div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# <span style="color: #0000ff;">sh</span> break.<span style="color: #0000ff;">sh</span>
<span style="color: #800080;">1</span>
<span style="color: #800080;">2</span><span style="color: #000000;">
ok</span></pre>
</div>
</div>
<h3>
<span id="152_continue">1.5.2 continue命令说明</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@clsn scripts]# help continue
continue: continue [n] 继续 for、while、或 until 循环。
继续当前 FOR、WHILE 或 UNTIL 循环的下一步。
如果指定了 N, 则继续当前的第 N 重循环。
退出状态:
退出状态为 </span><span style="color: #800080;"></span> 除非 N 不大于或等于1。</pre>
</div>
</div>
<p>
测试脚本内容
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('543d7a8a-c947-488f-954e-036c818f4d4b')">
<img id="code_img_closed_543d7a8a-c947-488f-954e-036c818f4d4b" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_543d7a8a-c947-488f-954e-036c818f4d4b" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('543d7a8a-c947-488f-954e-036c818f4d4b',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_543d7a8a-c947-488f-954e-036c818f4d4b" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> #!/bin/<span style="color: #000000;">bash
2 for i in {1..5} 3 do 4 if [ $i -eq 3 ] 5 then 6 continue 7 fi 8 echo $i 9 done 10 echo "ok"
</div>
<p>
<span class="cnblogs_code_collapse">View Code continue 测试脚本</span></div>
</div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# <span style="color: #0000ff;">sh</span> continue.<span style="color: #0000ff;">sh</span>
1 2 4 5 ok
</div>
</div>
<h3>
<span id="153_exit">1.5.3 exit命令说明</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@clsn scripts]# help exit
exit: exit [n] 退出shell。
以状态 N 退出 shell。 如果 N 被省略,则退出状态
为最后一个执行的命令的退出状态。</span></pre>
</div>
</div>
<p>
测试脚本内容
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('c2ce9413-e0ad-4c5f-b136-8e3fb98d1f12')">
<img id="code_img_closed_c2ce9413-e0ad-4c5f-b136-8e3fb98d1f12" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_c2ce9413-e0ad-4c5f-b136-8e3fb98d1f12" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('c2ce9413-e0ad-4c5f-b136-8e3fb98d1f12',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_c2ce9413-e0ad-4c5f-b136-8e3fb98d1f12" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> #!/bin/<span style="color: #000000;">bash
2 for i in {1..5} 3 do 4 if [ $i -eq 3 ] 5 then 6 exit 7 fi 8 echo $i 9 done 10 echo "ok"
</div>
<p>
<span class="cnblogs_code_collapse">View Code exit 测试脚本</span></div>
</div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre> [root@clsn scripts]# <span style="color: #0000ff;">sh</span> exit.<span style="color: #0000ff;">sh</span>
<span style="color: #800080;">1</span>
<span style="color: #800080;">2</span></pre>
</div>
</div>
<h3>
<span id="154_return">1.5.4 return命令说明</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@clsn tuichu]# help return
return: return [n] 从一个 shell 函数返回。
使一个函数或者被引用的脚本以指定的返回值 N 退出。
如果 N 被省略,则返回状态就是
函数或脚本中的最后一个执行的命令的状态。
退出状态:
返回 N,或者如果 shell 不在执行一个函数或引用脚本时,失败。</span></pre>
</div>
</div>
<h2>
<span id="16_shell">1.6 shell中的数组</span>
</h2>
<h3>
<span id="161_Shell">1.6.1 为什么会产生Shell数组</span>
</h3>
<p>
通常在开发Shell脚本时,定义变量采用的形式为“a=l;b=2;C=3”,可如果有多个 变量呢?这时再逐个地定义就会很费劲,并且要是有多个不确定的变量内容,也会难以 进行变量定义,此外,快速读取不同变量的值也是一件很痛苦的事情,于是数组就诞生 了,它就是为了解决上述问题而出现的。
</p>
<h3>
<span id="162_Shell">1.6.2 什么是Shell数组</span>
</h3>
<p>
Shell的数组就是一个元素集合,它把有限个元素(变量或字符内容)用一个名字来 命名,然后用编号对它们进行区分。这个名字就称为数组名,用于区分不同内容的编 号就称为数组下标。组成数组的各个元素(变量)称为数组的元素,有时也称为下标变量。
</p>
<h3>
<span id="163">1.6.3 数组中的增删改查</span>
</h3>
<p>
数组的定义
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;"># 定义数组
[root@clsn scripts]# stu=(001 002 003)
打印数组
[root@clsn scripts]# echo ${stu[@]} 001 002 003
显示数组长度
[root@clsn scripts]# echo ${#stu} 3
</div>
</div>
<p>
<strong><em>查: </em></strong><em>遍历数组的内容</em>
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;"># 打印数组内容(通过数组下标或索引)
[root@clsn scripts]# echo ${stu[]} 001 [root@clsn scripts]# echo ${stu[1]} 002 [root@clsn scripts]# echo ${stu[2]} 003 [root@clsn scripts]# echo ${stu[3]}
# 遍历数组
方法一
[root@clsn scripts]# for i in ${stu[@]};do echo $i ;done
001
002
003
# 方法二
[root@clsn scripts]# array=(1 2 3 4 5)
[root@clsn scripts]# for i in eval <span style="color: #0000ff;">echo</span> {<span style="color: #800080;">1</span>..${#array[@]}}
;do echo ${array[i-1]};done
1
2
3
4
5
</div>
</div>
<p>
<strong><em>增</em></strong><em>:数组添加</em>
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# stu[<span style="color: #800080;">3</span>]=<span style="color: #800080;">004</span><span style="color: #000000;">
[root@clsn scripts]# echo ${stu[@]} 001 002 003 004
</div>
</div>
<p>
<strong><em>改</em></strong>:<em>数组修改</em>
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# stu[<span style="color: #800080;">2</span>]=<span style="color: #800080;">000</span><span style="color: #000000;">
[root@clsn scripts]# echo ${stu[@]} 001 002 000 004
</div>
</div>
<p>
<strong><em>删</em></strong>:<em>数组删除</em>
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# unset stu[<span style="color: #800080;">2</span><span style="color: #000000;">]
[root@clsn scripts]# echo ${#stu[@]} 3 [root@clsn scripts]# echo ${stu[@]} 001 002 004
</div>
</div>
<h3>
<span id="164">1.6.4 将命令的结果赋值给数组</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">dir</span>=(`<span style="color: #0000ff;">ls</span><span style="color: #000000;">`)
dir=($(ls))
</div>
</div>
<p>
命令定义数组
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# COM=(`<span style="color: #0000ff;">ls</span><span style="color: #000000;">`)
[root@clsn scripts]# echo ${COM[@]} bianliang.sh case cfb.sh chanshu.sh check check_url.sh clsn.sh clsn_test.sh daojishi.sh ddos_check.sh echo.sh for for2 fruits.shfunction fz.sh html jingdutiao.sh jishuanqi2.sh jishuanqi.sh lamp.sh lnmp.sh memcache_check.sh menu.sh nginx.sh panduan panduan1 play quanju.sh rsync_check.sh rsyncd system6 tuichu web_check.sh web_view.sh while xiugaizhuji.sh yhk.sh yunshuan.sh zhuajiu.sh
</div>
</div>
<h3>
<span id="161">1.6.1 数组定义格式</span>
</h3>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# a=(<span style="color: #800080;">1</span> <span style="color: #800080;">2</span> <span style="color: #800080;">3</span><span style="color: #000000;"> )
[root@clsn scripts]# b=(1
2 3 4 ) [root@clsn scripts]# echo ${a[@]} 1 2 3 [root@clsn scripts]# echo ${b[@]} 1 2 3 4
</div>
</div>
<h3>
<span id="162">1.6.2 数组的本质就是一个变量,只是这个变量存了多个值</span>
</h3>
<div>
<p class="a4">
在shell 常用的功能是查
</p>
</div>
<p>
array=(valuel value2 value3 ...)
</p>
<p>
<strong><em>打印数组格式</em></strong>
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">${array[@]} 所有元素
${#array[@]} 数组长度 ${array[i]} 单个元素,i是下标
</div>
</div>
<h3>
<span id="163-2">1.6.3 【练习题】批量检查多个网站地址是否正常</span>
</h3>
<p>
要求:
</p>
<p>
1、<strong><span style="text-decoration: underline;">使用</span></strong><strong><span style="text-decoration: underline;">shell</span></strong><strong><span style="text-decoration: underline;">数组方法实现</span></strong>,检测策略尽量模拟用户访问。
</p>
<p>
2、每10秒钟做一次所有的检测,无法访问的输出报警。
</p>
<p>
3、待检测的地址如下
</p>
<div>
<div style="mso-element: para-border-div; border: solid windowtext 2.0pt; padding: 1.0pt 4.0pt 1.0pt 4.0pt; mso-border-shadow: yes; background: #F2F2F2; mso-shading: windowtext; mso-pattern: gray-5 auto;">
<p class="a">
<span lang="EN-US"> http://www.cnblogs.com/clsn/</span>
</p>
<p class="a">
<span lang="EN-US"> http://blog.znix.top</span>
</p>
<p class="a">
<span lang="EN-US"> http://blog.nmtui.com</span>
</p>
<p class="a">
<span lang="EN-US"> http://10.0.0.7</span>
</p>
</div>
</div>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('580d7ec8-e8d0-4650-b89f-31059a87e60a')">
<img id="code_img_closed_580d7ec8-e8d0-4650-b89f-31059a87e60a" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_580d7ec8-e8d0-4650-b89f-31059a87e60a" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('580d7ec8-e8d0-4650-b89f-31059a87e60a',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_580d7ec8-e8d0-4650-b89f-31059a87e60a" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]# <span style="color: #0000ff;">cat</span> check_url.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash 3 ############################################################# 4 # File Name: check_url.sh 5 # Version: V1.3 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-12 10:44:39 9 # Description: 10 ############################################################# 11 url=( 12 http://www.cnblogs.com/clsn/ 13 http://blog.znix.top 14 http://blog.nmtui.com 15 http://10.0.0.7 16 ) 17 while true 18 do 19 for i in ${url[@]} 20 do 21 #curl $i &>/dev/null 22 a=$(curl -I -w "%{http_code}\n" -o /dev/null -s $i) 23 if [ $a -ne 200 ] 24 then 25 echo "$url 异常" 26 fi 27 done 28 sleep 10 29 done
</div>
<p>
<span class="cnblogs_code_collapse">View Code 批量检查多个网站地址是否正常</span></div>
</div>
<h2>
<span id="17_Shell">1.7 Shell 函数</span>
</h2>
<p>
shell一个非常重要的特性是它可作为一种编程语言来使用。因为shell是一个解释器,所以它不能对为它编写的程序进行编译,而是在每次从磁盘加载这些程序时对它们进行解释。而程序的加载和解释都是非常耗时的。
</p>
<p>
针对此问题,许多shell(如BourneAgainShell)都包含shell函数,shell把这些函数放在内存中,这样每次需要执行它们时就不必再从磁盘读入。shell还以一种内部格式来存放这些函数,这样就不必耗费大量的时间来解释它们。
</p>
<p>
<strong>函数的作用</strong>就是把程序里多次调用相同代码的部分定义成一份,然后起个名字,所有的调用都 只用这名字就可以了,修改代码时,只需要改变函数体内的代码即可。
</p>
<h3>
<span id="171">1.7.1 使用函数的优势</span>
</h3>
<p>
? 把相同的程序段定义成函数,可以减少代码量。
</p>
<p>
? 增加程序的可读、易读性
</p>
<p>
? 实现程序功能的模块化
</p>
<h3>
<span id="172">1.7.2 定义函数</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">function</span><span style="color: #000000;"> clsn(){
</span><span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">http://blog.znix.top</span><span style="color: #800000;">"</span><span style="color: #000000;">
}
znix(){ echo "http://www.znix.top " }
</div>
</div>
<p>
<strong>说明:</strong>
</p>
<p>
1、可以带function clsn() 定义,也可以直接clsn() 定义,不带任何参数。
</p>
<p>
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255)
</p>
<p>
3、执行函数就是将函数名放到定义的函数之后即可
</p>
<p>
<em><span style="text-decoration: underline;">将函数加载到当前窗口执行:</span></em>
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# . fun1.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
[root@clsn function]# zn
znew znix
[root@clsn function]# znix
test
[root@clsn function]# clsn
http://blog.znix.top
</div>
</div>
<h3>
<span id="173">1.7.3 引用函数</span>
</h3>
<p>
脚本内容
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">cat</span> fun2.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: fun2.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-11 19:25:56
Description:
############################################################# Fun_File=/server/scripts/function/fun1.sh [ -f $Fun_File ] && . $Fun_File clsn
</div>
</div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">sh</span> fun2.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
http://blog.znix.top
</div>
</div>
<h3>
<span id="174">1.7.4 函数传参</span>
</h3>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">cat</span> fun3.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: fun3.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-12 09:38:48
Description:
#############################################################
function clsn(){ echo "Hi " } CLSN(){ echo "Hello " echo $ echo $1 echo $2
} clsn CLSN xi xi
</div>
</div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">sh</span> fun3.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
Hi Hello fun3.sh xi xi
</div>
</div>
<h3>
<span id="175_0">1.7.5 $0 永远的脚本名称</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">function</span><span style="color: #000000;"> clsn(){
</span><span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">http://blog.znix.top $1 $2</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">echo</span> $<span style="color: #800080;"></span><span style="color: #000000;">
}
znix(){ echo "test" } clsn $1 $2
</div>
<p class="ad">
执行结果
</p>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">sh</span> fun1.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
http://blog.znix.top fun1.sh
</div>
</div>
<h3>
<span id="176_return">1.7.6 函数中return的用法</span>
</h3>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">cat</span> fun3.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: fun3.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-12 09:38:48
Description:
#############################################################
function clsn(){ echo "Hi " } CLSN(){ echo "Hello " echo $ echo $1 echo $2 return 4 echo "xxixiixxiix"
} clsn CLSN xi xi echo $?
</div>
</div>
<p>
脚本执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">sh</span> fun3.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
Hi Hello fun3.sh xi xi 4
</div>
</div>
<p>
return命令说明:
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span><span style="color: #000000;">]# help return
return: return [n] 从一个 shell 函数返回。
使一个函数或者被引用的脚本以指定的返回值 N 退出。
如果 N 被省略,则返回状态就是
函数或脚本中的最后一个执行的命令的状态。
退出状态:
返回 N,或者如果 shell 不在执行一个函数或引用脚本时,失败。</span></pre>
</div>
</div>
<h3>
<span id="177">1.7.7 自定义常用函数库</span>
</h3>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('c9c337d8-ab2d-4c10-a278-51f8c2c771bf')">
<img id="code_img_closed_c9c337d8-ab2d-4c10-a278-51f8c2c771bf" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_c9c337d8-ab2d-4c10-a278-51f8c2c771bf" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('c9c337d8-ab2d-4c10-a278-51f8c2c771bf',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_c9c337d8-ab2d-4c10-a278-51f8c2c771bf" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> #!/bin/<span style="color: #000000;">bash
2 #############################################################
3 # File Name: /server/scripts/userfun.sh
4 # Version: V1.
5 # Author: clsn
6 # Organization: http://blog.znix.top
7 # Created Time : 2017-12-12 10:40:04
8 # Description:
9 #############################################################
10
11 # 脚本初始化
12 function scripts_init(){
13 prog=<span style="color: #0000ff;">basename</span> $<span style="color: #800080;"></span> .<span style="color: #0000ff;">sh</span><span style="color: #000000;">
14 LockFile=/var/lock/subsys/${prog}.lock # 使用锁文件
15 LogFile=/var/log/${prog}.log # 脚本记录日志
16 PidFile=/var/run/${prog}.pid # 记录进程号,可以管理脚本
17
18 [ -f $LockFile ] && echo "There $LockFile is exist!!" && exit 1 ||touch $LockFile
19 [ ! -f $LogFile ] && touch $LogFile
20 [ -f $PidFile ] && echo "There $PidFile is exist!!" && exit 2|| echo $$ > $PidFile
21 }
22
23 # 记录日志
24 function writelog(){
25 Date=$(date "+%F_%T")
26 ShellName=<span style="color: #0000ff;">basename</span> $<span style="color: #800080;"></span><span style="color: #000000;">
27 Info=$1
28 echo "$Date : ${ShellName} : ${Info}" » ${LogFile}
29 }
30
31 # 脚本退出扫尾
32 function closeout(){
33 [ -f $LockFile ] && rm -f $LockFile
34 [ -f $PidFile ]&& rm -f $PidFile
35 }
36
37 # 判断输入是整数
38 function int_judge(){
39 fun_a=$1
40 expr $fun_a + 1 &>/dev/null
41 RETVAL=$?
42 return $RETVAL
43 }
44
45 # 判断输入非空
46 function input_judge(){
47 RETVAL=
48 fun_a=$1
49 [ ${#fun_a} -eq ]&& RETVAL=1
50 return $RETVAL
51 }
</div>
<p>
<span class="cnblogs_code_collapse">View Code 自定义函数库脚本</span></div>
<p class="ad">
basename命令
</p>
</div>
<p>
取出文件名称
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">basename</span> /server/scripts/zhuajiu.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
zhuajiu.sh [root@clsn function]# basename /server/scripts/zhuajiu.sh .sh zhuajiu
</div>
</div>
<p>
$$ 参数
</p>
<p>
取出脚本运行时的pid, 脚本运行的当前进程ID号
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">echo</span><span style="color: #000000;"> $$
37208 [root@clsn function]# ps -ef |grep 37208 root 37208 37206 08:39 pts/ 00:00:00 -bash root 38578 37208 1 10:33 pts/ 00:00:00 ps -ef root 38579 37208 10:33 pts/ 00:00:00 grep –color=auto 37208
</div>
</div>
<p>
<em><span style="text-decoration: underline;">引用自定义函数库示例: </span></em>
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">function</span>]# <span style="color: #0000ff;">head</span> -<span style="color: #800080;">22</span> fun3.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: fun3.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-12 09:38:48
Description:
#############################################################
. /server/scripts/userfun.sh
scripts_init i=1 while ((i<=10)) do uptime ((i++)) sleep 1 done closeout
</div>
</div>
<h2>
<span id="18_shell">1.8 shell脚本的调试</span>
</h2>
<h3>
<span id="181">1.8.1 脚本调试技巧</span>
</h3>
<p>
调试技巧1:使用dos2unix处理脚本
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">从windows编辑的脚本到Linux下需要使用这个命令
dos2unix windows.sh
</div>
<p>
调试技巧2:使用echo命令调试
</p>
</div>
<div>
<div class="cnblogs_code">
<pre>在变量读取或修改的前后加入echo $变量,也可在后面使用exit退出脚本,这样可以不用注释后面的代码</pre>
</div>
<p>
调试技巧3:sh -x 脚本 ==》全局调试
</p>
</div>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">sh</span> -x scripts.<span style="color: #0000ff;">sh</span></pre>
</div>
<p>
调试技巧4:局部调试
</p>
</div>
<div>
<div class="cnblogs_code">
<pre>set -<span style="color: #000000;">x
要调试的脚本内容 set +x
</div>
</div>
<h3>
<span id="182_Shell">1.8.2 Shell调试技巧小结</span>
</h3>
<p>
①要记得首先用<span style="background-color: #ffff00;">dos2unix</span>对脚本格式化。
</p>
<p>
②直接执行脚本根据报错来调试,有时报错不准确。
</p>
<p>
③<span style="background-color: #ffff00;">sh -x</span>调试整个脚本,显示执行过程。
</p>
<p>
④<span style="background-color: #ffff00;">set -x和set +x</span>调试部分脚本(在脚本中设置)
</p>
<p>
⑤<span style="background-color: #ffff00;">echo</span>输出变量及相关内容,然后紧跟着exit退出,不执行后面程序的方式,一步步跟踪脚本,对于逻辑错误比较好用。
</p>
<p>
写法: <span class="cnblogs_code"><span style="color: #0000ff;">echo</span> $var;exit</span>
</p>
<p>
⑥最关键的是语法熟练、编码习惯、编程思想,将错误扼杀在萌芽之中,减轻调试负担,提高效率。
</p>
<h2>
<span id="19_shell">1.9 shell编程练习题</span>
</h2>
<h3>
<span id="191_1shell">1.9.1 【练习题1】shell实现抓阄程序</span>
</h3>
<p>
需要使用shell编写一个抓阄的程序:
</p>
<p>
<em><span style="text-decoration: underline;">要求:</span></em>
</p>
<p>
1、执行脚本后,输入英文名字全拼,产生随机数01-99之间的数字,数字越大评分就去高,前面已经抓到的数字,下次不能在出现相同数字。
</p>
<p>
2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别人输入。
</p>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('4e5024e2-00ba-4491-b548-559fa4bdc44d')">
<img id="code_img_closed_4e5024e2-00ba-4491-b548-559fa4bdc44d" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_4e5024e2-00ba-4491-b548-559fa4bdc44d" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('4e5024e2-00ba-4491-b548-559fa4bdc44d',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_4e5024e2-00ba-4491-b548-559fa4bdc44d" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]# <span style="color: #0000ff;">cat</span> zhuajiu.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: zhuajiu.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-10 15:21:27
9 # Description:
10 #############################################################
11 Randow_Temp=/tmp/randow.test
12 name_Out_File1=/tmp/name_Out_File1.test
13 name_Out_File2=/tmp/name_Out_File2.test
14
15 >$name_Out_File1
16 >$name_Out_File2
17 >$Randow_Temp
18
19 trap ’echo “请勿使用 Ctrl+c”’ 2
20
21 Randow() {
22 sum=<span style="color: #0000ff;">echo</span> $RANDOM |<span style="color: #0000ff;">cut</span> -c-<span style="color: #800080;">2</span><span style="color: #000000;">
23 grep $sum $Randow_Temp
24 if [ $? -eq ];then
25 Randow
26 else
27 echo $sum »$Randow_Temp
28 daxiao=$sum
29 fi
30 }
31
32 Print() {
33 read -p "请输入名字的拼音:" Name
34 Randow
35 echo $Name $daxiao »$name_Out_File
36 }
37
38 while true
39 do
40 clear
41 echo ""
42 echo -e "\033[32m 这个程序会将随机数字排名前三的同学显示出来!\033[0m"
43 echo -e "\033[31m 退出脚本请使用 ’exit’ \033[0m"
44 echo ""
45 head -4 $name_Out_File2
46 read -p "请输入名字的拼音:" "Name"
47 if [ "$Name" == exit ]
48 then
49 exit
50 fi
51 Randow
52 echo $daxiao $Name »$name_Out_File1
53 echo "随机数最大的三位同学是:" >$name_Out_File2
54 sort -k1 -r $name_Out_File1 |column -t »$name_Out_File2
55 clear
56 done
</div>
<p>
<span class="cnblogs_code_collapse">View Code 抓阄程序 脚本</span></div>
</div>
<p>
脚本执行过程
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212155426379-987435185.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<h3>
<span id="192_29x9">1.9.2 【练习题2】输出9x9 乘法表</span>
</h3>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('636b9275-9dcc-4e5b-accf-0d4e96c72b76')">
<img id="code_img_closed_636b9275-9dcc-4e5b-accf-0d4e96c72b76" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_636b9275-9dcc-4e5b-accf-0d4e96c72b76" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('636b9275-9dcc-4e5b-accf-0d4e96c72b76',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_636b9275-9dcc-4e5b-accf-0d4e96c72b76" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]# <span style="color: #0000ff;">cat</span> cfb.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: cfb.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-11 15:40:03
9 # Description:
10 #############################################################
11 for a in <span style="color: #0000ff;">seq</span> <span style="color: #800080;">1</span> <span style="color: #800080;">9</span><span style="color: #000000;">
12 do
13 for b in <span style="color: #0000ff;">seq</span> <span style="color: #800080;">1</span> <span style="color: #800080;">9</span><span style="color: #000000;">
14 do
15 if [ $a -ge $b ]
16 then
17 echo -en "$a x $b = $(expr $a * $b) "
18
19 fi
20 done
21 echo " "
22 done
</div>
<p>
<span class="cnblogs_code_collapse">View Code 九九乘法表</span></div>
</div>
<p>
执行结果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# <span style="color: #0000ff;">sh</span> cfb.<span style="color: #0000ff;">sh</span>
1 x 1 = 1
2 x 1 = 2 2 x 2 = 4
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25
6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49
8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
</div>
</div>
<h3>
<span id="193_3dDOS">1.9.3 【练习题3】解决dDOS攻击生产案例</span>
</h3>
<p>
写一个Shell脚本解决DOS攻击生产案例。
</p>
<p>
请根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100(读者根据实际情况设定),即调用防火墙命令封掉对应的IP。防火墙命令为:iptables-I INPUT -s IP地址 -j DROP。
</p>
<p>
练习使用日志下载地址:<em>https://files.cnblogs.com/files/clsn/access-web-log.zip</em>
</p>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('53e446a8-8397-40a4-bf4d-2e9f6f9628fc')">
<img id="code_img_closed_53e446a8-8397-40a4-bf4d-2e9f6f9628fc" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_53e446a8-8397-40a4-bf4d-2e9f6f9628fc" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('53e446a8-8397-40a4-bf4d-2e9f6f9628fc',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_53e446a8-8397-40a4-bf4d-2e9f6f9628fc" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn <span style="color: #0000ff;">while</span>]# <span style="color: #0000ff;">cat</span> ddos_check.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: ddos_check.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-10 12:34:15
9 # Description:
10 #############################################################
11
12 Info_File=/tmp/ddos_check.log
13
14 #从连接数获取
15 #netstat -lant|awk -F "[ :]+" ’/180:80/{clsn[$6]++}END{for(pol in clsn)print pol,clsn[pol]}’ >$Info_File
16
17 # 从日志获取
18 awk ’{hotel[$1]++}END{for(pol in hotel)print pol,hotel[pol]}’ access.log|sort -nk2 -r >$Info_File
19
20 while read line
21 do
22 Ip_Add=<span style="color: #0000ff;">echo</span> $line |<span style="color: #0000ff;">awk</span> <span style="color: #800000;">'</span><span style="color: #800000;">{print $1}</span><span style="color: #800000;">'</span><span style="color: #000000;">
23 Access=<span style="color: #0000ff;">echo</span> $line |<span style="color: #0000ff;">awk</span> <span style="color: #800000;">'</span><span style="color: #800000;">{print $2}</span><span style="color: #800000;">'</span><span style="color: #000000;">
24 if [ $Access -ge 10000 ]
25 then
26 #echo $Ip_Add
27 iptables -I INPUT -s $Ip_Add -j DROP
28 fi
29 done <$Info_File
</div>
<p>
<span class="cnblogs_code_collapse">View Code 解决dDOS攻击脚本</span></div>
</div>
<p>
脚本执行结果:
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #0000ff;">while</span>]# iptables -<span style="color: #000000;">L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all – 112.64.171.98 anywhere
DROP all – 58.220.223.62 anywhere
Chain FORWARD (policy ACCEPT) target prot opt source destination
Chain OUTPUT (policy ACCEPT) target prot opt source destination
</div>
</div>
<p>
其他方法进行日志分析
</p>
<div>
<p class="a4">
elk日志分析
</p>
<p class="a4">
http://blog.oldboyedu.com/elk/
</p>
<p class="a4">
nginx-WAF
</p>
<p class="a4">
http://blog.oldboyedu.com/nginx-waf/
</p>
</div>
<h3>
<span id="194_4">1.9.4 【练习题4】开发脚本入侵检测与报警案例(入侵检测系统)</span>
</h3>
<p>
监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次。
</p>
<p>
IDS是英文“Intrusion Detection Systems”的缩写,中文意思是“入侵检测系统”。
</p>
<p>
脚本内容
</p>
<p>
<strong>第一个里程碑:生成md5</strong><strong>验证文件</strong>
</p>
<p>
<em><span style="text-decoration: underline;">文件的校验文件</span></em>
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">find</span> /var/html/www<span style="color: #008000;">/*</span><span style="color: #008000;"> -type f |xargs md5sum >/tmp/check/web_file_check.md5sum</span></pre>
</div>
</div>
<p>
<span style="text-decoration: underline;">目录的校验文件</span>
</p>
<div>
<div class="cnblogs_code">
<pre>tree /var/html/www/ -d >/tmp/check/<span style="color: #000000;">web_dir_check.txt
md5sum /tmp/check/web_dir_check.txt md5sum /tmp/check/web_dir_check.txt >/tmp/check/web_dir_check.md5sum
</div>
</div>
<p>
脚本内容
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('42f158a6-ab3f-44f4-986c-316edba21921')">
<img id="code_img_closed_42f158a6-ab3f-44f4-986c-316edba21921" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-9.gif" alt="Shell编程进阶篇(完结)" alt="" /><img id="code_img_opened_42f158a6-ab3f-44f4-986c-316edba21921" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('42f158a6-ab3f-44f4-986c-316edba21921',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-9.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" /></p>
<div id="cnblogs_code_open_42f158a6-ab3f-44f4-986c-316edba21921" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn check]# <span style="color: #0000ff;">cat</span> web_file_check.<span style="color: #0000ff;">sh</span>
2 #!/bin/bash
3 #############################################################
4 # File Name: web_file_check.sh
5 # Version: V1.
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-10 14:15:34
9 # Description:
10 #############################################################
11
12 Url1=http://10.0.0.1/web_file_check.md5sum
13 Url2=http://10.0.0.1/web_dir_check.md5sum
14 Dir=/tmp/check/
15 Web_Dir=/var/html/www/
16 Check_File1=/tmp/check/web_file_check.md5sum
17 Check_File2=/tmp/check/web_dir_check.md5sum
18 Check_Dir=/tmp/check/web_dir_check.txt
19 Check_Out=/tmp/check/check_out.md5sum
20 Mail_info=/tmp/check/mail.txt
21 Date=<span style="color: #0000ff;">date</span> +%F_%<span style="color: #000000;">T
22 Host_Name=<span style="color: #0000ff;">hostname</span><span style="color: #000000;">
23 Host_IP=<span style="color: #0000ff;">hostname</span> -<span style="color: #000000;">I
24 Email_Addr=995701749@qq.com
25
26 [ -d $Dir ] || mkdir -p $Dir
27 if [ ! -f $Check_File1 ]
28 then
29 cd $Dir &&
30 wget $Url1
31 elif [ ! -f $Check_File2 ]
32 then
33 cd $Dir &&
34 wget $Url2
35 fi
36
37 md5sum -c $Check_File1 >$Check_Out 2>/dev/null
38 Back_num1=$?
39 tree -d $Web_Dir >$Check_Dir
40 md5sum -c $Check_File2 &>/dev/null
41 Back_num2=$?
42 if [ $Back_num1 -ne ]
43 then
44 echo "发生主机:$Host_Name 主机IP地址:$Host_IP" > $Mail_info
45 echo "在 $Date 的检测中发现以下文件被篡改" »$Mail_info
46 echo "==================================================" »$Mail_info
47 egrep -i "失败|failed" $Check_Out »$Mail_info
48 echo "==================================================" »$Mail_info
49 echo "请尽快登陆服务器进行处理!!!" »$Mail_info
50 mail -s "【警告】web站点文件被篡改" -a $Check_File1 $Email_Addr <$Mail_info
51 fi
52
53 if [ $Back_num2 -ne ]
54 then
55 echo "目录检测信息" >$Mail_info
56 echo "在 $Date 的检测中发现目录被篡改" »$Mail_info
57 mail -s "【警告】web站点目录被篡改" -a $Check_Dir $Email_Addr<$Check_Dir
58 fi
</div>
<p>
<span class="cnblogs_code_collapse">View Code 入侵检测与报警</span></div>
</div>
<p>
修改文件目录后执行结果
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212155716222-2020673909.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212155737019-225058576.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212155744769-240256875.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<h3>
<span id="195_5">1.9.5 【练习题5】单词及字母去重排序案例</span>
</h3>
<p>
用shell处理以下内容
</p>
<div>
<p class="a4">
1、按单词出现频率降序排序!
</p>
<p class="a4">
2、按字母出现频率降序排序!
</p>
<div class="cnblogs_code">
<pre>the squid project provides a number ofresources to assist users design implement and support squid installations.Please browse the documentation and support sections <span style="color: #0000ff;">for</span> <span style="color: #0000ff;">more</span> infomation byoldboy training</pre>
</div>
</div>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn play]# <span style="color: #0000ff;">cat</span> abc.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: abc.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-11 17:23:19
Description:
#############################################################
a="the squid project provides a number ofresources to assist users design implement and support squid installations.Please browse the documentation and support sections for more infomation byoldboy training"
echo "按单词出现频率降序排序!"
for i in $a
do
echo $i
done|
sort |uniq -c|sort -nk1 -r
echo "按字母出现频率降序排序!"
echo $a |grep -o "[a-z]" |sort|uniq -c |sort -nk1 -r
</div>
</div>
<h3>
<span id="196_6">1.9.6 【练习题6】编写正(或长)方形图形案例</span>
</h3>
<p>
请用shell或Python编写一个正(或长)方形,接收用户输入的数字。
</p>
<p>
shell脚本内容
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn play]# <span style="color: #0000ff;">cat</span> zhengfangxing.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: zhengfangxing.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-11 17:33:33
Description:
#############################################################
trap "echo 输入exit退出" 2
while true
do
read -p "你想看多大的正方形:" a
[ "$a" == "exit" ] && exit
expr 1 + $a &>/dev/null
[ $? -ne ] && echo "请输入一个数字!" && exit 2
b="■ "
d=$(for i in eval <span style="color: #0000ff;">echo</span> {<span style="color: #800080;">1</span>..$a}
;do echo -n $b; echo -n " ";done)
</span><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> `eval <span style="color: #0000ff;">echo</span> {<span style="color: #800080;">1</span><span style="color: #000000;">..$a}`
</span><span style="color: #0000ff;">do</span>
<span style="color: #0000ff;">echo</span> <span style="color: #800000;">"</span><span style="color: #800000;">$d</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">done</span>
done
</div>
</div>
<p>
脚本执行效果
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn play]# <span style="color: #0000ff;">sh</span> zhengfangxing.<span style="color: #0000ff;">sh</span> <span style="color: #800080;">4</span><span style="color: #000000;">
■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ [root@clsn play]# sh zhengfangxing.sh 3 ■ ■ ■ ■ ■ ■ ■ ■ ■
</div>
</div>
<h2>
<span id="110">1.10 各种语句小结</span>
</h2>
<p>
? while循环的特长是执行守护进程以及我们希望循环不退出持续执行,用于频率小于1分钟循环处理(crond),其他的while循环几乎都可以被for循环替代。
</p>
<p>
? case语句可以被if语句替换,一般在系统启动脚本传入少量固定规则字符串用case语句,其他普通判断多用if。
</p>
<p>
? 一句话,if,for语句最常用,其次while(守护进程),case(服务启动脚本)。
</p>
<h2>
<span id="111">1.11 附录</span>
</h2>
<h3>
<span id="1111">1.11.1 格式化文本--对齐文本</span>
</h3>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# column --<span style="color: #000000;">help
用法: column [选项] [文件 …]
选项: -c, –columns <宽度> 输出宽度(字符数) -t, –table 创建表格 -s, –separator <字符串> 可用的表格分隔符 -o, –output-separator <字符串> 表格输出列分隔符,默认为两个空格 -x, –fillrows 先填充行,再填充列
-h, –help 显示此帮助并退出 -V, –version 输出版本信息并退出
更多信息请参阅 column(1)。
</div>
</div>
<h3>
<span id="1112">1.11.2 服务器被挂马怎么办?</span>
</h3>
<ol>
<li>
将被挂马服务器上,原有重要数据备份下来
</li>
<li>
仔细筛查数据,确保其中都是正常文件
</li>
<li>
重新安装服务器操作系统,并针对性的加强安全防护。
</li>
<li>
<strong>切记</strong>,服务器中木马后必须重新安装操作系统。
</li>
</ol>
<h3>
<span id="1113">1.11.3 怎么通过进程找到程序</span>
</h3>
<p>
通过进程找到pid号码
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn proc]# <span style="color: #0000ff;">ps</span> -ef |<span style="color: #0000ff;">grep</span><span style="color: #000000;"> sshd
root 1294 1 09:18 ? 00:00:00 /usr/sbin/sshd -D
</div>
</div>
<p>
通过pid找到在proc目录下的临时文件夹
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn proc]# cd /proc/<span style="color: #800080;">1294</span><span style="color: #000000;">
[root@clsn 1294]#
</div>
</div>
<p>
查看其中的exe文件,即可发现程序文件的路径
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn <span style="color: #800080;">1294</span><span style="color: #000000;">]# ll exe
lrwxrwxrwx 1 root root 12月 11 09:18 exe -> /usr/sbin/sshd
</div>
</div>
<h3>
<span id="1114_screen">1.11.4 screen程序 防止脚本执行中断</span>
</h3>
<p>
用操作,更多详情参照 <a href="/wp-content/themes/clsn-003/inc/go.php?url=http://www.cnblogs.com/clsn/p/8022625.html" target="_blank">http://www.cnblogs.com/clsn/p/8022625.html</a>
</p>
<div>
<div class="cnblogs_code">
<pre>ctrl +a +<span style="color: #000000;">d 退出当前终端,返回加载screen前的shell命令状态
screen -ls 可看screen会话 screen -r id 指定进入哪个screen会话
</div>
</div>
<p>
screen进程原理
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn ~]# <span style="color: #0000ff;">ps</span> -ef |<span style="color: #0000ff;">grep</span> <span style="color: #0000ff;">ping</span><span style="color: #000000;">
root 30170 30153 11:57 pts/5 00:00:00 ping 10.0.0.254 root 30172 30078 11:58 pts/ 00:00:00 grep –color=auto ping [root@clsn ~]# ps -ef |grep 30170 root 30170 30153 11:57 pts/5 00:00:00 ping 10.0.0.254 root 30174 30078 11:59 pts/ 00:00:00 grep –color=auto 30170 [root@clsn ~]# ps -ef |grep 30153 root 30153 30119 11:57 pts/5 00:00:00 /bin/bash root 30170 30153 11:57 pts/5 00:00:00 ping 10.0.0.254 root 30176 30078 11:59 pts/ 00:00:00 grep –color=auto 30153 [root@clsn ~]# ps -ef |grep 30119 root 30119 1 11:56 ? 00:00:00 SCREEN root 30120 30119 11:56 pts/4 00:00:00 /bin/bash root 30153 30119 11:57 pts/5 00:00:00 /bin/bash root 30178 30078 11:59 pts/ 00:00:00 grep –color=auto 30119
</div>
</div>
<h3>
<span id="1115_shell">1.11.5 shell中实现倒计时效果</span>
</h3>
<p>
脚本内容
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]# <span style="color: #0000ff;">cat</span> daojishi.<span style="color: #0000ff;">sh</span><span style="color: #000000;">
#!/bin/bash #############################################################
File Name: daojishi.sh
Version: V1.
Author: clsn
Organization: http://blog.znix.top
Created Time : 2017-12-12 08:49:11
Description:
#############################################################
for i in <span style="color: #0000ff;">seq</span> -<span style="color: #0000ff;">w</span> <span style="color: #800080;">10</span> -<span style="color: #800080;">1</span> <span style="color: #800080;">1</span><span style="color: #000000;">
do
echo -ne "\b\b$i";
sleep 1;
done
</div>
</div>
<p>
脚本执行效果
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171212160059894-1879805377.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程进阶篇(完结)" alt="" />
</p>
<h2>
<span id="112_linux">1.12 linux中的信号</span>
</h2>
<h3>
<span id="1121_Linux">1.12.1 Linux系统的重要信号及说明</span>
</h3>
<table class="MsoTable15Grid4Accent1" style="width: 100%; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width: 13.52%; border-top-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-top-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left-color: #5b9bd5; border-right: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 5;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">信</span></strong><strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">号</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; border-top-color: #5b9bd5; border-right-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">说</span></strong><strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">明</span></strong>
</p>
</td>
</tr>
<tr>
<td style="width: 13.52%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 68;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">HUP(l)</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">挂起,通常因终端掉线或用户退出而引发</span>
</p>
</td>
</tr>
<tr>
<td style="width: 13.52%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 4;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">INT(2)</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">中断,通常因按下</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">Ctrl+c</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">组合键而引发</span>
</p>
</td>
</tr>
<tr>
<td style="width: 13.52%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 68;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">QUIT(3)</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">退出,通常因按下</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">Ctrl+\</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">组合键而引发</span>
</p>
</td>
</tr>
<tr>
<td style="width: 13.52%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 4;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">ABRT(6)</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">中止,通常因某些严重的执行错误而引发</span>
</p>
</td>
</tr>
<tr>
<td style="width: 13.52%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 68;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">ALRM(14)</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">报警,通常用来处理超时</span>
</p>
</td>
</tr>
<tr>
<td style="width: 13.52%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 4;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">TERM(15)</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">终止,通常在系统关机时发送</span>
</p>
</td>
</tr>
<tr>
<td style="width: 13.52%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="13%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 68;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">TSTP(20)</span></strong>
</p>
</td>
<td style="width: 86.48%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="86%">
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">停止进程的运行,但该信号可以被处理和忽略,通常因按下</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">Ctrl+z</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">组合键而引发</span>
</p>
</td>
</tr>
</table>
<p>
<span style="font-size: 1.17em;"> 使用trap控制信号</span>通常需要忽略的信号包括HUP、INT、QUIT、TSTP、TERM等,对应的信号编号分别为1、2、3、20、15。Shell脚本中既可以用数字来代表信号,也可以使用信号的名字来代表信号
</p>
<p>
<strong><span style="font-size: 1.17em; font-family: 微软雅黑, sans-serif;">1.12.2使用trap控制信号</span></strong>
</p>
<p>
trap命令用于指定在接收到信号后将要采取的行动,信号的相关说明前面已经提到 过。trap命令的一种常见用途是在脚本程序被中断时完成清理工作,或者屏蔽用户非法 使用的某些信号。在使用信号名时需要省略SIG前缀。可以在命令提示符下输人命令 trap -1来查看信号的编号及其关联的名称。
</p>
<p>
trap命令的参数分为两部分,前一部分是接收到指定信号时将要采取的行动,后一部分是要处理的信号名。
</p>
<p>
trap命令的使用语法如下:
</p>
<div>
<div class="cnblogs_code">
<pre>trap command signal</pre>
</div>
</div>
<p>
signal是指接收到的信号,command是指接收到该信号应采取的行动。也就是:
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">trap ‘命令;命令’ 信号编号
或 trap ‘命令;命令’ 信号名
</div>
</div>
<div class="cnblogs_code">
<pre>[root@clsn ~]# trap <span style="color: #800000;">'</span><span style="color: #800000;">echo clsn</span><span style="color: #800000;">'</span> <span style="color: #800080;">2</span><span style="color: #000000;">
[root@clsn ~]# ^Cclsn
</div>
<h2>
<span id="113">1.13 参考文档</span>
</h2>
<div>
<div class="cnblogs_code">
<pre>http:<span style="color: #008000;">//</span><span style="color: #008000;">blog.csdn.net/zhangna20151015/article/details/50293987</span>
https://zh.wikipedia.org/wiki/For迴圈#傳統的_for_迴圈_for-loops https://zh.wikipedia.org/wiki/While迴圈 https://www.cnblogs.com/kerrycode/p/6537175.html (生产随机数) http://blog.51cto.com/lidao/1936495 (不循环,批量创建用户) http://www.runoob.com/linux/linux-shell-func.html 函数
</div>
<p>
</p>
</div>
<p>
</p>
<div id="toc_container" class="toc_white have_bullets">
<ul class="toc_list">
<li>
<a href="#11_for">1.1 for循环语句</a><ul>
<li>
<a href="#111_shellfor">1.1.1 shell中的for循环</a>
</li>
<li>
<a href="#112_For">1.1.2 不同语言的For循环</a>
</li>
</ul>
</li>
<li>
<a href="#12_for">1.2 for循环相关练习题</a><ul>
<li>
<a href="#121_1">1.2.1 【练习题1】批量生成随机字符文件名案例</a>
</li>
<li>
<a href="#122_2">1.2.2 【练习题2】批量改名特殊案例</a><ul>
<li>
<a href="#1221nbsp">1.2.2.1 批量改名其他方式</a>
</li>
</ul>
</li>
<li>
<a href="#123_3">1.2.3 【练习题3】批量创建特殊要求用户案例</a><ul>
<li>
<a href="#1231nbsp_shell">1.2.3.1 批量创建用户并设置随机密码(不使用shell循环)</a>
</li>
</ul>
</li>
<li>
<a href="#124_4">1.2.4 【练习题4】扫描网络内存活主机案例</a>
</li>
<li>
<a href="#125_5">1.2.5 【练习题5】筛选符合长度的单词案例</a>
</li>
<li>
<a href="#126_6RANDOM">1.2.6 【练习题6】破解RANDOM随机数案例</a>
</li>
<li>
<a href="#127_7">1.2.7 【练习题7】博客园博文爬虫案例</a><ul>
<li>
<a href="#1271nbsp_51CTO">1.2.7.1 51CTO博客爬虫案例</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#13_while">1.3 while循环语句</a><ul>
<li>
<a href="#131_shellwhile">1.3.1 shell中while语法</a>
</li>
<li>
<a href="#132_while">1.3.2 while 使用场景</a>
</li>
<li>
<a href="#133_while">1.3.3 while 作用</a>
</li>
</ul>
</li>
<li>
<a href="#14">1.4 获取取文件中的行,单词和字符</a><ul>
<li>
<a href="#141">1.4.1 迭代获取文件中的每一行</a>
</li>
<li>
<a href="#142">1.4.2 迭代获取每一个单词</a>
</li>
<li>
<a href="#143">1.4.3 迭代获取每一个字符</a>
</li>
<li>
<a href="#144">1.4.4 同时获取取文件中的行,单词和字符脚本</a>
</li>
<li>
<a href="#145_eval">1.4.5 eval 命令用法</a>
</li>
</ul>
</li>
<li>
<a href="#15_break_continue_exit_return">1.5 break continue exit return</a><ul>
<li>
<a href="#151_break">1.5.1 break命令说明</a>
</li>
<li>
<a href="#152_continue">1.5.2 continue命令说明</a>
</li>
<li>
<a href="#153_exit">1.5.3 exit命令说明</a>
</li>
<li>
<a href="#154_return">1.5.4 return命令说明</a>
</li>
</ul>
</li>
<li>
<a href="#16_shell">1.6 shell中的数组</a><ul>
<li>
<a href="#161_Shell">1.6.1 为什么会产生Shell数组</a>
</li>
<li>
<a href="#162_Shell">1.6.2 什么是Shell数组</a>
</li>
<li>
<a href="#163">1.6.3 数组中的增删改查</a>
</li>
<li>
<a href="#164">1.6.4 将命令的结果赋值给数组</a>
</li>
<li>
<a href="#161">1.6.1 数组定义格式</a>
</li>
<li>
<a href="#162">1.6.2 数组的本质就是一个变量,只是这个变量存了多个值</a>
</li>
<li>
<a href="#163-2">1.6.3 【练习题】批量检查多个网站地址是否正常</a>
</li>
</ul>
</li>
<li>
<a href="#17_Shell">1.7 Shell 函数</a><ul>
<li>
<a href="#171">1.7.1 使用函数的优势</a>
</li>
<li>
<a href="#172">1.7.2 定义函数</a>
</li>
<li>
<a href="#173">1.7.3 引用函数</a>
</li>
<li>
<a href="#174">1.7.4 函数传参</a>
</li>
<li>
<a href="#175_0">1.7.5 $0 永远的脚本名称</a>
</li>
<li>
<a href="#176_return">1.7.6 函数中return的用法</a>
</li>
<li>
<a href="#177">1.7.7 自定义常用函数库</a>
</li>
</ul>
</li>
<li>
<a href="#18_shell">1.8 shell脚本的调试</a><ul>
<li>
<a href="#181">1.8.1 脚本调试技巧</a>
</li>
<li>
<a href="#182_Shell">1.8.2 Shell调试技巧小结</a>
</li>
</ul>
</li>
<li>
<a href="#19_shell">1.9 shell编程练习题</a><ul>
<li>
<a href="#191_1shell">1.9.1 【练习题1】shell实现抓阄程序</a>
</li>
<li>
<a href="#192_29x9">1.9.2 【练习题2】输出9x9 乘法表</a>
</li>
<li>
<a href="#193_3dDOS">1.9.3 【练习题3】解决dDOS攻击生产案例</a>
</li>
<li>
<a href="#194_4">1.9.4 【练习题4】开发脚本入侵检测与报警案例(入侵检测系统)</a>
</li>
<li>
<a href="#195_5">1.9.5 【练习题5】单词及字母去重排序案例</a>
</li>
<li>
<a href="#196_6">1.9.6 【练习题6】编写正(或长)方形图形案例</a>
</li>
</ul>
</li>
<li>
<a href="#110">1.10 各种语句小结</a>
</li>
<li>
<a href="#111">1.11 附录</a><ul>
<li>
<a href="#1111">1.11.1 格式化文本--对齐文本</a>
</li>
<li>
<a href="#1112">1.11.2 服务器被挂马怎么办?</a>
</li>
<li>
<a href="#1113">1.11.3 怎么通过进程找到程序</a>
</li>
<li>
<a href="#1114_screen">1.11.4 screen程序 防止脚本执行中断</a>
</li>
<li>
<a href="#1115_shell">1.11.5 shell中实现倒计时效果</a>
</li>
</ul>
</li>
<li>
<a href="#112_linux">1.12 linux中的信号</a><ul>
<li>
<a href="#1121_Linux">1.12.1 Linux系统的重要信号及说明</a>
</li>
</ul>
</li>
<li>
<a href="#113">1.13 参考文档</a>
</li>
</ul>
</div>
- 原文作者:惨绿少年
- 原文链接:https://clsn.io/clsn/lx466.html
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。