运维常用的 34 个 Linux Shell 脚本,一定能帮到你!

来源:CSDN  http://985.so/xyf7
运维常用的 34 个 Linux Shell 脚本,一定能帮到你!
作为一名 Linux 工程师,会写好的脚本不仅能提高工作效率,还能有更多的时间做自己的事。最近在网上冲浪的时候,也注意收集一些大佬写过的脚本,汇总整理一下,欢迎收藏,与君共勉!
(1)用户猜数字
#!/bin/bash

# 脚本生成一个 100 以内的随机数,提示用户猜数字,根据用户的输入,提示用户猜对了,
# 猜小了或猜大了,直至用户猜对脚本结束。

# RANDOM 为系统自带的系统变量,值为 0‐32767的随机数
# 使用取余算法将随机数变为 1‐100 的随机数
num=$[RANDOM%100+1]
echo "$num"

# 使用 read 提示用户猜数字
# 使用 if 判断用户猜数字的大小关系:‐eq(等于),‐ne(不等于),‐gt(大于),‐ge(大于等于),
# ‐lt(小于),‐le(小于等于)
while :
do 
 read -p "计算机生成了一个 1‐100 的随机数,你猜: " cai  
    if [ $cai -eq $num ]   
    then     
        echo "恭喜,猜对了"     
        exit  
     elif [ $cai -gt $num ]  
     then       
            echo "Oops,猜大了"    
       else      
            echo "Oops,猜小了" 
  fi
done
(2)查看有多少远程的 IP 在连接本机
#!/bin/bash

#!/bin/bash
# 查看有多少远程的 IP 在连接本机(不管是通过 ssh 还是 web 还是 ftp 都统计) 

# 使用 netstat ‐atn 可以查看本机所有连接的状态,‐a 查看所有,
# -t仅显示 tcp 连接的信息,‐n 数字格式显示
# Local Address(第四列是本机的 IP 和端口信息)
# Foreign Address(第五列是远程主机的 IP 和端口信息)
# 使用 awk 命令仅显示第 5 列数据,再显示第 1 列 IP 地址的信息
# sort 可以按数字大小排序,最后使用 uniq 将多余重复的删除,并统计重复的次数
netstat -atn  |  awk  '{print $5}'  | awk  '{print $1}' | sort -nr  |  uniq -c
(3)helloworld
#!/bin/bash

