SHELL DAY07
1)执行脚本后打开firefox浏览器,访问http://server0.example.com/
2)如果firefox窗口被关闭,自动重启步骤1
提示:
!!!! 使用 while true 死循环,调用 firefox URL 操作
-
#!/bin/bash
-
while
true
-
do
-
firefox http://192.168.122.37
-
done
2. 编写序列用户管理脚本
1)编写脚本 uadd.sh,能够批量添加用户 ceshi01、ceshi02、……、ceshi20
2)编写脚本 udel.sh,能够批量删除用户 ceshi01、ceshi02、……、ceshi20
提示:
!!!! 编号小于10的用户的名称前缀为 ceshi0,其余用户的前缀为 ceshi
!!!! 也可以用 seq -w 1 20 生成等宽数列
-
#!/bin/bash
-
for i
in $(seq -w 1 20)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">do</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> useradd ceshi<span class="hljs-variable"><span class="hljs-variable">$i
-
done
3. 编写脚本 tc.sh,
1)找出/usr/share/doc/下的所有文件
2)如果文件的类型是text,则wc -l统计此文件的行数,显示“文件 xxx,一共 nn 行”
3)最后显示“一共统计了 nn 个文件,累计总行数 nn 行”
提示:
!!!! 使用find命令列出所有目标文件
!!!! 通过“file 文件路径…”检测文件类型,看结果是否出现 text 以便筛选
-
#!/bin/bash
-
#记录文件总数的变量
-
fileNum=0
-
#当前文件的行数
-
currentFileRow=0
-
#所有文件的行数
-
totalRow=0
-
for file
in $(find /usr/share/doc/ -<span class="hljs-built_in"><span class="hljs-built_in">type</span></span> f)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">do</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> file <span class="hljs-variable"><span class="hljs-variable">$file | egrep -w
'text'
-
if [ $? -eq 0 ];<span class="hljs-keyword"><span class="hljs-keyword">then</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> currentFileRow=$(cat
$file</span></span> | wc -l)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"文本文件</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$file
,一共
$currentFileRow</span></span></span><span class="hljs-string"> 行"</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in"><span class="hljs-built_in">let</span></span> fileNum++</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in"><span class="hljs-built_in">let</span></span> totalRow+=<span class="hljs-variable"><span class="hljs-variable">$currentFileRow
-
fi
-
done
-
echo
"一共统计了$fileNum</span></span></span><span class="hljs-string"> 个文件,累计总行数:</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$totalRow"
4. 编写脚本 /root/vhost_add,为server0快速添加新的虚拟Web主机
1)虚拟主机的域名由脚本在执行时指定(./vhost_add www0.example.com),若未指定域名,则提示正确用法后退出脚本
2)如果指定FQDN的虚拟主机已经存在,提示“虚拟主机XX已存在”,退出脚本
3)执行脚本后能够向 /etc/httpd/conf.d/vhosts.conf 添加新配置
4)并为此虚拟机建立网页目录 /var/www/FQDN,首页内容为虚拟主机的域名
5)在本机 /etc/hosts 文件末尾追加“172.25.0.11 虚拟Web主机域名”记录
提示:
!!!! 需提前准备好 httpd 服务器
!!!! 通过检查 vhosts.conf 文件是否存在’.*ServerName.*域名.*”记录判断是否存在
!!!! 追加记录用echo ”文件内容” >> 文件路径
-
#!/bin/bash
-
if [ $# -eq 0 ];then</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> echo "Usage: $0 指定域名"
-
exit 1
-
fi
-
#判断/etc/httpd/conf.d/vhosts.conf是否存在,不存在则创建
-
if [ ! -e "/etc/httpd/conf.d/vhosts.conf" ];then
-
touch /etc/httpd/conf.d/vhosts.conf
-
fi
-
#检测虚拟主机是否添加过
-
egrep ".*ServerName.*$1.*" /etc/httpd/conf.d/vhosts.conf</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">if [ $? -eq 0 ];then
-
echo "虚拟主机 $1 已存在"</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> exit 2</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">fi</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">#截取域名以.分割的第一部分</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">firstDomain=$(echo $1 | cut -d. -f1)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">#为虚拟主机的访问路径添加主页文件</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">if [ ! -e "/var/www/$firstDomain" ];then
-
mkdir /var/www/$firstDomain</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">fi</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">cd /var/www/$firstDomain
-
echo $1 > ./index.html</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">#新建虚拟Web主机</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">echo "<span class="hljs-tag"><<span class="hljs-name">VirtualHost</span> *<span class="hljs-attr">:80</span>></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">ServerName $1
-
DocumentRoot /var/www/$firstDomain</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="28"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-tag"></<span class="hljs-name">VirtualHost</span>></span>" >> /etc/httpd/conf.d/vhosts.conf</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="29"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">#启动httpd服务并设置为开机自启</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="30"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">systemctl restart httpd.service</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="31"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">systemctl enable httpd.service</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">#在本机追加域名解析记录</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">echo "192.168.122.37 $1" >> /etc/hosts
-
-
#访问Desktop的虚拟web
-
firefox $1</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre><br>5. 为指定的一批计算机远程部署XX文件<br>1. 需要部署的文件从网址 http://sever0.example.cm/a.html 获取<br>2. 部署的目标位置 /tmp/a.html<br>3. 目标计算机的IP地址存放在文件 ipadds.txt 中(172.25.0.11、172.25.0.10)<br>4. 用于认证的身份信息:用户名 root、密码 redhat<br><pre οnclick="hljs.copyCode(event)"><code class="language-perl hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment"><span class="hljs-comment">#/bin/bash</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">for</span></span> ip in $(cat /root/shell/Exercise01/ipTexts.txt)
-
do
-
expect << EOF
-
spawn ssh -o StrictHostKeyChecking=
no root@$ip</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">expect <span class="hljs-string"><span class="hljs-string">"password"</span></span> {<span class="hljs-keyword"><span class="hljs-keyword">send</span></span> <span class="hljs-string"><span class="hljs-string">"redhat\r"</span></span>}</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">expect <span class="hljs-string"><span class="hljs-string">"#"</span></span> {<span class="hljs-keyword"><span class="hljs-keyword">send</span></span> <span class="hljs-string"><span class="hljs-string">"wget http://192.168.122.37/a.html -O /tmp/a.html\r"</span></span>}</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">expect <span class="hljs-string"><span class="hljs-string">"#"</span></span> {<span class="hljs-keyword"><span class="hljs-keyword">send</span></span> <span class="hljs-string"><span class="hljs-string">"exit\r"</span></span>}</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">expect <span class="hljs-string"><span class="hljs-string">"eof"</span></span> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">EOF</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">done</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>6. 正则表示式操作练习<br>1)新建测试文件 a.txt,内容如下<br>Tarena IT Group. @2018-2028<br>@ IN NS mail.tedu.cn<br>bt bat bet boot booot keroot root rooty<br>52:54:00:90:A7:A2<br>1234567@qq.com<br>192.168.4.5 3.1415926<br>1.2.3.4.5 No:6.7.8.9<br>hunter@tedu.cn<br>mike:x:500:500::/home/mike:/bin/bash<br>2)找出以b开始、t结尾,中间包含2~3个字母的字符串<br># egrep -o 'b[a-z]{2,3}t' a.txt<br>3)找出包括大写字母的行、找出只包括数字或.的行<br># egrep [A-Z] a.txt<br># egrep -v [^0-9.] a.txt<br>4)找出正确的电子邮箱地址<br># egrep '[a-Z0-9_]{2,}@([a-Z0-9]+\.){1,3}[a-Z0-9]{2,}' a.txt<br>5)找出以m结尾包括3个字符的独立字符串<br># egrep '\b..m\b' a.txt<br>6)找出以数字开头、数字结尾的行<br># egrep '^[0-9].*[0-9]$' a.txt
7)找出每行的第3个字符
# egrep -o '^(.)(.)(.)' a.txt | cut -b 3
8)找出每行的倒数第3个字符
egrep -o '(.)(.)(.)$' a.txt | cut -b 1<br></div><div style="text-align:left;"><br></div>案例1:编写一键部署软件脚本<br>案例2:启动脚本<br>案例3:编写监控脚本<br>案例4:编写安全检测脚本<br>案例5:编写进度显示脚本<p></p><a style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;" target="_blank"></a><span style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"></span><h2 style="font-size:20px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t1"></a>1 案例1:编写一键部署软件脚本</h2><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t2"></a>1.1 问题</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">本案例要求编写脚本实现一键部署Nginx软件(Web服务器):</p><ul class="list" style="margin-left:30px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><li>一键源码安装Nginx软件</li><li>脚本自动安装相关软件的依赖包</li><li>脚本自动判断yum是否可用</li></ul><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t3"></a>1.2 步骤</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">实现此案例需要按照如下步骤进行。</p><p class="number" style="text-indent:21pt;font-size:15px;font-weight:bold;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">步骤一:准备工作</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)判断yum源是否可用</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">通过yum repolist查看软件包数量是否大于0:</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>root@svr5 <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum repolist </li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>root@svr5 <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum repolist <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">|</span> awk <span class="sh_string" style="color:rgb(166,133,0);">'/repolist/{print $2}'</span> </li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>root@svr5 <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum repolist <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">|</span> awk <span class="sh_string" style="color:rgb(166,133,0);">'/repolist/{print $2}'</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">|</span>sed <span class="sh_string" style="color:rgb(166,133,0);">'s/,//'</span></li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>root@svr5 <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># N<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span>$
(yum repolist
| awk
'/repolist/{print $2}'</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">|</span>sed <span class="sh_string" style="color:rgb(166,133,0);">'s/,//'</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">)</span></li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>root@svr5 <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span> $N –le 0 ] && echo 'yum 不可用'
2)依赖包
源码安装Nginx需要提前安装依赖包软件gcc,openssl-devel,pcre-devel
步骤二:编写脚本
1)参考脚本内容如下:
-
#!/bin/bash
-
yumNum=$(yum repolist | awk <span class="hljs-string"><span class="hljs-string">'/^repolist/{print $2}'</span></span> | sed <span class="hljs-string"><span class="hljs-string">'s/,//'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">if</span></span> [<span class="hljs-variable"><span class="hljs-variable">$yumNum -le 0];
then
-
echo
"yum不可用"
-
exit 1
-
fi
-
yum -y install gcc openssl-devel pcre-devel
-
tar -xf nginx-1.12.2.tar.gz
-
cd nginx-1.12.2
-
./configure
-
make
-
make install
2)确认安装效果
Nginx默认安装路径为/usr/local/nginx,该目录下会提供4个子目录,分别如下:
/usr/local/nginx/conf 配置文件目录
/usr/local/nginx/html 网站页面目录
/usr/local/nginx/logs Nginx日志目录
/usr/local/nginx/sbin 主程序目录
主程序命令参数:
-
[root@svr5 ~]# /usr/
local/nginx/sbin/nginx
//启动服务
-
[root@svr5 ~]# /usr/
local/nginx/sbin/nginx -s stop
//关闭服务
-
[root@svr5 ~]# /usr/
local/nginx/sbin/nginx -V
//查看软件信息
2 案例2:启动脚本
2.1 问题
本案例要求编写Ngin启动脚本,要求如下:
- 脚本支持start、stop、restart、status
- 脚本支持报错提示
- 脚本具有判断是否已经开启或关闭的功能
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:编写脚本
脚本通过位置变量1读取用户的操作指令,判断是start、stop、restart还是status。</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)参考脚本内容如下</p><pre οnclick="hljs.copyCode(event)"><code class="language-bash hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta"><span class="hljs-meta">#!/bin/bash</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">yumNum= 1读取用户的操作指令,判断是start、stop、restart还是status。</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)参考脚本内容如下</p><pre οnclick="hljs.copyCode(event)"><code class="language-bash hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta"><span class="hljs-meta">#!/bin/bash</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">yumNum= (yum repolist | awk ’/^repolist/{print 2}'</span></span> | sed <span class="hljs-string"><span class="hljs-string">'s/,//'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">if</span></span> [<span class="hljs-variable"><span class="hljs-variable"> 2}'</span></span> | sed <span class="hljs-string"><span class="hljs-string">'s/,//'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">if</span></span> [<span class="hljs-variable"><span class="hljs-variable"> yumNum -le 0];then
-
echo ”yum不可用”
-
exit 1
-
fi
-
yum -y install gcc openssl-devel pcre-devel
-
tar -xf nginx-1.12.2.tar.gz
-
cd nginx-1.12.2
-
./configure
-
make
-
make install
-
2)执行测试脚本:
-
systemctl status nginx .service
-
systemctl restart nginx .service
-
systemctl enable nginx .service
3 案例3:编写监控脚本
3.1 问题
本案例要求编写脚本,实现计算机各个性能数据监控的功能,具体监控项目要求如下:
- CPU负载
- 网卡流量
- 内存剩余容量
- 磁盘剩余容量
- 计算机账户数量
- 当前登录账户数量
- 计算机当前开启的进程数量
- 本机已安装的软件包数量
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:准备工作
1)查看性能数据的命令
- [root@svr5 ~]# uptime //查看CPU负载
15:31:30 //系统当前时间 up 127 days, 3:00 //主机已运行时间,时间越大,说明你的机器越稳定。 1 user //用户连接数,是总连接数而不是用户数 load average: 0.00, 0.00, 0.00 // 系统平均负载,统计最近1,5,15分钟的系统平均负载
- [root@svr5 ~]# ifconfig eth0 //查看网卡流量
- [root@svr5 ~]# free //查看内存信息
- [root@svr5 ~]# df //查看磁盘空间
- [root@svr5 ~]# wc –l /etc/passwd //查看计算机账户数量
- [root@svr5 ~]# who |wc –l //查看登录账户数量
- [root@svr5 ~]# rpm –qa |wc -l //查看已安装软件包数量
步骤二:编写参考脚本
1)脚本内容如下:
[root@svr5 ~]# vim listen.sh
-
#!/bin/bash
-
ip=$(ifconfig eth0 | awk <span class="hljs-string"><span class="hljs-string">'/inet /{print $2}'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"本机IP地址为:</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$ip "
-
cpu=$(uptime | awk <span class="hljs-string"><span class="hljs-string">'{print $10}'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"本机CPU最近5分钟的负载是:</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$cpu "
-
net_in=$(ifconfig eth0 | awk <span class="hljs-string"><span class="hljs-string">'/RX p/{print $5}'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"入站网卡流量为</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$net_in "
-
net_out=$(ifconfig eth0 | awk <span class="hljs-string"><span class="hljs-string">'/TX p/{print $5}'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"出站网卡流量为</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$net_out "
-
menFree=$(free | awk <span class="hljs-string"><span class="hljs-string">'/^Mem/{print $4}'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"内存剩余流量为</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$menFree "
-
diskFree=$(df | awk <span class="hljs-string"><span class="hljs-string">'/\/$/{print $4}'</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"根分区剩余容量为</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$diskFree "
-
users=$(cat /etc/passwd | wc -l)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"计算机账户数量为</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$users 个"
-
loginUsers=$(who | wc -l)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"当前登录账户为</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$loginUsers "
-
processNum=$(ps -aux | wc -l)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"当前开启的进程数量为</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$processNum 个"
-
softNum=$(rpm -qa | wc -l)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> <span class="hljs-string"><span class="hljs-string">"本机已安装的软件包有</span><span class="hljs-variable"><span class="hljs-string"><span class="hljs-variable">$softNum 个"
4 案例4:编写安全检测脚本
4.1 问题
本案例要求编写脚本,防止远程ssh暴力破解密码,具体监控项目要求如下:
- 检测ssh登录日志,如果远程登陆账号名错误3次,则屏蔽远程主机的IP
- 检测ssh登录日志,如果远程登陆密码错误3次,则屏蔽远程主机的IP
4.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:准备工作
1)过滤帐户名失败的命令(登陆日志文件为/var/log/secure)
- [root@svr5 ~]# awk ‘/Invalid user/{print 10}'</span> <span class="sh_regexp" style="color:rgb(166,133,0);">/var/</span><span class="sh_normal">log</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>secure</li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">2)过滤密码失败的命令</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>root@svr5 <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># awk <span class="sh_string" style="color:rgb(166,133,0);">'/Failed password/{print $11}'</span> <span class="sh_regexp" style="color:rgb(166,133,0);">/var/</span><span class="sh_normal">log</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>secure</li></ol></div></div><p class="number" style="text-indent:21pt;font-size:15px;font-weight:bold;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">步骤二:编写参考脚本</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)脚本内容如下:</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">[root@svr5 ~]# vim test.sh<br></p><pre οnclick="hljs.copyCode(event)"><code class="language-bash hljs"><ol class="hljs-ln" style="width:1400px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta"><span class="hljs-meta">#!/bin/bash</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">awk <span class="hljs-string"><span class="hljs-string">'/Failed password/{print $11}'</span></span> /var/<span class="hljs-built_in"><span class="hljs-built_in">log</span></span>/secure | awk <span class="hljs-string"><span class="hljs-string">'{ip[$1]++}END{for(i in ip){print ip[i],i}}'</span></span> | awk <span class="hljs-string"><span class="hljs-string">' 10}'</span> <span class="sh_regexp" style="color:rgb(166,133,0);">/var/</span><span class="sh_normal">log</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>secure</li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">2)过滤密码失败的命令</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>root@svr5 <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># awk <span class="sh_string" style="color:rgb(166,133,0);">'/Failed password/{print $11}'</span> <span class="sh_regexp" style="color:rgb(166,133,0);">/var/</span><span class="sh_normal">log</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>secure</li></ol></div></div><p class="number" style="text-indent:21pt;font-size:15px;font-weight:bold;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">步骤二:编写参考脚本</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)脚本内容如下:</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">[root@svr5 ~]# vim test.sh<br></p><pre οnclick="hljs.copyCode(event)"><code class="language-bash hljs"><ol class="hljs-ln" style="width:1400px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta"><span class="hljs-meta">#!/bin/bash</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">awk <span class="hljs-string"><span class="hljs-string">'/Failed password/{print $11}'</span></span> /var/<span class="hljs-built_in"><span class="hljs-built_in">log</span></span>/secure | awk <span class="hljs-string"><span class="hljs-string">'{ip[$1]++}END{for(i in ip){print ip[i],i}}'</span></span> | awk <span class="hljs-string"><span class="hljs-string">' 1>3{print “远程登录密码错误3次以上的IP:”,2}'</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">awk <span class="hljs-string"><span class="hljs-string">'/Invalid user/{print $10}'</span></span> /var/<span class="hljs-built_in"><span class="hljs-built_in">log</span></span>/secure | awk <span class="hljs-string"><span class="hljs-string">'{ip[$1]++}END{for(i in ip){print ip[i],i}}'</span></span> | awk <span class="hljs-string"><span class="hljs-string">' 2}'</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">awk <span class="hljs-string"><span class="hljs-string">'/Invalid user/{print $10}'</span></span> /var/<span class="hljs-built_in"><span class="hljs-built_in">log</span></span>/secure | awk <span class="hljs-string"><span class="hljs-string">'{ip[$1]++}END{for(i in ip){print ip[i],i}}'</span></span> | awk <span class="hljs-string"><span class="hljs-string">' 1>3{print “远程登录用户错误三次以上的IP:”,2}'</span></span></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre><a style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;" target="_blank"></a><span style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"></span><h2 style="font-size:20px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t13"></a>5 案例5:编写进度显示脚本</h2><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t14"></a>5.1 问题</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">本案例要求编写脚本,实现带进程显示的复制脚本,具体要求如下:</p><ul class="list" style="margin-left:30px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><li>默认Linux的cp命令不具有进度显示</li><li>我们需要自己编写脚本实现进度显示</li><li>可以使用进度条的方式,或者显示百分比的方式</li></ul><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t15"></a>5.2 步骤</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">实现此案例需要按照如下步骤进行。</p><p class="number" style="text-indent:21pt;font-size:15px;font-weight:bold;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">步骤一:编写参考脚本</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)脚本内容如下:</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">[root@svr5 ~]# vim test.sh</p><pre οnclick="hljs.copyCode(event)"><code class="language-bash hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta"><span class="hljs-meta">#!/bin/bash</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">progress</span></span></span></span>(){</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">while</span></span> :</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">do</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> -ne <span class="hljs-string"><span class="hljs-string">'\033[43m#\033[0m'</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> sleep 0.3</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">done</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">progress &</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">cp -r <span class="hljs-variable"><span class="hljs-variable"> 2}'</span></span></div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre><a style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;" target="_blank"></a><span style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"></span><h2 style="font-size:20px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t13"></a>5 案例5:编写进度显示脚本</h2><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t14"></a>5.1 问题</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">本案例要求编写脚本,实现带进程显示的复制脚本,具体要求如下:</p><ul class="list" style="margin-left:30px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><li>默认Linux的cp命令不具有进度显示</li><li>我们需要自己编写脚本实现进度显示</li><li>可以使用进度条的方式,或者显示百分比的方式</li></ul><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t15"></a>5.2 步骤</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">实现此案例需要按照如下步骤进行。</p><p class="number" style="text-indent:21pt;font-size:15px;font-weight:bold;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">步骤一:编写参考脚本</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)脚本内容如下:</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">[root@svr5 ~]# vim test.sh</p><pre οnclick="hljs.copyCode(event)"><code class="language-bash hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta"><span class="hljs-meta">#!/bin/bash</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">progress</span></span></span></span>(){</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">while</span></span> :</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">do</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-built_in"><span class="hljs-built_in">echo</span></span> -ne <span class="hljs-string"><span class="hljs-string">'\033[43m#\033[0m'</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> sleep 0.3</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword"><span class="hljs-keyword">done</span></span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">progress &</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">cp -r <span class="hljs-variable"><span class="hljs-variable"> 1 2</span></span></div></div></li><li><divclass="hljs−ln−numbers"><divclass="hljs−ln−linehljs−ln−n"data−line−number="11"></div></div><divclass="hljs−ln−code"><divclass="hljs−ln−line"><spanclass="hljs−builtin"><spanclass="hljs−builtin">kill</span></span> 2 < / s p a n >< / s p a n >< / d i v >< / d i v >< / l i >< l i >< d i v c l a s s =" h l j s − l n − n u m b e r s ">< d i v c l a s s =" h l j s − l n − l i n e h l j s − l n − n " d a t a − l i n e − n u m b e r =" 11 ">< / d i v >< / d i v >< d i v c l a s s =" h l j s − l n − c o d e ">< d i v c l a s s =" h l j s − l n − l i n e ">< s p a n c l a s s =" h l j s − b u i l t i n ">< s p a n c l a s s =" h l j s − b u i l t i n "> k i l l < / s p a n >< / s p a n > !
知识总结
if [ i==2];then<br></p><p>fi</p>case i == 2 ] ; t h e n < b r >< / p >< p > f i < / p > c a s e 1 in
1)
echo 1;;<br>2)<br><span></span>echo 1 ; ; < b r > 2 ) < b r >< s p a n >< / s p a n > e c h o 1;;
3)
echo 1;;<br>4)<br><span></span>echo 1 ; ; < b r > 4 ) < b r >< s p a n >< / s p a n > e c h o 1;;
*)
echo this is default;;
esac
环境变量
USER USER UID PATH<br>if[ P A T H < b r > i f [ USER == “root” ];then
fi
# 位置变量个数<br> # 位置变量个数<br> * 所有的位置参数
?存放上一次执行命令的结果<br><span></span>if[ ? 存 放 上 一 次 执 行 命 令 的 结 果 < b r >< s p a n >< / s p a n > i f [ ? -eq 0 ];then
fi
循环
(for循环,列表循环:次数是固定的)
for i in 值1 值2 值3
do
命令
done
(while循环,条件循环:次数不是固定的)
while 条件
do
命令
[ let i++ ]
done
函数
cecho(){
echo -e “\033[ 1m 1 m 2\033[0m”
}
cecho 32 OK
cecho 33 OK
区分 ” ”” “
“ ”: 引用整体,不屏蔽特殊符号含义 (“” 和 ‘’只有包含特殊符号才有区别,其他情况没有任何区别)
echo “a b”
echo a b
”: 引用整体,屏蔽特殊符号含义
echo ‘a b’
echo a b
“ :里面放命令,返回执行结果
():放命令<br> ( ) : 放 命 令 < b r > []:计算
{}: awk
sed 3p
sed和awk
sed: vim 编辑文件
awk: grep 过滤文件
tr ‘[a-z]’ ‘[A-Z]’ < test.txt
tr a b < 文件
将竖的输出结果转换成横的
seq 10 | tr ‘\n’ ’ ‘
字符串比较
== != -z
read -p “输入用户名:” user
if [ -z user] && exit<br><br>expect<br>expect << EOF<br>spawn ssh -o StrictHost..<br>expect "password" {send "xx\n"}<span> </span><br>expect "#" {send "xx\n"}<br>EOF<br><br>vim 1.txt & 不影响其他命令的执行<br>kill后面只能加ID号<br> user] && exit<br><br>expect<br>expect << EOF<br>spawn ssh -o StrictHost..<br>expect "password" {send "xx\n"}<span> </span><br>expect "#" {send "xx\n"}<br>EOF<br><br>vim 1.txt & 不影响其他命令的执行<br>kill后面只能加ID号<br> !最后一个后台进程的id号 -