function example {
echo "Hello world!"
}
example
(4)打印 tomcat 的pid
#!/bin/sh</span><br style="outline: 0px;"><br style="outline: 0px;">v1=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"Hello"</span><br style="outline: 0px;">v2=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"world"</span><br style="outline: 0px;">v3=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">${v1}${v2}</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$v3</span><br style="outline: 0px;"><br style="outline: 0px;">pidlist=ps -ef|grep apache-tomcat-7.0.75|grep -v "grep"|awk '{print $2}'<br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$pidlist</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"tomcat Id list :<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$pidlist</span>"</span>  //显示pid</span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(5)脚本编写 剪刀 、 石头、布游戏</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;">game=(石头 剪刀 布)<br style="outline: 0px;">num=$[RANDOM%3]<br style="outline: 0px;">computer=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">${game[$sum]}</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"请根据下列提示选择您的出拳手势"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">" 1. 石头"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">" 2. 剪刀"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">" 3. 布 "</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">read</span> -p <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"请选择 1-3 :"</span> person<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">case</span> <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$person</span> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span><br style="outline: 0px;">1)<br style="outline: 0px;">  <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num</span> -eq 0 ]<br style="outline: 0px;">  <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span> <br style="outline: 0px;">    <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"平局"</span><br style="outline: 0px;">    <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">elif</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num</span> -eq 1 ]<br style="outline: 0px;">    <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;">      <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"你赢"</span><br style="outline: 0px;">    <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">else</span> <br style="outline: 0px;">      <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"计算机赢"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span>;;<br style="outline: 0px;">2)<br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num</span> -eq 0 ]<br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;">    <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"计算机赢"</span><br style="outline: 0px;">    <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">elif</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num</span> -eq 1 ] <br style="outline: 0px;">    <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;">     <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"平局"</span><br style="outline: 0px;">    <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">else</span> <br style="outline: 0px;">      <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"你赢"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span>;;<br style="outline: 0px;">3)<br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num</span> -eq 0 ]<br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span>  <br style="outline: 0px;">   <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"你赢"</span><br style="outline: 0px;">   <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">elif</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num</span> -eq 1 ]<br style="outline: 0px;">   <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span> <br style="outline: 0px;">     <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"计算机赢"</span><br style="outline: 0px;">   <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">else</span> <br style="outline: 0px;">      <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"平局"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span>;;<br style="outline: 0px;">*)<br style="outline: 0px;">  <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"必须输入1-3 的数字"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">esac</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(6)九九乘法表</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span> i <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span> seq 9<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span> j <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span> seq $i<br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> -n <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$j</span>*<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$i</span>=$[i*j] "</span><br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span><br style="outline: 0px;">    <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(7)脚本用源码来安装 memcached 服务器</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 一键部署 memcached </span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 脚本用源码来安装 memcached 服务器</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 注意:如果软件的下载链接过期了,请更新 memcached 的下载链接</span><br style="outline: 0px;">wget http://www.memcached.org/files/memcached-1.5.1.tar.gz<br style="outline: 0px;">yum -y install gcc<br style="outline: 0px;">tar -xf  memcached‐1.5.1.tar.gz<br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">cd</span> memcached‐1.5.1<br style="outline: 0px;">./configure<br style="outline: 0px;">make<br style="outline: 0px;">make install</span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(8)检测本机当前用户是否为超级管理员</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 是,则提示您非管理员(使用字串对比版本) </span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$USER</span> == <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"root"</span> ] <br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span> <br style="outline: 0px;"> yum -y install vsftpd<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">else</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"您不是管理员,没有权限安装软件"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(9)if 运算表达式</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#!/bin/bash -xv</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$1</span> -eq 2 ] ;<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"wo ai wenmin"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">elif</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$1</span> -eq 3 ] ;<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"wo ai wenxing "</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">elif</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$1</span> -eq 4 ] ;<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"wo de xin "</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">elif</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$1</span> -eq 5 ] ;<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"wo de ai "</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(10)脚本 杀掉 tomcat 进程并重新启动</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#kill tomcat pid</span><br style="outline: 0px;"><br style="outline: 0px;">pidlist=ps -ef|grep apache-tomcat-7.0.75|grep -v "grep"|awk '{print $2}'  <span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#找到tomcat的PID号</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"tomcat Id list :<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$pidlist</span>"</span>  //显示pid<br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">kill</span> -9 <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$pidlist</span>  <span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#杀掉改进程</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"KILL <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$pidlist</span>:"</span> //提示进程以及被杀掉<br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"service stop success"</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"start tomcat"</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">cd</span> /opt/apache-tomcat-7.0.75<br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">pwd</span> <br style="outline: 0px;"><br style="outline: 0px;">rm -rf work/*<br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">cd</span> bin<br style="outline: 0px;"><br style="outline: 0px;">./startup.sh <span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#;tail -f ../logs/catalina.out</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(11)打印国际象棋棋盘</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 打印国际象棋棋盘</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 设置两个变量,i 和 j,一个代表行,一个代表列,国际象棋为 8*8 棋盘</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># i=1 是代表准备打印第一行棋盘,第 1 行棋盘有灰色和蓝色间隔输出,总共为 8 列</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># i=1,j=1 代表第 1 行的第 1 列;i=2,j=3 代表第 2 行的第 3 列</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 棋盘的规律是 i+j 如果是偶数,就打印蓝色色块,如果是奇数就打印灰色色块</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 使用 echo ‐ne 打印色块,并且打印完成色块后不自动换行,在同一行继续输出其他色块</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span> i <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span> {1..8}<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span><br style="outline: 0px;">   <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span> j <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span> {1..8}<br style="outline: 0px;">   <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span><br style="outline: 0px;">    sum=$[i+j]<br style="outline: 0px;">  <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [  $[sum%2] -eq 0 ];<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;">    <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> -ne <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"�33[46m  �33[0m"</span><br style="outline: 0px;">  <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">else</span><br style="outline: 0px;">   <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> -ne <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"�33[47m  �33[0m"</span><br style="outline: 0px;">  <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span><br style="outline: 0px;">   <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span><br style="outline: 0px;">   <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span><br style="outline: 0px;"><br style="outline: 0px;"></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(12)统计当前 Linux 系统中可以登录计算机的账户有多少个</strong><br style="outline: 0px;"></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 统计当前 Linux 系统中可以登录计算机的账户有多少个</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#方法 1:</span><br style="outline: 0px;">grep <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"bash$"</span> /etc/passwd | wc -l<br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#方法 2:</span><br style="outline: 0px;">awk -f : <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">'/bash$/{x++}end{print x}'</span> /etc/passwd</span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(13)备份 MySQL 表数据</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/sh<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">source</span> /etc/profile<br style="outline: 0px;">dbName=mysql<br style="outline: 0px;">tableName=db<br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> [date +'%Y-%m-%d %H:%M:%S']<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">' start loading data...'</span><br style="outline: 0px;">mysql -uroot -proot -P3306 <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">${dbName}</span> -e <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"LOAD DATA LOCAL INFILE '# /home/wenmin/wenxing.txt' INTO TABLE <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">${tableName}</span> FIELDS TERMINATED BY ';'"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> [date +'%Y-%m-%d %H:%M:%S']<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">' end loading data...'</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">exit</span><br style="outline: 0px;">EOF</span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(14)使用死循环实时显示 eth0 网卡发送的数据包流量</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 使用死循环实时显示 eth0 网卡发送的数据包流量 </span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">while</span> :<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">'本地网卡 ens33 流量信息如下:'</span><br style="outline: 0px;"> ifconfig ens33 | grep <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"RX pack"</span> | awk <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">'{print $5}'</span><br style="outline: 0px;">     ifconfig ens33 | grep <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"TX pack"</span> | awk <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">'{print $5}'</span><br style="outline: 0px;"> sleep 1<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(15)编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 状态(for 版本)</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span> i <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span> {1..254}<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 每隔0.3秒ping一次,一共ping2次,并以1毫秒为单位设置ping的超时时间</span><br style="outline: 0px;"> ping -c 2 -i 0.3 -W 1 192.168.1.<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$i</span> &>/dev/null<br style="outline: 0px;">     <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ $? -eq 0 ];<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"192.168.1.<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$i</span> is up"</span><br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">else</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"192.168.1.<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$i</span> is down"</span><br style="outline: 0px;"> <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(16)编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 认的 123456 作为默认密码。</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">read</span> -p <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"请输入用户名:"</span> user<br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#使用‐z 可以判断一个变量是否为空,如果为空,提示用户必须输入账户名,并退出脚本,退出码为 2</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#没有输入用户名脚本退出后,使用$?查看的返回码为 2</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ -z <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$user</span> ]; <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">" 您不需要输入账户名"</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">exit</span> 2<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span> <br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#使用 stty ‐echo 关闭 shell 的回显功能</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;">#使用 stty  echo 打开 shell 的回显功能</span><br style="outline: 0px;">stty -<span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">read</span> -p <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"请输入密码:"</span> pass<br style="outline: 0px;">stty <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <br style="outline: 0px;">pass=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">${pass:-123456}</span><br style="outline: 0px;">useradd <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$user</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$pass</span>"</span> | passwd --stdin <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$user</span>"</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(17)使用脚本对输入的三个整数进行排序</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 依次提示用户输入 3 个整数,脚本根据数字大小依次排序输出 3 个数字</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">read</span> -p <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">" 请输入一个整数:"</span> num1<br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">read</span> -p <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">" 请输入一个整数:"</span> num2<br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">read</span> -p <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">" 请输入一个整数:  "</span> num3<br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 不管谁大谁小,最后都打印 echo "$num1,$num2,$num3"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># num1 中永远存最小的值,num2 中永远存中间值,num3 永远存最大值</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 如果输入的不是这样的顺序,则改变数的存储顺序,如:可以将 num1 和 num2 的值对调</span><br style="outline: 0px;">tmp=0<br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 如果 num1 大于 num2,就把 num1 和和 num2 的值对调,确保 num1 变量中存的是最小值</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num1</span> -gt <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num2</span> ];<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> tmp=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num1</span><br style="outline: 0px;"> num1=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num2</span><br style="outline: 0px;"> num2=tmp<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 如果 num1 大于 num3,就把 num1 和 num3 对调,确保 num1 变量中存的是最小值</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num1</span> -gt <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num3</span> ];<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> tmp=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num1</span><br style="outline: 0px;"> num1=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num3</span><br style="outline: 0px;"> num3=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$tmp</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 如果 num2 大于 num3,就把 num2 和 num3 对调,确保 num2 变量中存的是最小值</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num2</span> -gt <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num3</span> ];<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> tmp=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num2</span><br style="outline: 0px;"> num2=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num3</span><br style="outline: 0px;"> num3=<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$tmp</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"排序后数据(从小到大)为:<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num1</span>,<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num2</span>,<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$num3</span>"</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;">(18)根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动</span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动 </span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 00‐12 点为早晨,12‐18 点为下午,18‐24 点为晚上</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 使用 date 命令获取时间后,if 判断时间的区间,确定问候语内容</span><br style="outline: 0px;">tm=$(date +%H)<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">if</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$tm</span> -le 12 ];<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;"> msg=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"Good Morning <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$USER</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">elif</span> [ <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$tm</span> -gt 12 -a <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$tm</span> -le 18 ];<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">then</span><br style="outline: 0px;">   msg=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"Good Afternoon <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$USER</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">else</span><br style="outline: 0px;">   msg=<span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"Good Night <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$USER</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">fi</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"当前时间是:<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$(date +"%Y‐%m‐%d %H:%M:%S")</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> -e <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"�33[34m<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$msg</span>�33[0m"</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(19)将 I lov cls 写入到 txt 文件中</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">cd</span> /home/wenmin/<br style="outline: 0px;">touch wenxing.txt<br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"I lov cls"</span> >>wenxing.txt</span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(20)脚本编写 for 循环判断</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;">s=0;<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span>((i=1;i<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> s=$[<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$s</span>+<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$i</span>]<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span> <br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$s</span><br style="outline: 0px;"><br style="outline: 0px;">r=0;<br style="outline: 0px;">a=0;<br style="outline: 0px;">b=0;<br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span>((x=1;x<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> a=$[<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$a</span>+<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$x</span>] <br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$x</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span>((y=1;y<span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> b=$[<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$b</span>+<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$y</span>]<br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$y</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$r</span>=$[<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$a</span>+<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$b</span>]</span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(21)脚本编写 for 循环判断</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash<br style="outline: 0px;"></span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span> i <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"$*"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"wenmin xihuan <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$i</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span><br style="outline: 0px;"><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">for</span> j <span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">in</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"<span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$@</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">do</span> <br style="outline: 0px;"> <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">echo</span> <span style="outline: 0px;color: rgb(152, 195, 121);line-height: 26px;">"wenmin xihuan <span style="outline: 0px;color: rgb(209, 154, 102);line-height: 26px;">$j</span>"</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(198, 120, 221);line-height: 26px;">done</span></span></section></pre></section><section style='outline: 0px;color: rgb(34, 34, 34);letter-spacing: 0.544px;white-space: normal;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;background-color: rgb(255, 255, 255);line-height: 1.75em;margin-bottom: 1.2em !important;'><span style="outline: 0px;font-size: 17px;"><strong style="outline: 0px;">(22)脚本 每周 5 使用 tar 命令备份/var/log 下的所有日志文件</strong></span></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style='margin-bottom: 0px;padding-right: 10px;padding-left: 10px;outline: 0px;white-space: normal;background-color: rgb(255, 255, 255);font-size: 16px;color: black;line-height: 1.6;letter-spacing: 0px;word-break: break-word;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;'><pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;outline: 0px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><section style='margin-bottom: 1em;padding: 15px 16px 16px;outline: 0px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(40, 44, 52);border-radius: 5px;line-height: 1.75em;'><span style="outline: 0px;font-size: 17px;"><span style="outline: 0px;color: rgb(97, 174, 238);line-height: 26px;">#!/bin/bash</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 每周 5 使用 tar 命令备份/var/log 下的所有日志文件</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># vim  /root/logbak.sh</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 编写备份脚本,备份后的文件名包含日期标签,防止后面的备份将前面的备份数据覆盖</span><br style="outline: 0px;"><span style="outline: 0px;color: rgb(92, 99, 112);font-style: italic;line-height: 26px;"># 注意 date 命令需要使用反引号括起来,反引号在键盘<tab>键上面</tab></span><br style="outline: 0px;"><br style="outline: 0px;">tar -czf <span style="outline: 0px;color: rgb(230, 192, 123);line-height: 26px;">log</span>-date +%Y%m%d`.tar.gz /var/log 

# crontab -e #编写计划任务,执行备份脚本
00 03 * * 5 /home/wenmin/datas/logbak.sh
(23)脚本编写 求和 函数运算 function xx()
#!/bin/bash

function sum()
{
 s=0;
 s=$[$1+$2]
 echo $s
}
read -p "input your parameter " p1
read -p "input your parameter " p2

sum $p1 $p2

function multi()
{
 r=0;
 r=$[$1/$2]
 echo $r
}
read -p "input your parameter " x1
read -p "input your parameter " x2

multi $x1 $x2

v1=1
v2=2
let v3=$v1+$v2
echo $v3
(24)脚本编写 case — esac 分支结构表达式
#!/bin/bash 

case $1 in 
1) 
 echo "wenmin "
;;
2)
 echo "wenxing "
;; 
3)  
 echo "wemchang "
;;
4) 
 echo "yijun"
;;
5)
 echo "sinian"
;;
6)  
 echo "sikeng"
;;
7) 
 echo "yanna"
;;
*)
 echo "danlian"
;; 
esac
(25)# 定义要监控的页面地址,对 tomcat 状态进行重启或维护
#!/bin/sh  
# function:自动监控tomcat进程,挂了就执行重启操作  
# author:huanghong  
# DEFINE  

# 获取tomcat PPID  
TomcatID=$(ps -ef |grep tomcat |grep -w 'apache-tomcat-7.0.75'|grep -v 'grep'|awk '{print $2}')  

# tomcat_startup  
StartTomcat=/opt/apache-tomcat-7.0.75/bin/startup.sh  


#TomcatCache=/usr/apache-tomcat-5.5.23/work  

# 定义要监控的页面地址  
WebUrl=http://192.168.254.118:8080/

# 日志输出  
GetPageInfo=/dev/null  
TomcatMonitorLog=/tmp/TomcatMonitor.log  

Monitor()  
  {  
   echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]"  
   if [ $TomcatID ]
 then  
      echo "[info]tomcat进程ID为:$TomcatID."  
      # 获取返回状态码  
      TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})  
      if [ $TomcatServiceCode -eq 200 ];then  
          echo "[info]返回码为$TomcatServiceCode,tomcat启动成功,页面正常."  
      else  
          echo "[error]访问出错,状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo"  
          echo "[error]开始重启tomcat"  
          kill -9 $TomcatID  # 杀掉原tomcat进程  
          sleep 3  
          #rm -rf $TomcatCache # 清理tomcat缓存  
          $StartTomcat  
      fi  
      else  
      echo "[error]进程不存在!tomcat自动重启..."  
      echo "[info]$StartTomcat,请稍候......"  
      #rm -rf $TomcatCache  
      $StartTomcat  
    fi  
    echo "------------------------------"  
   }  
   Monitor>>$TomcatMonitorLog
(26)通过位置变量创建 Linux 系统账户及密码
#!/bin/bash

# 通过位置变量创建Linux 系统账户及密码

# $1 是执行脚本的第一个参数,$2  是执行脚本的第二个参数

useradd "$1"
echo "$2" | passwd --stdin "$1"
(27)对变量的传入与获取个数及打印
#!/bin/bash
echo "$0 $1 $2 $3"  // 传入三个参数
echo $#    //获取传入参数的数量
echo $@    //打印获取传入参数
echo $*    //打印获取传入参数
(28)实时监控本机内存和硬盘剩余空间,剩余内存小于500M、根分区剩余空间小于1000M时,发送报警邮件给root管理员
#!/bin/bash

# 实时监控本机内存和硬盘剩余空间,剩余内存小于500M、根分区剩余空间小于1000M时,发送报警邮件给root管理员

# 提取根分区剩余空间
disk_size=$(df / | awk '///{print $4}')

# 提取内存剩余空空间
mem_size=$(free | awk '/Mem/{print $4}')
while :
do 
# 注意内存和磁盘提取的空间大小都是以 Kb 为单位
if  [  $disk_size -le 512000 -a $mem_size -le 1024000  ]
then
    mail  ‐s  "Warning"  root   Insufficient resources,资源不足
EOF
fi
done
(29)检查指定目录下是否存在 对应 文件
#!/bin/bash

if [ -f /home/wenmin/datas ]
then 
echo "File exists"
fi
(30)脚本定义while循环语句
#!/bin/bash

if [ -f /home/wenmin/datas ]
then 
echo "File exists"
fi

[root@rich datas]# cat while.sh 
#!/bin/bash

s=0
i=1
while [ $i -le 100 ]
do
        s=$[$s + $i]
        i=$[$i + 1]
done

echo $s
echo $i
(31)一键部署 LNMP(RPM 包版本)
#!/bin/bash 

# 一键部署 LNMP(RPM 包版本)
# 使用 yum 安装部署 LNMP,需要提前配置好 yum 源,否则该脚本会失败
# 本脚本使用于 centos7.2 或 RHEL7.2
yum -y install httpd
yum -y install mariadb mariadb-devel mariadb-server
yum -y install php php-mysql

systemctl start httpd mariadb
systemctl enable httpd mariadb
(32)读取控制台传入参数
#!/bin/bash
read -t 7 -p "input your name " NAME
echo $NAME

read -t 11 -p "input you age " AGE
echo $AGE

read -t 15 -p "input your friend " FRIEND
echo $FRIEND

read -t 16 -p "input your love " LOVE
echo $LOVE
(33)脚本实现 复制
#!/bin/bash

cp $1 $2
(34)脚本实现文件存在与否的判断
#!/bin/bash

if [ -f file.txt ];then
 echo "文件存在"
else 
 echo "文件不存在"
fi
0 0 投票数
文章评分

本文转载自Linux就该这么学,原文链接:https://mp.weixin.qq.com/s/bCEfdsMdPSLTfg5hndm51A。

(0)
上一篇 2023-03-01 22:33
下一篇 2023-03-02 10:52

相关推荐

订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x