好哇网

脚本大全100例(100个Linux Shell脚本经典案例)

作者 2022-05-14 57

给大家分享100个Linux Shell脚本经典案例,另外本篇文档也已经整理成pdf,易失效,请及时保存~

链接:
https://pan.baidu.com/s/1H8RhYJJWWeoLPd7WUaTi0A?pwd=nawp

提取码:nawp

1、编写hello world脚本

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写hello world脚本</span>
 
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello World!"</span>
</code>

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

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 通过位置变量创建 Linux 系统账户及密码</span>
 
<span class="hljs-comment">#$1 是执行脚本的第一个参数,$2 是执行脚本的第二个参数</span>
useradd    <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> 
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$2</span>"</span>  |  passwd  ‐‐stdin  <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
</code>

3、备份日志

<code><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># 每周 5 使用 tar 命令备份/var/log 下的所有日志文件</span>
<span class="hljs-comment"># vim  /root/logbak.sh</span>
<span class="hljs-comment"># 编写备份脚本,备份后的文件名包含日期标签,防止后面的备份将前面的备份数据覆盖</span>
<span class="hljs-comment"># 注意 date 命令需要使用反引号括起来,反引号在键盘&lt;tab&gt;键上面</span>
tar  -czf  <span class="hljs-built_in">log</span>-`date +%Y%m%d`.tar.gz  /var/<span class="hljs-built_in">log</span> 
 
<span class="hljs-comment"># crontab ‐e  #编写计划任务,执行备份脚本</span>
00  03  *  *  5  /root/logbak.sh
</code>

4、一键部署 LNMP(RPM 包版本)

<code><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># 一键部署 LNMP(RPM 包版本)</span>
<span class="hljs-comment"># 使用 yum 安装部署 LNMP,需要提前配置好 yum 源,否则该脚本会失败</span>
<span class="hljs-comment"># 本脚本使用于 centos7.2 或 RHEL7.2</span>
yum ‐y install httpd
yum ‐y install mariadb mariadb‐devel mariadb‐server
yum ‐y install php  php‐mysql
 
systemctl start httpd mariadb
systemctl <span class="hljs-built_in">enable</span> httpd mariadb
</code>

5、监控内存和磁盘容量,小于给定值时报警

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 实时监控本机内存和硬盘剩余空间,剩余内存小于500M、根分区剩余空间小于1000M时,发送报警邮件给root管理员</span>
 
<span class="hljs-comment"># 提取根分区剩余空间</span>
disk_size=$(df / | awk <span class="hljs-string">'/\//{print $4}'</span>)
 
<span class="hljs-comment"># 提取内存剩余空间</span>
mem_size=$(free | awk <span class="hljs-string">'/Mem/{print $4}'</span>)
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
<span class="hljs-comment"># 注意内存和磁盘提取的空间大小都是以 Kb 为单位</span>
<span class="hljs-keyword">if</span>  [  <span class="hljs-variable">$disk_size</span> -le 512000 -a <span class="hljs-variable">$mem_size</span> -le 1024000  ]
<span class="hljs-keyword">then</span>
    mail  ‐s  <span class="hljs-string">"Warning"</span>  root  &lt;&lt;EOF
  Insufficient resources,资源不足
EOF
<span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
</code>

6、猜数字游戏

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 脚本生成一个 100 以内的随机数,提示用户猜数字,根据用户的输入,提示用户猜对了,</span>
<span class="hljs-comment"># 猜小了或猜大了,直至用户猜对脚本结束。</span>
 
<span class="hljs-comment"># RANDOM 为系统自带的系统变量,值为 0‐32767的随机数</span>
<span class="hljs-comment"># 使用取余算法将随机数变为 1‐100 的随机数</span>
num=$[RANDOM%100+1]
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$num</span>"</span>
 
<span class="hljs-comment"># 使用 read 提示用户猜数字</span>
<span class="hljs-comment"># 使用 if 判断用户猜数字的大小关系:‐eq(等于),‐ne(不等于),‐gt(大于),‐ge(大于等于),</span>
<span class="hljs-comment"># ‐lt(小于),‐le(小于等于)</span>
<span class="hljs-keyword">while</span>  :
<span class="hljs-keyword">do</span>
  <span class="hljs-built_in">read</span> -p <span class="hljs-string">"计算机生成了一个 1‐100 的随机数,你猜: "</span> cai
    <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$cai</span> -eq <span class="hljs-variable">$num</span> ]
    <span class="hljs-keyword">then</span>
         <span class="hljs-built_in">echo</span> <span class="hljs-string">"恭喜,猜对了"</span>
         <span class="hljs-built_in">exit</span>
      <span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$cai</span> -gt <span class="hljs-variable">$num</span> ]
      <span class="hljs-keyword">then</span>
             <span class="hljs-built_in">echo</span> <span class="hljs-string">"Oops,猜大了"</span>
        <span class="hljs-keyword">else</span>
             <span class="hljs-built_in">echo</span> <span class="hljs-string">"Oops,猜小了"</span>
   <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
</code>

7、检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员(使用字串对比版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不</span>
<span class="hljs-comment"># 是,则提示您非管理员(使用字串对比版本) </span>
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$USER</span> == <span class="hljs-string">"root"</span> ]
<span class="hljs-keyword">then</span>
  yum ‐y install vsftpd
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"您不是管理员,没有权限安装软件"</span>
<span class="hljs-keyword">fi</span>
</code>

8、检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员(使用 UID 数字对比版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不</span>
<span class="hljs-comment"># 是,则提示您非管理员(使用 UID 数字对比版本)</span>
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$UID</span> -eq 0 ];<span class="hljs-keyword">then</span>
    yum ‐y install vsftpd
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"您不是管理员,没有权限安装软件"</span>
<span class="hljs-keyword">fi</span>
</code>

9、编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默认的 123456 作为默认密码。

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户</span>
<span class="hljs-comment"># 不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默</span>
<span class="hljs-comment"># 认的 123456 作为默认密码。</span>
 
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入用户名: "</span> user
<span class="hljs-comment">#使用‐z 可以判断一个变量是否为空,如果为空,提示用户必须输入账户名,并退出脚本,退出码为 2</span>
<span class="hljs-comment">#没有输入用户名脚本退出后,使用$?查看的返回码为 2</span>
<span class="hljs-keyword">if</span> [ -z <span class="hljs-variable">$user</span> ];<span class="hljs-keyword">then</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">"您不需输入账户名"</span>
   <span class="hljs-built_in">exit</span> 2
<span class="hljs-keyword">fi</span>
<span class="hljs-comment">#使用 stty ‐echo 关闭 shell 的回显功能</span>
<span class="hljs-comment">#使用 stty  echo 打开 shell 的回显功能</span>
stty -<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入密码: "</span> pass
stty <span class="hljs-built_in">echo</span>
pass=<span class="hljs-variable">${pass:‐123456}</span>
useradd <span class="hljs-string">"<span class="hljs-variable">$user</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$pass</span>"</span> | passwd ‐‐stdin <span class="hljs-string">"<span class="hljs-variable">$user</span>"</span>
</code>

10、输入三个数并进行升序排序

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 依次提示用户输入 3 个整数,脚本根据数字大小依次排序输出 3 个数字</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入一个整数:"</span> num1
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入一个整数:"</span> num2
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入一个整数:"</span> num3
<span class="hljs-comment"># 不管谁大谁小,最后都打印 echo "$num1,$num2,$num3"</span>
<span class="hljs-comment"># num1 中永远存最小的值,num2 中永远存中间值,num3 永远存最大值</span>
<span class="hljs-comment"># 如果输入的不是这样的顺序,则改变数的存储顺序,如:可以将 num1 和 num2 的值对调</span>
tmp=0
<span class="hljs-comment"># 如果 num1 大于 num2,就把 num1 和和 num2 的值对调,确保 num1 变量中存的是最小值</span>
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$num1</span> -gt <span class="hljs-variable">$num2</span> ];<span class="hljs-keyword">then</span>   
  tmp=<span class="hljs-variable">$num1</span>
  num1=<span class="hljs-variable">$num2</span>
  num2=<span class="hljs-variable">$tmp</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment"># 如果 num1 大于 num3,就把 num1 和 num3 对调,确保 num1 变量中存的是最小值</span>
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$num1</span> -gt <span class="hljs-variable">$num3</span> ];<span class="hljs-keyword">then</span>   
    tmp=<span class="hljs-variable">$num1</span>
    num1=<span class="hljs-variable">$num3</span>
    num3=<span class="hljs-variable">$tmp</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment"># 如果 num2 大于 num3,就把 num2 和 num3 对标,确保 num2 变量中存的是小一点的值</span>
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$num2</span> -gt <span class="hljs-variable">$num3</span> ];<span class="hljs-keyword">then</span>
    tmp=<span class="hljs-variable">$num2</span>
    num2=<span class="hljs-variable">$num3</span>
    num3=<span class="hljs-variable">$tmp</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"排序后数据(从小到大)为:<span class="hljs-variable">$num1</span>,<span class="hljs-variable">$num2</span>,<span class="hljs-variable">$num3</span>"</span>
</code>

11、石头、剪刀、布游戏

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写脚本,实现人机&lt;石头,剪刀,布&gt;游戏</span>
game=(石头 剪刀 布)
num=$[RANDOM%3]
computer=<span class="hljs-variable">${game[$num]}</span>
<span class="hljs-comment"># 通过随机数获取计算机的出拳</span>
<span class="hljs-comment"># 出拳的可能性保存在一个数组中,game[0],game[1],game[2]分别是 3 中不同的可能</span>
 
<span class="hljs-built_in">echo</span> <span class="hljs-string">"请根据下列提示选择您的出拳手势"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"1.石头"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"2.剪刀"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"3.布"</span>
 
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请选择 1‐3:"</span> person
<span class="hljs-keyword">case</span>  <span class="hljs-variable">$person</span>  <span class="hljs-keyword">in</span>
1)
  <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$num</span> -eq 0 ]
  <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"平局"</span>
    <span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$num</span> -eq 1 ]
    <span class="hljs-keyword">then</span>
      <span class="hljs-built_in">echo</span> <span class="hljs-string">"你赢"</span>
  <span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"计算机赢"</span>
  <span class="hljs-keyword">fi</span>;;
2)   
  <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$num</span> -eq 0 ]
  <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"计算机赢"</span>
    <span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$num</span> -eq 1 ]
    <span class="hljs-keyword">then</span>
      <span class="hljs-built_in">echo</span> <span class="hljs-string">"平局"</span>
  <span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"你赢"</span>
  <span class="hljs-keyword">fi</span>;;
3)
  <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$num</span> -eq 0 ]
  <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"你赢"</span>
    <span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$num</span> -eq 1 ]
    <span class="hljs-keyword">then</span>
      <span class="hljs-built_in">echo</span> <span class="hljs-string">"计算机赢"</span>
  <span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"平局"</span>
  <span class="hljs-keyword">fi</span>;;
*)
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"必须输入 1‐3 的数字"</span>
<span class="hljs-keyword">esac</span>
</code>

12、编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(for 版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机</span>
<span class="hljs-comment"># 状态(for 版本)</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..254}
<span class="hljs-keyword">do</span>
  <span class="hljs-comment"># 每隔0.3秒ping一次,一共ping2次,并以1毫秒为单位设置ping的超时时间</span>
     ping ‐c 2 ‐i 0.3 ‐W 1 192.168.4.<span class="hljs-variable">$i</span>  &amp;&gt;/dev/null
    <span class="hljs-keyword">if</span>  [ $? -eq 0 ];<span class="hljs-keyword">then</span>
         <span class="hljs-built_in">echo</span> <span class="hljs-string">"192.168.4.<span class="hljs-variable">$i</span> is up"</span>
     <span class="hljs-keyword">else</span>
         <span class="hljs-built_in">echo</span>  <span class="hljs-string">"192.168.4.<span class="hljs-variable">$i</span> is down"</span>
     <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
</code>

13、编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(while 版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机</span>
<span class="hljs-comment"># 状态(while 版本) </span>
i=1
<span class="hljs-keyword">while</span> [ <span class="hljs-variable">$i</span> -le 254 ]
<span class="hljs-keyword">do</span>
     ping ‐c 2 ‐i 0.3 ‐W 1 192.168.4.<span class="hljs-variable">$i</span>  &amp;&gt;/dev/null
     <span class="hljs-keyword">if</span>  [ $? -eq 0 ];<span class="hljs-keyword">then</span>
         <span class="hljs-built_in">echo</span> <span class="hljs-string">"192.168.4.<span class="hljs-variable">$i</span> is up"</span>
    <span class="hljs-keyword">else</span>
         <span class="hljs-built_in">echo</span>  <span class="hljs-string">"192.168.4.<span class="hljs-variable">$i</span> is down"</span>
     <span class="hljs-keyword">fi</span>
     <span class="hljs-built_in">let</span> i++
<span class="hljs-keyword">done</span>
</code>

14、编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(多进程版)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机</span>
<span class="hljs-comment"># 状态(多进程版)</span>
 
<span class="hljs-comment">#定义一个函数,ping 某一台主机,并检测主机的存活状态</span>
<span class="hljs-function"><span class="hljs-title">myping</span></span>(){
ping ‐c 2 ‐i 0.3 ‐W 1 <span class="hljs-variable">$1</span>  &amp;&gt;/dev/null
<span class="hljs-keyword">if</span>  [ $? -eq 0 ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$1</span> is up"</span>
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$1</span> is down"</span>
<span class="hljs-keyword">fi</span>
}
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..254}
<span class="hljs-keyword">do</span>
     myping 192.168.4.<span class="hljs-variable">$i</span> &amp;
<span class="hljs-keyword">done</span>
<span class="hljs-comment"># 使用&amp;符号,将执行的函数放入后台执行</span>
<span class="hljs-comment"># 这样做的好处是不需要等待ping第一台主机的回应,就可以继续并发ping第二台主机,依次类推。</span>
</code>

15、编写脚本,显示进度条

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写脚本,显示进度条</span>
<span class="hljs-function"><span class="hljs-title">jindu</span></span>(){
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
     <span class="hljs-built_in">echo</span> -n <span class="hljs-string">'#'</span>
     sleep 0.2
<span class="hljs-keyword">done</span>
}
jindu &amp;
cp -a <span class="hljs-variable">$1</span> <span class="hljs-variable">$2</span>
killall <span class="hljs-variable">$0</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"拷贝完成"</span>
</code>

16、进度条,动态时针版本;定义一个显示进度的函数,屏幕快速显示| / ‐ \

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 进度条,动态时针版本</span>
<span class="hljs-comment"># 定义一个显示进度的函数,屏幕快速显示|  / ‐ \</span>
<span class="hljs-function"><span class="hljs-title">rotate_line</span></span>(){
INTERVAL=0.5  <span class="hljs-comment">#设置间隔时间</span>
COUNT=<span class="hljs-string">"0"</span>     <span class="hljs-comment">#设置4个形状的编号,默认编号为 0(不代表任何图像)</span>
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
  COUNT=`expr <span class="hljs-variable">$COUNT</span> + 1` <span class="hljs-comment">#执行循环,COUNT 每次循环加 1,(分别代表4种不同的形状)</span>
  <span class="hljs-keyword">case</span> <span class="hljs-variable">$COUNT</span> <span class="hljs-keyword">in</span>          <span class="hljs-comment">#判断 COUNT 的值,值不一样显示的形状就不一样</span>
  <span class="hljs-string">"1"</span>)                    <span class="hljs-comment">#值为 1 显示‐</span>
          <span class="hljs-built_in">echo</span> -e <span class="hljs-string">'‐'</span><span class="hljs-string">"\b\c"</span>
          sleep <span class="hljs-variable">$INTERVAL</span>
          ;;
    <span class="hljs-string">"2"</span>)                  <span class="hljs-comment">#值为 2 显示\\,第一个\是转义</span>
          <span class="hljs-built_in">echo</span> -e <span class="hljs-string">'\\'</span><span class="hljs-string">"\b\c"</span>
          sleep <span class="hljs-variable">$INTERVAL</span>
          ;;
    <span class="hljs-string">"3"</span>)                  <span class="hljs-comment">#值为 3 显示|</span>
          <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"|\b\c"</span>
          sleep <span class="hljs-variable">$INTERVAL</span>
          ;;
   <span class="hljs-string">"4"</span>)                   <span class="hljs-comment">#值为 4 显示/</span>
          <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"/\b\c"</span>
          sleep <span class="hljs-variable">$INTERVAL</span>
          ;;
    *)                    <span class="hljs-comment">#值为其他时,将 COUNT 重置为 0</span>
          COUNT=<span class="hljs-string">"0"</span>;;
    <span class="hljs-keyword">esac</span>
<span class="hljs-keyword">done</span>
}
rotate_line
</code>

17、9*9 乘法表

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 9*9 乘法表(编写 shell 脚本,打印 9*9 乘法表) </span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `seq 9`
<span class="hljs-keyword">do</span>
    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> `seq <span class="hljs-variable">$i</span>`
     <span class="hljs-keyword">do</span>
         <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"<span class="hljs-variable">$j</span>*<span class="hljs-variable">$i</span>=$[i*j]  "</span>
     <span class="hljs-keyword">done</span>
    <span class="hljs-built_in">echo</span>
<span class="hljs-keyword">done</span>
</code>

18、使用死循环实时显示 eth0 网卡发送的数据包流量

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用死循环实时显示 eth0 网卡发送的数据包流量 </span>
 
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
   <span class="hljs-built_in">echo</span>  <span class="hljs-string">'本地网卡 eth0 流量信息如下: '</span>
    ifconfig eth0 | grep <span class="hljs-string">"RX pack"</span> | awk <span class="hljs-string">'{print $5}'</span>
    ifconfig eth0 | grep <span class="hljs-string">"TX pack"</span> | awk <span class="hljs-string">'{print $5}'</span>
     sleep 1
<span class="hljs-keyword">done</span>
</code>

19、使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码本脚本执行,需要提前准备一个 user.txt 文件,该文件中包含有若干用户名信息

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码</span>
<span class="hljs-comment"># 本脚本执行,需要提前准备一个 user.txt 文件,该文件中包含有若干用户名信息</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `cat user.txt`
<span class="hljs-keyword">do</span>
     useradd  <span class="hljs-variable">$i</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">"123456"</span> | passwd ‐‐stdin <span class="hljs-variable">$i</span>
<span class="hljs-keyword">done</span>
</code>

20、编写批量修改扩展名脚本

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写批量修改扩展名脚本,如批量将 txt 文件修改为 doc 文件 </span>
<span class="hljs-comment"># 执行脚本时,需要给脚本添加位置参数</span>
<span class="hljs-comment"># 脚本名  txt  doc(可以将 txt 的扩展名修改为 doc)</span>
<span class="hljs-comment"># 脚本名  doc  jpg(可以将 doc 的扩展名修改为 jpg)</span>
 
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `ls *.<span class="hljs-variable">$1</span>`
<span class="hljs-keyword">do</span>
     mv <span class="hljs-variable">$i</span> <span class="hljs-variable">${i%.*}</span>.<span class="hljs-variable">$2</span>
<span class="hljs-keyword">done</span>
</code>

21、使用 expect 工具自动交互密码远程其他主机安装 httpd 软件

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用 expect 工具自动交互密码远程其他主机安装 httpd 软件 </span>
 
<span class="hljs-comment"># 删除~/.ssh/known_hosts 后,ssh 远程任何主机都会询问是否确认要连接该主机</span>
rm  ‐rf  ~/.ssh/known_hosts
expect &lt;&lt;EOF
spawn ssh 192.168.4.254
expect <span class="hljs-string">"yes/no"</span> {send <span class="hljs-string">"yes\r"</span>}
<span class="hljs-comment"># 根据自己的实际情况将密码修改为真实的密码字串</span>
expect <span class="hljs-string">"password"</span> {send  <span class="hljs-string">"密码\r"</span>}
expect <span class="hljs-string">"#"</span> {send  <span class="hljs-string">"yum ‐y install httpd\r"</span>}
expect <span class="hljs-string">"#"</span> {send  <span class="hljs-string">"exit\r"</span>}
EOF
</code>

22、一键部署 LNMP(源码安装版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 一键部署 LNMP(源码安装版本)</span>
<span class="hljs-function"><span class="hljs-title">menu</span></span>()
{
clear
<span class="hljs-built_in">echo</span> <span class="hljs-string">"  ##############‐‐‐‐Menu‐‐‐‐##############"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"# 1. Install Nginx"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"# 2. Install MySQL"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"# 3. Install PHP"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"# 4. Exit Program"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"  ########################################"</span>
}
 
<span class="hljs-function"><span class="hljs-title">choice</span></span>()
{
  <span class="hljs-built_in">read</span> -p <span class="hljs-string">"Please choice a menu[1‐9]:"</span> select
}
 
<span class="hljs-function"><span class="hljs-title">install_nginx</span></span>()
{
  id nginx &amp;&gt;/dev/null
  <span class="hljs-keyword">if</span> [ $? -ne 0 ];<span class="hljs-keyword">then</span>
    useradd -s /sbin/nologin nginx
  <span class="hljs-keyword">fi</span>
  <span class="hljs-keyword">if</span> [ -f nginx‐1.8.0.tar.gz ];<span class="hljs-keyword">then</span>
    tar -xf nginx‐1.8.0.tar.gz
    <span class="hljs-built_in">cd</span> nginx‐1.8.0
    yum -y install  gcc pcre‐devel openssl‐devel zlib‐devel make
    ./configure ‐‐prefix=/usr/<span class="hljs-built_in">local</span>/nginx ‐‐with‐http_ssl_module
    make
    make install
    ln -s /usr/<span class="hljs-built_in">local</span>/nginx/sbin/nginx /usr/sbin/
    <span class="hljs-built_in">cd</span> ..
  <span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有 Nginx 源码包"</span>
  <span class="hljs-keyword">fi</span>
}
 
<span class="hljs-function"><span class="hljs-title">install_mysql</span></span>()
{
  yum -y install gcc gcc‐c++ cmake ncurses‐devel perl
  id mysql &amp;&gt;/dev/null
  <span class="hljs-keyword">if</span> [ $? -ne 0 ];<span class="hljs-keyword">then</span>
    useradd -s /sbin/nologin mysql
  <span class="hljs-keyword">fi</span>
  <span class="hljs-keyword">if</span> [ -f mysql‐5.6.25.tar.gz ];<span class="hljs-keyword">then</span>
    tar -xf mysql‐5.6.25.tar.gz
    <span class="hljs-built_in">cd</span> mysql‐5.6.25
    cmake .
    make
    make install
    /usr/<span class="hljs-built_in">local</span>/mysql/scripts/mysql_install_db ‐‐user=mysql ‐‐datadir=/usr/<span class="hljs-built_in">local</span>/mysql/data/
‐‐basedir=/usr/<span class="hljs-built_in">local</span>/mysql/
    chown -R root.mysql /usr/<span class="hljs-built_in">local</span>/mysql
    chown -R mysql /usr/<span class="hljs-built_in">local</span>/mysql/data
    /bin/cp -f /usr/<span class="hljs-built_in">local</span>/mysql/support‐files/mysql.server /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    /bin/cp -f /usr/<span class="hljs-built_in">local</span>/mysql/support‐files/my‐default.cnf /etc/my.cnf
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"/usr/local/mysql/lib/"</span> &gt;&gt; /etc/ld.so.conf
    ldconfig
    <span class="hljs-built_in">echo</span> <span class="hljs-string">'PATH=\$PATH:/usr/local/mysql/bin/'</span> &gt;&gt; /etc/profile
    <span class="hljs-built_in">export</span> PATH
  <span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span>  <span class="hljs-string">"没有 mysql 源码包"</span>
    <span class="hljs-built_in">exit</span>
  <span class="hljs-keyword">fi</span>
}
 
<span class="hljs-function"><span class="hljs-title">install_php</span></span>()
{
<span class="hljs-comment">#安装 php 时没有指定启动哪些模块功能,如果的用户可以根据实际情况自行添加额外功能如‐‐with‐gd 等</span>
yum  -y  install  gcc  libxml2‐devel
<span class="hljs-keyword">if</span> [ -f mhash‐0.9.9.9.tar.gz ];<span class="hljs-keyword">then</span>
  tar -xf mhash‐0.9.9.9.tar.gz
  <span class="hljs-built_in">cd</span> mhash‐0.9.9.9
  ./configure
  make
  make install
  <span class="hljs-built_in">cd</span> ..
<span class="hljs-keyword">if</span> [ ! ‐f /usr/lib/libmhash.so ];<span class="hljs-keyword">then</span>
  ln -s /usr/<span class="hljs-built_in">local</span>/lib/libmhash.so /usr/lib/
<span class="hljs-keyword">fi</span>
ldconfig
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有 mhash 源码包文件"</span>
  <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-keyword">if</span> [ -f libmcrypt‐2.5.8.tar.gz ];<span class="hljs-keyword">then</span>
  tar -xf libmcrypt‐2.5.8.tar.gz
  <span class="hljs-built_in">cd</span> libmcrypt‐2.5.8
  ./configure
  make
  make install
  <span class="hljs-built_in">cd</span> ..
  <span class="hljs-keyword">if</span> [ ! -f /usr/lib/libmcrypt.so ];<span class="hljs-keyword">then</span>  
    ln -s /usr/<span class="hljs-built_in">local</span>/lib/libmcrypt.so /usr/lib/
  <span class="hljs-keyword">fi</span>
  ldconfig
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有 libmcrypt 源码包文件"</span>
  <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-keyword">if</span> [ -f php‐5.4.24.tar.gz ];<span class="hljs-keyword">then</span>
  tar -xf php‐5.4.24.tar.gz
  <span class="hljs-built_in">cd</span> php‐5.4.24
  ./configure  ‐‐prefix=/usr/<span class="hljs-built_in">local</span>/php5  ‐‐with‐mysql=/usr/<span class="hljs-built_in">local</span>/mysql  ‐‐<span class="hljs-built_in">enable</span>‐fpm    ‐‐
  <span class="hljs-built_in">enable</span>‐mbstring  ‐‐with‐mcrypt  ‐‐with‐mhash  ‐‐with‐config‐file‐path=/usr/<span class="hljs-built_in">local</span>/php5/etc  ‐‐with‐
  mysqli=/usr/<span class="hljs-built_in">local</span>/mysql/bin/mysql_config
  make &amp;&amp; make install
  /bin/cp -f php.ini‐production /usr/<span class="hljs-built_in">local</span>/php5/etc/php.ini
  /bin/cp -f /usr/<span class="hljs-built_in">local</span>/php5/etc/php‐fpm.conf.default /usr/<span class="hljs-built_in">local</span>/php5/etc/php‐fpm.conf
  <span class="hljs-built_in">cd</span> ..
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有 php 源码包文件"</span>
  <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">fi</span> 
}
 
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
  menu
  choice
  <span class="hljs-keyword">case</span> <span class="hljs-variable">$select</span> <span class="hljs-keyword">in</span>
  1)
    install_nginx
    ;;
  2)
    install_mysql
    ;;
  3)
    install_php
    ;;
  4)
    <span class="hljs-built_in">exit</span>
    ;;
  *)
    <span class="hljs-built_in">echo</span> Sorry!
  <span class="hljs-keyword">esac</span>
<span class="hljs-keyword">done</span>
</code>

23、编写脚本快速克隆 KVM 虚拟机

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写脚本快速克隆 KVM 虚拟机</span>
 
<span class="hljs-comment"># 本脚本针对 RHEL7.2 或 Centos7.2</span>
<span class="hljs-comment"># 本脚本需要提前准备一个 qcow2 格式的虚拟机模板,</span>
<span class="hljs-comment"># 名称为/var/lib/libvirt/images  /.rh7_template 的虚拟机模板</span>
<span class="hljs-comment"># 该脚本使用 qemu‐img 命令快速创建快照虚拟机</span>
<span class="hljs-comment"># 脚本使用 sed 修改模板虚拟机的配置文件,将虚拟机名称、UUID、磁盘文件名、MAC 地址</span>
<span class="hljs-comment"># exit code:  </span>
<span class="hljs-comment">#    65 ‐&gt; user input nothing</span>
<span class="hljs-comment">#    66 ‐&gt; user input is not a number</span>
<span class="hljs-comment">#    67 ‐&gt; user input out of range</span>
<span class="hljs-comment">#    68 ‐&gt; vm disk image exists</span>
 
IMG_DIR=/var/lib/libvirt/images
BASEVM=rh7_template
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"Enter VM number: "</span> VMNUM
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$VMNUM</span> -le 9 ];<span class="hljs-keyword">then</span>
VMNUM=0<span class="hljs-variable">$VMNUM</span>
<span class="hljs-keyword">fi</span>
 
<span class="hljs-keyword">if</span> [ -z <span class="hljs-string">"<span class="hljs-variable">${VMNUM}</span>"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"You must input a number."</span>
    <span class="hljs-built_in">exit</span> 65
<span class="hljs-keyword">elif</span> [[  <span class="hljs-variable">${VMNUM}</span> =~ [a‐z]  ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"You must input a number."</span>
    <span class="hljs-built_in">exit</span> 66
<span class="hljs-keyword">elif</span> [ <span class="hljs-variable">${VMNUM}</span> -lt 1 -o <span class="hljs-variable">${VMNUM}</span> -gt 99 ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Input out of range"</span>
    <span class="hljs-built_in">exit</span> 67
<span class="hljs-keyword">fi</span>
 
NEWVM=rh7_node<span class="hljs-variable">${VMNUM}</span>
 
<span class="hljs-keyword">if</span> [ -e <span class="hljs-variable">$IMG_DIR</span>/<span class="hljs-variable">${NEWVM}</span>.img ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"File exists."</span>
    <span class="hljs-built_in">exit</span> 68
<span class="hljs-keyword">fi</span>
 
<span class="hljs-built_in">echo</span> -en <span class="hljs-string">"Creating Virtual Machine disk image......\t"</span>
qemu‐img create -f qcow2 ‐b <span class="hljs-variable">$IMG_DIR</span>/.<span class="hljs-variable">${BASEVM}</span>.img <span class="hljs-variable">$IMG_DIR</span>/<span class="hljs-variable">${NEWVM}</span>.img &amp;&gt; /dev/null
 
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\e[32;1m[OK]\e[0m"</span>
 
<span class="hljs-comment">#virsh dumpxml ${BASEVM} &gt; /tmp/myvm.xml</span>
cat /var/lib/libvirt/images/.rhel7.xml &gt; /tmp/myvm.xml
sed -i <span class="hljs-string">"/&lt;name&gt;<span class="hljs-variable">${BASEVM}</span>/s/<span class="hljs-variable">${BASEVM}</span>/<span class="hljs-variable">${NEWVM}</span>/"</span> /tmp/myvm.xml
sed -i <span class="hljs-string">"/uuid/s/&lt;uuid&gt;.*&lt;\/uuid&gt;/&lt;uuid&gt;<span class="hljs-variable">$(uuidgen)</span>&lt;\/uuid&gt;/"</span> /tmp/myvm.xml
sed -i <span class="hljs-string">"/<span class="hljs-variable">${BASEVM}</span>\.img/s/<span class="hljs-variable">${BASEVM}</span>/<span class="hljs-variable">${NEWVM}</span>/"</span> /tmp/myvm.xml
 
<span class="hljs-comment"># 修改 MAC 地址,本例使用的是常量,每位使用该脚本的用户需要根据实际情况修改这些值 </span>
<span class="hljs-comment"># 最好这里可以使用便利,这样更适合于批量操作,可以克隆更多虚拟机 </span>
sed -i <span class="hljs-string">"/mac /s/a1/0c/"</span> /tmp/myvm.xml
 
<span class="hljs-built_in">echo</span> -en <span class="hljs-string">"Defining new virtual machine......\t\t"</span>
virsh define /tmp/myvm.xml &amp;&gt; /dev/null
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\e[32;1m[OK]\e[0m"</span>
</code>

24、点名器脚本

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写一个点名器脚本</span>
 
<span class="hljs-comment"># 该脚本,需要提前准备一个 user.txt 文件</span>
<span class="hljs-comment"># 该文件中需要包含所有姓名的信息,一行一个姓名,脚本每次随机显示一个姓名</span>
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
<span class="hljs-comment">#统计 user 文件中有多少用户</span>
line=`cat user.txt |wc ‐l`
num=$[RANDOM%line+1]
sed -n <span class="hljs-string">"<span class="hljs-variable">${num}</span>p"</span>  user.txt
sleep 0.2
clear
<span class="hljs-keyword">done</span>
</code>

25、查看有多少远程的 IP 在连接本机

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 查看有多少远程的 IP 在连接本机(不管是通过 ssh 还是 web 还是 ftp 都统计) </span>
 
<span class="hljs-comment"># 使用 netstat ‐atn 可以查看本机所有连接的状态,‐a 查看所有,</span>
<span class="hljs-comment"># -t仅显示 tcp 连接的信息,‐n 数字格式显示</span>
<span class="hljs-comment"># Local Address(第四列是本机的 IP 和端口信息)</span>
<span class="hljs-comment"># Foreign Address(第五列是远程主机的 IP 和端口信息)</span>
<span class="hljs-comment"># 使用 awk 命令仅显示第 5 列数据,再显示第 1 列 IP 地址的信息</span>
<span class="hljs-comment"># sort 可以按数字大小排序,最后使用 uniq 将多余重复的删除,并统计重复的次数</span>
netstat -atn  |  awk  <span class="hljs-string">'{print $5}'</span>  | awk  <span class="hljs-string">'{print $1}'</span> | sort -nr  |  uniq -c
</code>

26、对 100 以内的所有正整数相加求和(1+2+3+4…+100)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 对 100 以内的所有正整数相加求和(1+2+3+4...+100)</span>
 
<span class="hljs-comment">#seq 100 可以快速自动生成 100 个整数</span>
sum=0
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `seq 100`
<span class="hljs-keyword">do</span>
    sum=$[sum+i]
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"总和是:<span class="hljs-variable">$sum</span>"</span>
</code>

27、统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个</span>
 
<span class="hljs-comment"># awk 使用‐F 选项指定文件内容的分隔符是/或者:</span>
<span class="hljs-comment"># 条件判断$7:$8 大于等于 13:30,并且要求,$7:$8 小于等于 14:30</span>
<span class="hljs-comment"># 最后使用 wc ‐l 统计这样的数据有多少行,即多少个</span>
awk -F <span class="hljs-string">"[ /:]"</span> <span class="hljs-string">'$7":"$8&gt;="13:30" &amp;&amp; $7":"$8&lt;="14:30"'</span> /var/<span class="hljs-built_in">log</span>/httpd/access_log |wc -l
</code>

28、统计 13:30 到 14:30 所有访问本机 Aapche 服务器的远程 IP 地址是什么

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 统计 13:30 到 14:30 所有访问本机 Aapche 服务器的远程 IP 地址是什么 </span>
<span class="hljs-comment"># awk 使用‐F 选项指定文件内容的分隔符是/或者:</span>
<span class="hljs-comment"># 条件判断$7:$8 大于等于 13:30,并且要求,$7:$8 小于等于 14:30</span>
<span class="hljs-comment"># 日志文档内容里面,第 1 列是远程主机的 IP 地址,使用 awk 单独显示第 1 列即可</span>
awk -F <span class="hljs-string">"[ /:]"</span> <span class="hljs-string">'$7":"$8&gt;="13:30" &amp;&amp; $7":"$8&lt;="14:30"{print $1}'</span> /var/<span class="hljs-built_in">log</span>/httpd/access_log
</code>

29、打印国际象棋棋盘

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 打印国际象棋棋盘</span>
<span class="hljs-comment"># 设置两个变量,i 和 j,一个代表行,一个代表列,国际象棋为 8*8 棋盘</span>
<span class="hljs-comment"># i=1 是代表准备打印第一行棋盘,第 1 行棋盘有灰色和蓝色间隔输出,总共为 8 列</span>
<span class="hljs-comment"># i=1,j=1 代表第 1 行的第 1 列;i=2,j=3 代表第 2 行的第 3 列</span>
<span class="hljs-comment"># 棋盘的规律是 i+j 如果是偶数,就打印蓝色色块,如果是奇数就打印灰色色块</span>
<span class="hljs-comment"># 使用 echo ‐ne 打印色块,并且打印完成色块后不自动换行,在同一行继续输出其他色块</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..8}
<span class="hljs-keyword">do</span>
    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> {1..8}
    <span class="hljs-keyword">do</span>
      sum=$[i+j]
    <span class="hljs-keyword">if</span> [  $[sum%2] -eq 0 ];<span class="hljs-keyword">then</span>
       <span class="hljs-built_in">echo</span> -ne <span class="hljs-string">"\033[46m  \033[0m"</span>
    <span class="hljs-keyword">else</span>
      <span class="hljs-built_in">echo</span> -ne <span class="hljs-string">"\033[47m  \033[0m"</span>
    <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">done</span>
    <span class="hljs-built_in">echo</span>
<span class="hljs-keyword">done</span>
</code>

30、统计每个远程 IP 访问了本机 apache 几次?

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 统计每个远程 IP 访问了本机 apache 几次? </span>
awk  <span class="hljs-string">'{ip[$1]++}END{for(i in ip){print ip[i],i}}'</span>  /var/<span class="hljs-built_in">log</span>/httpd/access_log
</code>

31、统计当前 Linux 系统中可以登录计算机的账户有多少个

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 统计当前 Linux 系统中可以登录计算机的账户有多少个</span>
<span class="hljs-comment">#方法 1:</span>
grep <span class="hljs-string">"bash$"</span> /etc/passwd | wc -l
<span class="hljs-comment">#方法 2:</span>
awk -f: <span class="hljs-string">'/bash$/{x++}end{print x}'</span>  /etc/passwd
</code>

32、统计/var/log 有多少个文件,并显示这些文件名

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 统计/var/log 有多少个文件,并显示这些文件名 </span>
<span class="hljs-comment"># 使用 ls 递归显示所有,再判断是否为文件,如果是文件则计数器加 1</span>
<span class="hljs-built_in">cd</span>  /var/<span class="hljs-built_in">log</span>
sum=0
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `ls -r *`
<span class="hljs-keyword">do</span>
   <span class="hljs-keyword">if</span> [ -f <span class="hljs-variable">$i</span> ];<span class="hljs-keyword">then</span>
       <span class="hljs-built_in">let</span> sum++
         <span class="hljs-built_in">echo</span> <span class="hljs-string">"文件名:<span class="hljs-variable">$i</span>"</span>
     <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"总文件数量为:<span class="hljs-variable">$sum</span>"</span>
</code>

33、自动为其他脚本添加解释器信息

Docker+K8s+Jenkins 主流技术全解视频资料【干货免费分享】

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 自动为其他脚本添加解释器信息#!/bin/bash,如脚本名为 test.sh 则效果如下: </span>
<span class="hljs-comment"># ./test.sh  abc.sh  自动为 abc.sh 添加解释器信息</span>
<span class="hljs-comment"># ./test.sh  user.sh  自动为 user.sh 添加解释器信息</span>
 
<span class="hljs-comment"># 先使用 grep 判断对象脚本是否已经有解释器信息,如果没有则使用 sed 添加解释器以及描述信息</span>
<span class="hljs-keyword">if</span>  !  grep  -q  <span class="hljs-string">"^#!"</span>  <span class="hljs-variable">$1</span>; <span class="hljs-keyword">then</span>
sed  <span class="hljs-string">'1i #!/bin/bash'</span>  <span class="hljs-variable">$1</span>
sed  <span class="hljs-string">'2i #Description: '</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment"># 因为每个脚本的功能不同,作用不同,所以在给对象脚本添加完解释器信息,以及 Description 后还希望</span>
<span class="hljs-comment"># 继续编辑具体的脚本功能的描述信息,这里直接使用 vim 把对象脚本打开,并且光标跳转到该文件的第 2 行</span>
vim +2 <span class="hljs-variable">$1</span>
</code>

34、自动化部署 varnish 源码包软件

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 自动化部署 varnish 源码包软件 </span>
<span class="hljs-comment"># 本脚本需要提前下载 varnish‐3.0.6.tar.gz 这样一个源码包软件,该脚本即可用自动源码安装部署软件</span>
 
yum -y install gcc readline‐devel pcre‐devel
useradd -s /sbin/nologin varnish
tar -xf varnish‐3.0.6.tar.gz
<span class="hljs-built_in">cd</span> varnish‐3.0.6
 
<span class="hljs-comment"># 使用 configure,make,make install 源码安装软件包</span>
./configure ‐‐prefix=/usr/<span class="hljs-built_in">local</span>/varnish
make &amp;&amp; make install
 
<span class="hljs-comment"># 在源码包目录下,将相应的配置文件拷贝到 Linux 系统文件系统中</span>
<span class="hljs-comment"># 默认安装完成后,不会自动拷贝或安装配置文件到 Linux 系统,所以需要手动 cp 复制配置文件</span>
<span class="hljs-comment"># 并使用 uuidgen 生成一个随机密钥的配置文件</span>
 
cp redhat/varnish.initrc /etc/init.d/varnish
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
cp redhat/varnish_reload_vcl /usr/bin/
ln -s /usr/<span class="hljs-built_in">local</span>/varnish/sbin/varnishd /usr/sbin/
ln -s /usr/<span class="hljs-built_in">local</span>/varnish/bin/* /usr/bin
mkdir /etc/varnish
cp /usr/<span class="hljs-built_in">local</span>/varnish/etc/varnish/default.vcl /etc/varnish/
uuidgen &gt; /etc/varnish/secret
</code>

35、编写 nginx 启动脚本

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 编写 nginx 启动脚本 </span>
<span class="hljs-comment"># 本脚本编写完成后,放置在/etc/init.d/目录下,就可以被 Linux 系统自动识别到该脚本</span>
<span class="hljs-comment"># 如果本脚本名为/etc/init.d/nginx,则 service nginx start 就可以启动该服务</span>
<span class="hljs-comment"># service nginx stop 就可以关闭服务</span>
<span class="hljs-comment"># service nginx restart 可以重启服务</span>
<span class="hljs-comment"># service nginx status 可以查看服务状态</span>
 
program=/usr/<span class="hljs-built_in">local</span>/nginx/sbin/nginx
pid=/usr/<span class="hljs-built_in">local</span>/nginx/logs/nginx.pid
<span class="hljs-function"><span class="hljs-title">start</span></span>(){
<span class="hljs-keyword">if</span> [ -f <span class="hljs-variable">$pid</span> ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span>  <span class="hljs-string">"nginx 服务已经处于开启状态"</span>
<span class="hljs-keyword">else</span>
  <span class="hljs-variable">$program</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-function"><span class="hljs-title">stop</span></span>(){
<span class="hljs-keyword">if</span> [ -! -f <span class="hljs-variable">$pid</span> ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"nginx 服务已经关闭"</span>
<span class="hljs-keyword">else</span>
  <span class="hljs-variable">$program</span> -s stop
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"关闭服务 ok"</span>
<span class="hljs-keyword">fi</span>
}
<span class="hljs-function"><span class="hljs-title">status</span></span>(){
<span class="hljs-keyword">if</span> [ -f <span class="hljs-variable">$pid</span> ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"服务正在运行..."</span>
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"服务已经关闭"</span>
<span class="hljs-keyword">fi</span>
}
 
<span class="hljs-keyword">case</span> <span class="hljs-variable">$1</span> <span class="hljs-keyword">in</span>
start)
  start;;
stop)
  stop;;
restart)
  stop
  sleep 1
  start;;
status)
  status;;
*)
  <span class="hljs-built_in">echo</span>  <span class="hljs-string">"你输入的语法格式错误"</span>
<span class="hljs-keyword">esac</span>
</code>

36、自动对磁盘分区、格式化、挂载

<code><span class="hljs-comment"> #!/bin/bash</span>
<span class="hljs-comment"> 
# 自动对磁盘分区、格式化、挂载</span>
<span class="hljs-comment"># 对虚拟机的 vdb 磁盘进行分区格式化,使用&lt;&lt;将需要的分区指令导入给程序 fdisk</span>
<span class="hljs-comment"># n(新建分区),p(创建主分区),1(分区编号为 1),两个空白行(两个回车,相当于将整个磁盘分一个区)</span>
<span class="hljs-comment"># 注意:1 后面的两个回车(空白行)是必须的!</span>
<span class="hljs-attr">fdisk</span> <span class="hljs-string">/dev/vdb &lt;&lt; EOF</span>
<span class="hljs-attr">n</span>
<span class="hljs-attr">p</span>
<span class="hljs-attr">1</span>
 
 
<span class="hljs-attr">wq</span>
<span class="hljs-attr">EOF</span>
<span class="hljs-comment"> 
#格式化刚刚创建好的分区</span>
<span class="hljs-meta">mkfs.xfs</span>   <span class="hljs-string">/dev/vdb1</span>
<span class="hljs-comment"> 
#创建挂载点目录</span>
<span class="hljs-attr">if</span> <span class="hljs-string">[ -e /data ]; then</span>
<span class="hljs-attr">exit</span>
<span class="hljs-attr">fi</span>
<span class="hljs-attr">mkdir</span> <span class="hljs-string">/data</span>
<span class="hljs-comment"> 
#自动挂载刚刚创建的分区,并设置开机自动挂载该分区</span>
<span class="hljs-attr">echo</span> <span class="hljs-string">'/dev/vdb1     /data    xfs    defaults        1 2'  &gt;&gt; /etc/fstab</span>
<span class="hljs-attr">mount</span> <span class="hljs-string">-a</span>
</code>

37、自动优化 Linux 内核参数

<code><span class="hljs-comment">#!/bin/bash</span>

<span class="hljs-comment"># 自动优化 Linux 内核参数</span>
 
<span class="hljs-comment">#脚本针对 RHEL7</span>
<span class="hljs-string">cat</span> <span class="hljs-string">&gt;&gt;</span> <span class="hljs-string">/usr/lib/sysctl.d/00‐system.conf</span> <span class="hljs-string">&lt;&lt;EOF</span>
<span class="hljs-string">fs.file‐max=65535</span>
<span class="hljs-string">net.ipv4.tcp_timestamps</span> <span class="hljs-string">=</span> <span class="hljs-number">0</span>
<span class="hljs-string">net.ipv4.tcp_synack_retries</span> <span class="hljs-string">=</span> <span class="hljs-number">5</span>
<span class="hljs-string">net.ipv4.tcp_syn_retries</span> <span class="hljs-string">=</span> <span class="hljs-number">5</span>
<span class="hljs-string">net.ipv4.tcp_tw_recycle</span> <span class="hljs-string">=</span> <span class="hljs-number">1</span>
<span class="hljs-string">net.ipv4.tcp_tw_reuse</span> <span class="hljs-string">=</span> <span class="hljs-number">1</span>
<span class="hljs-string">net.ipv4.tcp_fin_timeout</span> <span class="hljs-string">=</span> <span class="hljs-number">30</span>
<span class="hljs-comment">#net.ipv4.tcp_keepalive_time = 120</span>
<span class="hljs-string">net.ipv4.ip_local_port_range</span> <span class="hljs-string">=</span> <span class="hljs-number">1024</span>  <span class="hljs-number">65535</span>
<span class="hljs-string">kernel.shmall</span> <span class="hljs-string">=</span> <span class="hljs-number">2097152</span>
<span class="hljs-string">kernel.shmmax</span> <span class="hljs-string">=</span> <span class="hljs-number">2147483648</span>
<span class="hljs-string">kernel.shmmni</span> <span class="hljs-string">=</span> <span class="hljs-number">4096</span>
<span class="hljs-string">kernel.sem</span> <span class="hljs-string">=</span> <span class="hljs-number">5010</span> <span class="hljs-number">641280</span> <span class="hljs-number">5010</span> <span class="hljs-number">128</span>
<span class="hljs-string">net.core.wmem_default=262144</span>
<span class="hljs-string">net.core.wmem_max=262144</span>
<span class="hljs-string">net.core.rmem_default=4194304</span>
<span class="hljs-string">net.core.rmem_max=4194304</span>
<span class="hljs-string">net.ipv4.tcp_fin_timeout</span> <span class="hljs-string">=</span> <span class="hljs-number">10</span>
<span class="hljs-string">net.ipv4.tcp_keepalive_time</span> <span class="hljs-string">=</span> <span class="hljs-number">30</span>
<span class="hljs-string">net.ipv4.tcp_window_scaling</span> <span class="hljs-string">=</span> <span class="hljs-number">0</span>
<span class="hljs-string">net.ipv4.tcp_sack</span> <span class="hljs-string">=</span> <span class="hljs-number">0</span>
<span class="hljs-string">EOF</span>
 
<span class="hljs-string">sysctl</span> <span class="hljs-string">–p</span>
</code>

38、切割 Nginx 日志文件(防止单个文件过大,后期处理很困难)

<code><span class="hljs-comment">#mkdir  /data/scripts</span>
<span class="hljs-comment">#vim   /data/scripts/nginx_log.sh  </span>
<span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 切割 Nginx 日志文件(防止单个文件过大,后期处理很困难) </span>
logs_path=<span class="hljs-string">"/usr/local/nginx/logs/"</span>
mv <span class="hljs-variable">${logs_path}</span>access.log <span class="hljs-variable">${logs_path}</span>access_$(date -d <span class="hljs-string">"yesterday"</span> +<span class="hljs-string">"%Y%m%d"</span>).<span class="hljs-built_in">log</span>
<span class="hljs-built_in">kill</span> -USR1  `cat /usr/<span class="hljs-built_in">local</span>/nginx/logs/nginx.pid`
 
<span class="hljs-comment"># chmod +x  /data/scripts/nginx_log.sh</span>
<span class="hljs-comment"># crontab  ‐e                    #脚本写完后,将脚本放入计划任务每天执行一次脚本</span>
0  1  *  *   *   /data/scripts/nginx_log.sh
</code>

39、检测 MySQL 数据库连接数量

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 检测 MySQL 数据库连接数量 </span>
 
<span class="hljs-comment"># 本脚本每 2 秒检测一次 MySQL 并发连接数,可以将本脚本设置为开机启动脚本,或在特定时间段执行</span>
<span class="hljs-comment"># 以满足对 MySQL 数据库的监控需求,查看 MySQL 连接是否正常</span>
<span class="hljs-comment"># 本案例中的用户名和密码需要根据实际情况修改后方可使用</span>
log_file=/var/<span class="hljs-built_in">log</span>/mysql_count.log
user=root
passwd=123456
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
    sleep 2
    count=`mysqladmin  -u  <span class="hljs-string">"<span class="hljs-variable">$user</span>"</span>  -p  <span class="hljs-string">"<span class="hljs-variable">$passwd</span>"</span>   status |  awk <span class="hljs-string">'{print $4}'</span>`
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"`date +%Y‐%m‐%d` 并发连接数为:<span class="hljs-variable">$count</span>"</span> &gt;&gt; <span class="hljs-variable">$log_file</span>
<span class="hljs-keyword">done</span>
</code>

40、根据 md5 校验码,检测文件是否被修改

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 根据 md5 校验码,检测文件是否被修改 </span>
<span class="hljs-comment"># 本示例脚本检测的是/etc 目录下所有的 conf 结尾的文件,根据实际情况,您可以修改为其他目录或文件</span>
<span class="hljs-comment"># 本脚本在目标数据没有被修改时执行一次,当怀疑数据被人篡改,再执行一次</span>
<span class="hljs-comment"># 将两次执行的结果做对比,MD5 码发生改变的文件,就是被人篡改的文件</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> $(ls /etc/*.conf)
<span class="hljs-keyword">do</span>
  md5sum <span class="hljs-string">"<span class="hljs-variable">$i</span>"</span> &gt;&gt; /var/<span class="hljs-built_in">log</span>/conf_file.log
<span class="hljs-keyword">done</span>
</code>

41、检测 MySQL 服务是否存活

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 检测 MySQL 服务是否存活 </span>
 
<span class="hljs-comment"># host 为你需要检测的 MySQL 主机的 IP 地址,user 为 MySQL 账户名,passwd 为密码</span>
<span class="hljs-comment"># 这些信息需要根据实际情况修改后方可使用</span>
host=192.168.51.198
user=root
passwd=123456
mysqladmin -h <span class="hljs-string">'$host'</span> -u <span class="hljs-string">'$user'</span> -p<span class="hljs-string">'$passwd'</span> ping &amp;&gt;/dev/null
<span class="hljs-keyword">if</span> [ $? -eq 0 ]
<span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"MySQL is UP"</span>
<span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"MySQL is down"</span>
<span class="hljs-keyword">fi</span>
</code>

42、备份 MySQL 的 shell 脚本(mysqldump版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 备份 MySQL 的 shell 脚本(mysqldump版本) </span>
 
<span class="hljs-comment"># 定义变量 user(数据库用户名),passwd(数据库密码),date(备份的时间标签)</span>
<span class="hljs-comment"># dbname(需要备份的数据库名称,根据实际需求需要修改该变量的值,默认备份 mysql 数据库)</span>
 
user=root
passwd=123456
dbname=mysql
date=$(date +%Y%m%d)
 
<span class="hljs-comment"># 测试备份目录是否存在,不存在则自动创建该目录</span>
[ ! -d /mysqlbackup ] &amp;&amp; mkdir /mysqlbackup
<span class="hljs-comment"># 使用 mysqldump 命令备份数据库</span>
mysqldump -u <span class="hljs-string">"<span class="hljs-variable">$user</span>"</span> -p <span class="hljs-string">"<span class="hljs-variable">$passwd</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$dbname</span>"</span> &gt; /mysqlbackup/<span class="hljs-string">"<span class="hljs-variable">$dbname</span>"</span>-<span class="hljs-variable">${date}</span>.sql
</code>

43、将文件中所有的小写字母转换为大写字母

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 将文件中所有的小写字母转换为大写字母 </span>
 
<span class="hljs-comment"># $1是位置参数,是你需要转换大小写字母的文件名称</span>
<span class="hljs-comment"># 执行脚本,给定一个文件名作为参数,脚本就会将该文件中所有的小写字母转换为大写字母</span>
tr <span class="hljs-string">"[a‐z]"</span> <span class="hljs-string">"[A‐Z]"</span> &lt; <span class="hljs-variable">$1</span>
</code>

44、非交互自动生成 SSH 密钥文件

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 非交互自动生成 SSH 密钥文件 </span>
 
<span class="hljs-comment"># ‐t 指定 SSH 密钥的算法为 RSA 算法;‐N 设置密钥的密码为空;‐f 指定生成的密钥文件&gt;存放在哪里</span>
rm  -rf  ~/.ssh/{known_hosts,id_rsa*}
ssh‐keygen -t RSA -N <span class="hljs-string">''</span> -f ~/.ssh/id_rsa
</code>

45、检查特定的软件包是否已经安装

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 检查特定的软件包是否已经安装 </span>
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$#</span> -eq 0 ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"你需要制定一个软件包名称作为脚本参数"</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"用法:<span class="hljs-variable">$0</span> 软件包名称 ..."</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment"># $@提取所有的位置变量的值,相当于$*</span>
<span class="hljs-keyword">for</span> package <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">$@</span>"</span>
<span class="hljs-keyword">do</span>
    <span class="hljs-keyword">if</span> rpm -q <span class="hljs-variable">${package}</span> &amp;&gt;/dev/null ;<span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"<span class="hljs-variable">${package}</span>\033[32m 已经安装\033[0m"</span>
    <span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"<span class="hljs-variable">${package}</span>\033[34;1m 未安装\033[0m"</span>
    <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
</code>

46、监控 HTTP 服务器的状态(测试返回码)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 监控 HTTP 服务器的状态(测试返回码)</span>
 
<span class="hljs-comment"># 设置变量,url为你需要检测的目标网站的网址(IP 或域名),比如百度</span>
url=http://http://183.232.231.172/index.html
 
<span class="hljs-comment"># 定义函数 check_http:</span>
<span class="hljs-comment"># 使用 curl 命令检查 http 服务器的状态</span>
<span class="hljs-comment"># ‐m 设置curl不管访问成功或失败,最大消耗的时间为 5 秒,5 秒连接服务为相应则视为无法连接</span>
<span class="hljs-comment"># ‐s 设置静默连接,不显示连接时的连接速度、时间消耗等信息</span>
<span class="hljs-comment"># ‐o 将 curl 下载的页面内容导出到/dev/null(默认会在屏幕显示页面内容)</span>
<span class="hljs-comment"># ‐w 设置curl命令需要显示的内容%{http_code},指定curl返回服务器的状态码</span>
<span class="hljs-function"><span class="hljs-title">check_http</span></span>()
{
        status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} <span class="hljs-variable">$url</span>)
}
 
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
        check_http
        date=$(date +%Y%m%d‐%H:%M:%S)
 
<span class="hljs-comment"># 生成报警邮件的内容</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"当前时间为:<span class="hljs-variable">$date</span>
        <span class="hljs-variable">$url</span> 服务器异常,状态码为<span class="hljs-variable">${status_code}</span>.
        请尽快排查异常."</span> &gt; /tmp/http$$.pid
 
<span class="hljs-comment"># 指定测试服务器状态的函数,并根据返回码决定是发送邮件报警还是将正常信息写入日志</span>
        <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$status_code</span> -ne 200 ];<span class="hljs-keyword">then</span>
                mail -s Warning root &lt; /tmp/http$$.pid
        <span class="hljs-keyword">else</span>
                <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$url</span> 连接正常"</span> &gt;&gt; /var/<span class="hljs-built_in">log</span>/http.log
        <span class="hljs-keyword">fi</span>
        sleep 5
<span class="hljs-keyword">done</span>
</code>

47、自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)</span>
<span class="hljs-comment"># </span>
<span class="hljs-comment"># 设置变量定义需要添加到防火墙规则的服务和端口号</span>
<span class="hljs-comment"># 使用 firewall‐cmd ‐‐get‐services 可以查看 firewall 支持哪些服务</span>
service=<span class="hljs-string">"nfs http ssh"</span>
port=<span class="hljs-string">"80 22 8080"</span>
 
<span class="hljs-comment"># 循环将每个服务添加到防火墙规则中</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-variable">$service</span>
<span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Adding <span class="hljs-variable">$i</span> service to firewall"</span>
    firewall‐cmd  --add-service=<span class="hljs-variable">${i}</span>
<span class="hljs-keyword">done</span>
 
<span class="hljs-comment">#循环将每个端口添加到防火墙规则中</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-variable">$port</span>
<span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Adding <span class="hljs-variable">$i</span> Port to firewall"</span>
    firewall‐cmd --add-port=<span class="hljs-variable">${i}</span>/tcp
<span class="hljs-keyword">done</span>
<span class="hljs-comment">#将以上设置的临时防火墙规则,转换为永久有效的规则(确保重启后有效)</span>
firewall‐cmd  --runtime-to-permanent
</code>

48、使用脚本自动创建逻辑卷

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用脚本自动创建逻辑卷 </span>
 
<span class="hljs-comment"># 清屏,显示警告信息,创建将磁盘转换为逻辑卷会删除数据</span>
clear
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[32m           !!!!!!警告(Warning)!!!!!!\033[0m"</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"+++++++++++++++++++++++++++++++++++++++++++++++++"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"脚本会将整个磁盘转换为 PV,并删除磁盘上所有数据!!!"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"This Script will destroy all data on the Disk"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"+++++++++++++++++++++++++++++++++++++++++++++++++"</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请问是否继续 y/n?:"</span> sure
 
<span class="hljs-comment"># 测试用户输入的是否为 y,如果不是则退出脚本</span>
[ <span class="hljs-variable">$sure</span> != y ] &amp;&amp; <span class="hljs-built_in">exit</span>
 
<span class="hljs-comment"># 提示用户输入相关参数(磁盘、卷组名称等数据),并测试用户是否输入了这些值,如果没有输入,则脚本退出</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入磁盘名称,如/dev/vdb:"</span> disk
[ -z <span class="hljs-variable">$disk</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有输入磁盘名称"</span> &amp;&amp; <span class="hljs-built_in">exit</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入卷组名称:"</span> vg_name
[ -z <span class="hljs-variable">$vg_name</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有输入卷组名称"</span> &amp;&amp; <span class="hljs-built_in">exit</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入逻辑卷名称:"</span> lv_name
[ -z <span class="hljs-variable">$lv_name</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有输入逻辑卷名称"</span> &amp;&amp; <span class="hljs-built_in">exit</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入逻辑卷大小:"</span> lv_size
[ -z <span class="hljs-variable">$lv_size</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有输入逻辑卷大小"</span> &amp;&amp; <span class="hljs-built_in">exit</span>
 
<span class="hljs-comment"># 使用命令创建逻辑卷</span>
pvcreate <span class="hljs-variable">$disk</span>
vgcreate <span class="hljs-variable">$vg_name</span> <span class="hljs-variable">$disk</span>
lvcreate -L <span class="hljs-variable">${lv_size}</span>M -n <span class="hljs-variable">${lv_name}</span>  <span class="hljs-variable">${vg_name}</span>
</code>

49、显示 CPU 厂商信息

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 显示 CPU 厂商信息 </span>
awk <span class="hljs-string">'/vendor_id/{print $3}'</span> /proc/cpuinfo | uniq
</code>

50、删除某个目录下大小为 0 的文件

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 删除某个目录下大小为 0 的文件</span>
 
<span class="hljs-comment">#/var/www/html 为测试目录,脚本会清空该目录下所有 0 字节的文件</span>
dir=<span class="hljs-string">"/var/www/html"</span>
find <span class="hljs-variable">$dir</span> -<span class="hljs-built_in">type</span> f -size 0 -<span class="hljs-built_in">exec</span> rm -rf {} \;
</code>

51、查找 Linux 系统中的僵尸进程

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 查找 Linux 系统中的僵尸进程</span>
 
<span class="hljs-comment"># awk 判断 ps 命令输出的第 8 列为 Z 是,显示该进程的 PID 和进程命令</span>
ps aux | awk <span class="hljs-string">'{if($8 == "Z"){print $2,$11}}'</span>
</code>

52、提示用户输入年份后判断该年是否为闰年

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 提示用户输入年份后判断该年是否为闰年</span>
 
<span class="hljs-comment"># 能被4整除并且并不能被100整除的年份是闰年</span>
<span class="hljs-comment"># 能被400整除的年份也是闰年</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入一个年份:"</span> year
 
<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$year</span>"</span> = <span class="hljs-string">""</span> ];<span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有输入年份"</span>
    <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment">#使用正则测试变量 year 中是否包含大小写字母</span>
<span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$year</span>"</span> =~ [a‐Z] ]];<span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"你输入的不是数字"</span>
    <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment"># 判断是否为闰年</span>
<span class="hljs-keyword">if</span> [ $[year % 4] -eq 0 ] &amp;&amp; [ $[year % 100] -ne 0 ];<span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$year</span>年是闰年"</span>  
<span class="hljs-keyword">elif</span> [ $[year % 400] -eq 0 ];<span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$year</span>年是闰年"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$year</span>年不是闰年"</span>
<span class="hljs-keyword">fi</span>
</code>

53、生成随机密码(urandom 版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 生成随机密码(urandom 版本) </span>
 
<span class="hljs-comment"># /dev/urandom 文件是 Linux 内置的随机设备文件</span>
<span class="hljs-comment"># cat /dev/urandom 可以看看里面的内容,ctrl+c 退出查看</span>
<span class="hljs-comment"># 查看该文件内容后,发现内容有些太随机,包括很多特殊符号,我们需要的密码不希望使用这些符号</span>
<span class="hljs-comment"># tr ‐dc '_A‐Za‐z0‐9' &lt; /dev/urandom</span>
<span class="hljs-comment"># 该命令可以将随机文件中其他的字符删除,仅保留大小写字母,数字,下划线,但是内容还是太多</span>
<span class="hljs-comment"># 我们可以继续将优化好的内容通过管道传递给 head 命令,在大量数据中仅显示头 10 个字节</span>
<span class="hljs-comment"># 注意 A 前面有个下划线</span>
tr -dc <span class="hljs-string">'_A‐Za‐z0‐9'</span> &lt;/dev/urandom | head -c 10
</code>

54、生成随机密码(字串截取版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 生成随机密码(字串截取版本) </span>
 
<span class="hljs-comment"># 设置变量 key,存储密码的所有可能性(密码库),如果还需要其他字符请自行添加其他密码字符</span>
<span class="hljs-comment"># 使用$#统计密码库的长度</span>
key=<span class="hljs-string">"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>
num=<span class="hljs-variable">${#key}</span>
<span class="hljs-comment"># 设置初始密码为空</span>
pass=<span class="hljs-string">''</span>
<span class="hljs-comment"># 循环 8 次,生成随机密码</span>
<span class="hljs-comment"># 每次都是随机数对密码库的长度取余,确保提取的密码字符不超过密码库的长度</span>
<span class="hljs-comment"># 每次循环提取一位随机密码,并将该随机密码追加到 pass 变量的最后</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..8}
<span class="hljs-keyword">do</span>  
  index=$[RANDOM%num]
  pass=<span class="hljs-variable">$pass</span><span class="hljs-variable">${key:$index:1}</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$pass</span>
</code>

55、生成随机密码(UUID 版本,16 进制密码)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 生成随机密码(UUID 版本,16 进制密码) </span>
uuidgen
56、生成随机密码(进程 ID 版本,数字密码)


<span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 生成随机密码(进程 ID 版本,数字密码)</span>
<span class="hljs-built_in">echo</span> $$
</code>

57、测试用户名与密码是否正确

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 测试用户名与密码是否正确</span>
 
<span class="hljs-comment">#用户名为 tom 并且密码为 123456,则提示登录成功,否则提示登录失败</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入用户名:"</span>  user
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入密码:"</span>    pass
<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$user</span>"</span> == <span class="hljs-string">'tom'</span> -a <span class="hljs-string">"<span class="hljs-variable">$pass</span>"</span> == <span class="hljs-string">'123456'</span> ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"Login successful"</span>
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"Login Failed"</span>
<span class="hljs-keyword">fi</span>
</code>

58、循环测试用户名与密码是否正确

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 循环测试用户名与密码是否正确 </span>
 
<span class="hljs-comment"># 循环测试用户的账户名和密码,最大测试 3 次,输入正确提示登录成功,否则提示登录失败</span>
<span class="hljs-comment"># 用户名为 tom 并且密码为 123456  </span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..3}
<span class="hljs-keyword">do</span>
  <span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入用户名:"</span> user
  <span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入密码:"</span>   pass
<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$user</span>"</span> == <span class="hljs-string">'tom'</span> -a <span class="hljs-string">"<span class="hljs-variable">$pass</span>"</span> == <span class="hljs-string">'123456'</span> ];<span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Login successful"</span>
     <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Login Failed"</span>
</code>

59、Shell 脚本的 fork 炸弹

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># Shell 脚本的 fork 炸弹 </span>
 
<span class="hljs-comment"># 快速消耗计算机资源,致使计算机死机</span>
<span class="hljs-comment"># 定义函数名为.(点), 函数中递归调用自己并放入后台执行</span>
.() { .|.&amp; };.
</code>

60、批量下载有序文件(pdf、图片、视频等等)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 批量下载有序文件(pdf、图片、视频等等)</span>
 
<span class="hljs-comment"># 本脚本准备有序的网络资料进行批量下载操作(如 01.jpg,02.jpg,03.jpg)</span>
<span class="hljs-comment"># 设置资源来源的域名连接</span>
url=<span class="hljs-string">"http://www.baidu.com/"</span>
<span class="hljs-built_in">echo</span>  <span class="hljs-string">"开始下载..."</span>
sleep 2
<span class="hljs-built_in">type</span>=jpg
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `seq 100`
     <span class="hljs-built_in">echo</span> <span class="hljs-string">"正在下载<span class="hljs-variable">$i</span>.<span class="hljs-variable">$type</span>"</span>
  curl <span class="hljs-variable">$url</span>/<span class="hljs-variable">$i</span>.<span class="hljs-variable">$type</span> -o /tmp/<span class="hljs-variable">${i}</span><span class="hljs-variable">$type</span>
     sleep 1
<span class="hljs-keyword">done</span>
<span class="hljs-comment">#curl 使用-o 选项指定下载文件另存到哪里.</span>
</code>

61、显示当前计算机中所有账户的用户名称

<code> <span class="hljs-comment">#!/bin/bash</span>
 
<span class="hljs-comment"># 显示当前计算机中所有账户的用户名称</span>
 
<span class="hljs-comment"># 下面使用3种不同的方式列出计算机中所有账户的用户名</span>
<span class="hljs-comment"># 指定以:为分隔符,打印/etc/passwd 文件的第 1 列</span>
<span class="hljs-attribute">awk</span> -F: <span class="hljs-string">'{print <span class="hljs-variable">$1</span>}'</span> /etc/passwd
 
<span class="hljs-comment"># 指定以:为分隔符,打印/etc/passwd 文件的第 1 列</span>
cut -d: -f1 /etc/passwd
 
<span class="hljs-comment"># 使用 sed 的替换功能,将/etc/passwd 文件中:后面的所有内容替换为空(仅显示用户名)</span>
sed <span class="hljs-string">'s/:.*//'</span> /etc/passwd
</code>

62、制定目录路径,脚本自动将该目录使用 tar 命令打包备份到/data目录

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 制定目录路径,脚本自动将该目录使用 tar 命令打包备份到/data目录 </span>
 
[ ! -d /data ] &amp;&amp; mkdir /data
[ -z <span class="hljs-variable">$1</span> ] &amp;&amp; <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">if</span> [ -d <span class="hljs-variable">$1</span> ];<span class="hljs-keyword">then</span>
  tar -czf /data/<span class="hljs-variable">$1</span>.-`date +%Y%m%d`.tar.gz <span class="hljs-variable">$1</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"该目录不存在"</span>
<span class="hljs-keyword">fi</span>
</code>

63、显示进度条(回旋镖版)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 显示进度条(回旋镖版)</span>
 
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
  clear
  <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..20}
  <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> ‐e <span class="hljs-string">"\033[3;<span class="hljs-variable">${i}</span>H*"</span>
    sleep 0.1
    <span class="hljs-keyword">done</span>
    clear
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {20..1}
    <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> ‐e <span class="hljs-string">"\033[3;<span class="hljs-variable">${i}</span>H*"</span>
    sleep 0.1
    <span class="hljs-keyword">done</span>
    clear
<span class="hljs-keyword">done</span>
</code>

64、安装 LAMP 环境(yum 版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 安装 LAMP 环境(yum 版本) </span>
 
<span class="hljs-comment"># 本脚本适用于 RHEL7(RHEL6 中数据库为 mysql)</span>
yum makecache &amp;&gt;/dev/null
num=$(yum repolist | awk <span class="hljs-string">'/repolist/{print $2}'</span> | sed <span class="hljs-string">'s/,//'</span>)
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$num</span> -lt 0 ];<span class="hljs-keyword">then</span>
  yum -y install httpd
  yum -y install mariadb mariadb-server mariadb-devel
  yum -y install php php-mysql
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"未配置 yum 源..."</span>
<span class="hljs-keyword">fi</span>
</code>

65、循环关闭局域网中所有主机

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 循环关闭局域网中所有主机 </span>
 
<span class="hljs-comment"># 假设本机为 192.168.4.100,编写脚本关闭除自己外的其他所有主机</span>
<span class="hljs-comment"># 脚本执行,需要提前给所有其他主机传递 ssh 密钥,满足无密码连接</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..254}
<span class="hljs-keyword">do</span>
  [ <span class="hljs-variable">$i</span> -eq 100 ] &amp;&amp; <span class="hljs-built_in">continue</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"正在关闭 192.168.4.<span class="hljs-variable">$i</span>..."</span>
  ssh 192.168.4.<span class="hljs-variable">$i</span> poweroff
<span class="hljs-keyword">done</span>
</code>

66、获取本机 MAC 地址

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 获取本机 MAC 地址</span>
ip a s | awk <span class="hljs-string">'BEGIN{print  " 本 机 MAC 地 址 信 息 如 下 :"}/^[0‐9]/{print $2;getline;if($0~/link\/ether/){print $2}}'</span> | grep -v lo:
 
<span class="hljs-comment"># awk 读取 ip 命令的输出,输出结果中如果有以数字开始的行,先显示该行的地 2 列(网卡名称),</span>
<span class="hljs-comment"># 接着使用 getline 再读取它的下一行数据,判断是否包含 link/ether</span>
<span class="hljs-comment"># 如果保护该关键词,就显示该行的第 2 列(MAC 地址)</span>
<span class="hljs-comment"># lo 回环设备没有 MAC,因此将其屏蔽,不显示</span>
</code>

67、自动配置 rsynd 服务器的配置文件 rsyncd.conf

<code><span class="hljs-comment">#!/bin/bash</span>
 
<span class="hljs-comment"># 自动配置 rsynd 服务器的配置文件 rsyncd.conf</span>
 
<span class="hljs-comment"># See rsyncd.conf man page for more options.</span>
 
[ ! -d /home/ftp ] &amp;&amp; mkdir /home/ftp
echo 'uid = nobody
gid = nobody
<span class="hljs-keyword">use</span> chroot = yes
<span class="hljs-keyword">max</span> connections = <span class="hljs-number">4</span>
pid <span class="hljs-keyword">file</span> = /<span class="hljs-keyword">var</span>/run/rsyncd.pid
<span class="hljs-keyword">exclude</span> = lost+<span class="hljs-keyword">found</span>/
transfer <span class="hljs-keyword">logging</span> = yes
<span class="hljs-keyword">timeout</span> = <span class="hljs-number">900</span>
<span class="hljs-keyword">ignore</span> nonreadable = yes
dont <span class="hljs-keyword">compress</span>   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[<span class="hljs-keyword">ftp</span>]
    <span class="hljs-keyword">path</span> = /home/<span class="hljs-keyword">ftp</span>
    <span class="hljs-keyword">comment</span> = <span class="hljs-keyword">share</span><span class="hljs-string">' &gt; /etc/rsyncd.conf
</span></code>

68、修改 Linux 系统的最大打开文件数量

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 修改 Linux 系统的最大打开文件数量 </span>
 
<span class="hljs-comment"># 往/etc/security/limits.conf 文件的末尾追加两行配置参数,修改最大打开文件数量为 65536</span>
cat &gt;&gt; /etc/security/limits.conf &lt;&lt;EOF
* soft nofile  65536
* hard nofile  65536
EOF
</code>

69、设置 Python 支持自动命令补齐功能

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 设置 Python 支持自动命令补齐功能 </span>
 
<span class="hljs-comment"># Summary:Enable tab complete for python</span>
<span class="hljs-comment"># Description:</span>
 
Needs import readline and rlcompleter module
<span class="hljs-comment">#</span>
import readline
<span class="hljs-comment">#</span>
import rlcompleter
<span class="hljs-comment">#</span>
<span class="hljs-built_in">help</span>(rlcompleter) display detail: readline.parse_and_bind(<span class="hljs-string">'tab: complete'</span>)
<span class="hljs-comment">#</span>
man python display detail: PYTHONSTARTUP variable
 
<span class="hljs-keyword">if</span>  [ ! -f /usr/bin/tab.py ];<span class="hljs-keyword">then</span>
  cat &gt;&gt; /usr/bin/tab.py &lt;&lt;EOF
import readline
import rlcompleter
readline.parse_and_bind(<span class="hljs-string">'tab: complete'</span>)
EOF
<span class="hljs-keyword">fi</span>
sed  -i <span class="hljs-string">'$a export PYTHONSTARTUP=/usr/bin/tab.py'</span> /etc/profile
<span class="hljs-built_in">source</span> /etc/profile
</code>

70、自动修改计划任务配置文件

Docker+K8s+Jenkins 主流技术全解视频资料【干货免费分享】

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 自动修改计划任务配置文件 </span>
 
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入分钟信息(00‐59):"</span> min
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入小时信息(00‐24):"</span> hour
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入日期信息(01‐31):"</span> date
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入月份信息(01‐12):"</span> month
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入星期信息(00‐06):"</span> weak
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入计划任务需要执行的命令或脚本:"</span> program
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$min</span> <span class="hljs-variable">$hour</span> <span class="hljs-variable">$date</span> <span class="hljs-variable">$month</span> <span class="hljs-variable">$weak</span> <span class="hljs-variable">$program</span>"</span> &gt;&gt; /etc/crontab
</code>

71、使用脚本循环创建三位数字的文本文件(111-999 的文件)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用脚本循环创建三位数字的文本文件(111-999 的文件) </span>
 
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..9}
<span class="hljs-keyword">do</span>
  <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> {1..9}
  <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> {1..9}
    <span class="hljs-keyword">do</span>
      touch /tmp/<span class="hljs-variable">$i</span><span class="hljs-variable">$j</span><span class="hljs-variable">$k</span>.txt
    <span class="hljs-keyword">done</span>
    <span class="hljs-keyword">done</span>
<span class="hljs-keyword">done</span>
</code>

72、找出/etc/passwd 中能登录的用户,并将对应在/etc/shadow 中第二列密码提出处理

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 找出/etc/passwd 中能登录的用户,并将对应在/etc/shadow 中第二列密码提出处理</span>
 
user=$(awk -F: <span class="hljs-string">'/bash$/{print $1}'</span> /etc/passwd)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-variable">$user</span>
<span class="hljs-keyword">do</span>
  awk -F: -v x=<span class="hljs-variable">$i</span> <span class="hljs-string">'$1==x{print $1,$2}'</span> /etc/shadow
<span class="hljs-keyword">done</span>
</code>

73、统计/etc/passwd 中 root 出现的次数

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 统计/etc/passwd 中 root 出现的次数 </span>
 
<span class="hljs-comment">#每读取一行文件内容,即从第 1 列循环到最后 1 列,依次判断是否包含 root 关键词,如果包含则 x++</span>
awk -F: <span class="hljs-string">'{i=1;while(i&lt;=NF){if($i~/root/){x++};i++}} END{print "root 出现次数为"x}'</span> /etc/passwd
</code>

74、统计 Linux 进程相关数量信息

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 统计 Linux 进程相关数量信息 </span>
 
running=0
sleeping=0
stoped=0
zombie=0
<span class="hljs-comment"># 在 proc 目录下所有以数字开始的都是当前计算机正在运行的进程的进程 PID</span>
<span class="hljs-comment"># 每个 PID 编号的目录下记录有该进程相关的信息</span>
<span class="hljs-keyword">for</span> pid <span class="hljs-keyword">in</span> /proc/[1‐9]*
<span class="hljs-keyword">do</span>
  procs=$[procs+1]
  <span class="hljs-built_in">stat</span>=$(awk <span class="hljs-string">'{print $3}'</span> <span class="hljs-variable">$pid</span>/<span class="hljs-built_in">stat</span>)
<span class="hljs-comment"># 每个 pid 目录下都有一个 stat 文件,该文件的第 3 列是该进程的状态信息</span>
    <span class="hljs-keyword">case</span> <span class="hljs-variable">$stat</span> <span class="hljs-keyword">in</span>
    R)
    running=$[running+1]
    ;;
    T)
    stoped=$[stoped+1]
    ;;
    S)
    sleeping=$[sleeping+1]
    ;;
    Z)
     zombie=$[zombie+1]
     ;;
    <span class="hljs-keyword">esac</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"进程统计信息如下"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"总进程数量为:<span class="hljs-variable">$procs</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Running 进程数为:<span class="hljs-variable">$running</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Stoped 进程数为:<span class="hljs-variable">$stoped</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Sleeping 进程数为:<span class="hljs-variable">$sleeping</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Zombie 进程数为:<span class="hljs-variable">$zombie</span>"</span>
</code>

75、从键盘读取一个论坛积分,判断论坛用户等级

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 从键盘读取一个论坛积分,判断论坛用户等级</span>
 
<span class="hljs-comment">#等级分类如下:</span>
<span class="hljs-comment">#  大于等于 90        神功绝世</span>
<span class="hljs-comment">#  大于等于 80,小于 90       登峰造极</span>
<span class="hljs-comment">#  大于等于 70,小于 80       炉火纯青</span>
<span class="hljs-comment">#  大于等于 60,小于 70       略有小成</span>
<span class="hljs-comment">#  小于 60               初学乍练</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入积分(0‐100):"</span> JF
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$JF</span> -ge 90 ] ; <span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$JF</span> 分,神功绝世"</span>
<span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$JF</span> -ge 80 ] ; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$JF</span> 分,登峰造极"</span>
<span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$JF</span> -ge 70 ] ; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$JF</span> 分,炉火纯青"</span>
<span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$JF</span> -lt 60 ] ; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$JF</span> 分,略有小成"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$JF</span> 分,初学乍练"</span>
<span class="hljs-keyword">fi</span>
</code>

76、判断用户输入的数据类型(字母、数字或其他)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 判断用户输入的数据类型(字母、数字或其他) </span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入一个字符:"</span> KEY
<span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$KEY</span>"</span> <span class="hljs-keyword">in</span>
  [a‐z]|[A‐Z])
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"字母"</span> 
    ;;
  [0‐9])
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"数字"</span> 
    ;;
  *)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"空格、功能键或其他控制字符"</span>
<span class="hljs-keyword">esac</span>
</code>

77、显示进度条(数字版)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 显示进度条(数字版) </span>
<span class="hljs-comment"># echo 使用‐e 选项后,在打印参数中可以指定 H,设置需要打印内容的 x,y 轴的定位坐标</span>
<span class="hljs-comment"># 设置需要打印内容在第几行,第几列</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..100}
<span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[6;8H["</span>
    <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[6;9H<span class="hljs-variable">$i</span>%"</span>
    <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[6;13H]"</span>
    sleep 0.1
<span class="hljs-keyword">done</span>
</code>

78、打印斐波那契数列

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 打印斐波那契数列(该数列的特点是后一个数字,永远都是前 2 个数字之和) </span>
 
<span class="hljs-comment"># 斐波那契数列后一个数字永远是前 2 个数字之和</span>
<span class="hljs-comment"># 如:0  1  1  2  3  5  8  13 ... ...</span>
list=(0 1)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `seq 2 11`
<span class="hljs-keyword">do</span>
  list[<span class="hljs-variable">$i</span>]=`expr <span class="hljs-variable">${list[‐1]}</span> + <span class="hljs-variable">${list[‐2]}</span>`
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">${list[@]}</span>
</code>

79、判断用户输入的是 Yes 或 NO

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 判断用户输入的是 Yes 或 NO </span>
 
<span class="hljs-built_in">read</span> -p  <span class="hljs-string">"Are you sure?[y/n]:"</span>  sure
<span class="hljs-keyword">case</span>  <span class="hljs-variable">$sure</span>  <span class="hljs-keyword">in</span>
  y|Y|Yes|YES)  
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"you enter <span class="hljs-variable">$a</span>"</span>
    ;;
    n|N|NO|no)
     <span class="hljs-built_in">echo</span> <span class="hljs-string">"you enter <span class="hljs-variable">$a</span>"</span>
     ;;
    *)
     <span class="hljs-built_in">echo</span> <span class="hljs-string">"error"</span>;;
<span class="hljs-keyword">esac</span>
</code>

80、显示本机 Linux 系统上所有开放的端口列表

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 显示本机 Linux 系统上所有开放的端口列表 </span>
 
<span class="hljs-comment"># 从端口列表中观测有没有没用的端口,有的话可以将该端口对应的服务关闭,防止意外的攻击可能性</span>
ss -nutlp | awk <span class="hljs-string">'{print $1,$5}'</span> | awk -F<span class="hljs-string">"[: ]"</span> <span class="hljs-string">'{print "协议:"$1,"端口号:"$NF}'</span> | grep <span class="hljs-string">"[0‐9]"</span> | uniq
</code>

81、将 Linux 系统中 UID 大于等于 1000 的普通用户都删除

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 将 Linux 系统中 UID 大于等于 1000 的普通用户都删除 </span>
 
<span class="hljs-comment"># 先用 awk 提取所有 uid 大于等于 1000 的普通用户名称</span>
<span class="hljs-comment"># 再使用 for 循环逐个将每个用户删除即可</span>
user=$(awk -F: <span class="hljs-string">'$3&gt;=1000{print $1}'</span> /etc/passwd)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-variable">$user</span>
<span class="hljs-keyword">do</span>
     userdel -r <span class="hljs-variable">$i</span>
<span class="hljs-keyword">done</span>
</code>

82、使用脚本开启关闭虚拟机

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用脚本开启关闭虚拟机 </span>
 
<span class="hljs-comment"># 脚本通过调用virsh命令实现对虚拟机的管理,如果没有该命令,需要安装 libvirt‐client 软件包</span>
<span class="hljs-comment"># $1是脚本的第1个参数,$2是脚本的第2个参数</span>
<span class="hljs-comment"># 第1个参数是你希望对虚拟机进行的操作指令,第2个参数是虚拟机名称</span>
<span class="hljs-keyword">case</span> <span class="hljs-variable">$1</span> <span class="hljs-keyword">in</span>
  list)
    virsh list --all
    ;;
  start)
    virsh start <span class="hljs-variable">$2</span>
    ;;
  stop)
    virsh destroy <span class="hljs-variable">$2</span>
    ;;
  <span class="hljs-built_in">enable</span>)
    virsh autostart <span class="hljs-variable">$2</span>
    ;;
  <span class="hljs-built_in">disable</span>)
    virsh autostart --<span class="hljs-built_in">disable</span> <span class="hljs-variable">$2</span>
    ;;
  *)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Usage:<span class="hljs-variable">$0</span> list"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Usage:<span class="hljs-variable">$0</span> [start|stop|enable|disable]  VM_name"</span>
    cat &lt;&lt; EOF
    <span class="hljs-comment">#list      显示虚拟机列表</span>
    <span class="hljs-comment">#start     启动虚拟机</span>
    <span class="hljs-comment">#stop      关闭虚拟机</span>
    <span class="hljs-comment">#enable    设置虚拟机为开机自启</span>
    <span class="hljs-comment">#disable   关闭虚拟机开机自启功能</span>
    EOF
    ;;
<span class="hljs-keyword">esac</span>
</code>

83、调整虚拟机内存参数的 shell 脚本

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 调整虚拟机内存参数的 shell 脚本 </span>
 
<span class="hljs-comment"># 脚本通过调用 virsh 命令实现对虚拟机的管理,如果没有该命令,需要安装 libvirt‐client 软件包</span>
cat &lt;&lt; EOF
1.调整虚拟机最大内存数值
2.调整实际分配给虚拟机的内存数值
EOF
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请选择[1‐2]:"</span> select
<span class="hljs-keyword">case</span> <span class="hljs-variable">$select</span> <span class="hljs-keyword">in</span>
  1)
      <span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入虚拟机名称"</span> name
      <span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入最大内存数值(单位:k):"</span> size
      virsh setmaxmem <span class="hljs-variable">$name</span> --size <span class="hljs-variable">$size</span> --config
      ;;
  2)
      <span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入虚拟机名称"</span> name
      <span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入实际分配内存数值(单位:k):"</span> size
      virsh setmem <span class="hljs-variable">$name</span> <span class="hljs-variable">$size</span>
      ;;
  *)
      <span class="hljs-built_in">echo</span> <span class="hljs-string">"Error"</span>
      ;;
<span class="hljs-keyword">esac</span>
</code>

84、查看 KVM 虚拟机中的网卡信息(不需要进入启动或进入虚拟机)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 查看 KVM 虚拟机中的网卡信息(不需要进入启动或进入虚拟机) </span>
 
<span class="hljs-comment"># 该脚本使用 guestmount 工具,可以将虚拟机的磁盘系统挂载到真实机文件系统中</span>
<span class="hljs-comment"># Centos7.2 中安装 libguestfs‐tools‐c 可以获得 guestmount 工具</span>
<span class="hljs-comment"># 虚拟机可以启动或者不启动都不影响该脚本的使用</span>
<span class="hljs-comment"># 将虚拟机磁盘文件挂载到文件系统后,就可以直接读取磁盘文件中的网卡配置文件中的数据</span>
clear
mountpoint=<span class="hljs-string">"/media/virtimage"</span>
[ ! -d <span class="hljs-variable">$mountpoint</span> ] &amp;&amp; mkdir <span class="hljs-variable">$mountpoint</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"输入虚拟机名称:"</span> name
<span class="hljs-built_in">echo</span> <span class="hljs-string">"请稍后..."</span>
<span class="hljs-comment"># 如果有设备挂载到该挂载点,则先 umount 卸载</span>
<span class="hljs-keyword">if</span> mount | grep -q <span class="hljs-string">"<span class="hljs-variable">$mountpoint</span>"</span> ;<span class="hljs-keyword">then</span>
  umount <span class="hljs-variable">$mountpoint</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment"># 只读的方式,将虚拟机的磁盘文件挂载到特定的目录下,这里是/media/virtimage 目录</span>
guestmount -r -d <span class="hljs-variable">$name</span> -i <span class="hljs-variable">$mountpoint</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[32m<span class="hljs-variable">$name</span> 虚拟机中网卡列表如下:\033[0m"</span>
dev=$(ls /media/virtimage/etc/sysconfig/network‐scripts/ifcfg-* |awk -F<span class="hljs-string">"[/‐]"</span> <span class="hljs-string">'{print $9}'</span>)
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$dev</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐"</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"+++++++++++++++++++++++++++++++++++++++++++"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[32m 网卡 IP 地址信息如下:\033[0m"</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-variable">$dev</span>
<span class="hljs-keyword">do</span>
  <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"<span class="hljs-variable">$i</span>:"</span>
  grep -q <span class="hljs-string">"IPADDR"</span> /media/virtimage/etc/sysconfig/network‐scripts/ifcfg-<span class="hljs-variable">$i</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"未配置 IP地址"</span>
  awk -F= <span class="hljs-string">'/IPADDR/{print $2}'</span> /media/virtimage/etc/sysconfig/network-scripts/ifcfg-<span class="hljs-variable">$i</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"+++++++++++++++++++++++++++++++++++++++++++"</span>
</code>

85、不登陆虚拟机,修改虚拟机网卡 IP 地址

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 不登陆虚拟机,修改虚拟机网卡 IP 地址 </span>
 
<span class="hljs-comment"># 该脚本使用 guestmount 工具,Centos7.2 中安装 libguestfs‐tools‐c 可以获得 guestmount 工具</span>
<span class="hljs-comment"># 脚本在不登陆虚拟机的情况下,修改虚拟机的 IP 地址信息</span>
<span class="hljs-comment"># 在某些环境下,虚拟机没有 IP 或 IP 地址与真实主机不在一个网段</span>
<span class="hljs-comment"># 真实主机在没有 virt‐manger 图形的情况下,远程连接虚拟机很麻烦</span>
<span class="hljs-comment"># 该脚本可以解决类似的问题</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入虚拟机名称:"</span> name
<span class="hljs-keyword">if</span> virsh domstate <span class="hljs-variable">$name</span> | grep -q running ;<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"修改虚拟机网卡数据,需要关闭虚拟机"</span>
  virsh destroy <span class="hljs-variable">$name</span>
<span class="hljs-keyword">fi</span>
mountpoint=<span class="hljs-string">"/media/virtimage"</span>
[ ! -d <span class="hljs-variable">$mountpoint</span> ] &amp;&amp; mkdir <span class="hljs-variable">$mountpoint</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"请稍后..."</span>
<span class="hljs-keyword">if</span> mount | grep -q <span class="hljs-string">"<span class="hljs-variable">$mountpoint</span>"</span> ;<span class="hljs-keyword">then</span>
  umount <span class="hljs-variable">$mountpoint</span>
<span class="hljs-keyword">fi</span>
guestmount  -d <span class="hljs-variable">$name</span> -i <span class="hljs-variable">$mountpoint</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入需要修改的网卡名称:"</span> dev
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入 IP 地址:"</span> addr
<span class="hljs-comment"># 判断原本网卡配置文件中是否有 IP 地址,有就修改该 IP,没有就添加一个新的 IP 地址</span>
<span class="hljs-keyword">if</span> grep -q <span class="hljs-string">"IPADDR"</span>  <span class="hljs-variable">$mountpoint</span>/etc/sysconfig/network‐scripts/ifcfg‐<span class="hljs-variable">$dev</span> ;<span class="hljs-keyword">then</span>
  sed -i <span class="hljs-string">"/IPADDR/s/=.*/=<span class="hljs-variable">$addr</span>/"</span>  <span class="hljs-variable">$mountpoint</span>/etc/sysconfig/network‐scripts/ifcfg‐<span class="hljs-variable">$dev</span>
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"IPADDR=<span class="hljs-variable">$addr</span>"</span> &gt;&gt; <span class="hljs-variable">$mountpoint</span>/etc/sysconfig/network‐scripts/ifcfg‐<span class="hljs-variable">$dev</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-comment"># 如果网卡配置文件中有客户配置的 IP 地址,则脚本提示修改 IP 完成</span>
awk -F= -v x=<span class="hljs-variable">$addr</span> <span class="hljs-string">'$2==x{print "完成..."}'</span>  <span class="hljs-variable">$mountpoint</span>/etc/sysconfig/network‐scripts/ifcfg-<span class="hljs-variable">$dev</span>
</code>

86、破解虚拟机密码,无密码登陆虚拟机系统

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 破解虚拟机密码,无密码登陆虚拟机系统 </span>
 
<span class="hljs-comment"># 该脚本使用 guestmount 工具,Centos7.2 中安装 libguestfs‐tools‐c 可以获得 guestmount 工具</span>
 
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入虚拟机名称:"</span> name
<span class="hljs-keyword">if</span> virsh domstate <span class="hljs-variable">$name</span> | grep -q running ;<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"破解,需要关闭虚拟机"</span>
  virsh destroy <span class="hljs-variable">$name</span>
<span class="hljs-keyword">fi</span>
mountpoint=<span class="hljs-string">"/media/virtimage"</span>
[ ! -d <span class="hljs-variable">$mountpoint</span> ] &amp;&amp; mkdir <span class="hljs-variable">$mountpoint</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"请稍后..."</span>
<span class="hljs-keyword">if</span> mount | grep -q <span class="hljs-string">"<span class="hljs-variable">$mountpoint</span>"</span> ;<span class="hljs-keyword">then</span>
  umount <span class="hljs-variable">$mountpoint</span>
<span class="hljs-keyword">fi</span>
guestmount -d <span class="hljs-variable">$name</span> -i <span class="hljs-variable">$mountpoint</span>
<span class="hljs-comment"># 将 passwd 中密码占位符号 x 删除,该账户即可实现无密码登陆系统</span>
sed -i <span class="hljs-string">"/^root/s/x//"</span> <span class="hljs-variable">$mountpoint</span>/etc/passwd
</code>

87、Shell 脚本对信号的处理,执行脚本后,按键盘 Ctrl+C 无法终止的脚本

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># Shell 脚本对信号的处理,执行脚本后,按键盘 Ctrl+C 无法终止的脚本 </span>
 
<span class="hljs-comment"># 使用 trap 命令可以拦截用户通过键盘或 kill 命令发送过来的信号</span>
<span class="hljs-comment"># 使用 kill ‐l 可以查看 Linux 系统中所有的信号列表,其中 2 代表 Ctrl+C</span>
<span class="hljs-comment"># trap 当发现有用户 ctrl+C 希望终端脚本时,就执行 echo "暂停 10s";sleep 10 这两条命令</span>
<span class="hljs-comment"># 另外用户使用命令:[ kill ‐2 脚本的 PID ] 也可以中断脚本和 Ctrl+C 一样的效果,都会被 trap 拦截</span>
<span class="hljs-built_in">trap</span> <span class="hljs-string">'echo "暂停 10s";sleep 10'</span> 2
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"go go go"</span>
<span class="hljs-keyword">done</span>
</code>

88、一键部署 memcached

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 一键部署 memcached </span>
 
<span class="hljs-comment"># 脚本用源码来安装 memcached 服务器</span>
<span class="hljs-comment"># 注意:如果软件的下载链接过期了,请更新 memcached 的下载链接</span>
wget http://www.memcached.org/files/memcached-1.5.1.tar.gz
yum -y install gcc
tar -xf  memcached‐1.5.1.tar.gz
<span class="hljs-built_in">cd</span> memcached‐1.5.1
./configure
make
make install
</code>

89、一键配置 VNC 远程桌面服务器(无密码版本)

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 一键配置 VNC 远程桌面服务器(无密码版本)</span>
 
<span class="hljs-comment"># 脚本配置的 VNC 服务器,客户端无需密码即可连接</span>
<span class="hljs-comment"># 客户端仅有查看远程桌面的权限,没有鼠标和键盘的操作权限</span>
 
rpm --quiet -q tigervnc‐server
<span class="hljs-keyword">if</span> [  $? -ne  0 ];<span class="hljs-keyword">then</span>
  yum  -y  tigervnc‐server
<span class="hljs-keyword">fi</span>
x0vncserver AcceptKeyEvents=0 AlwaysShared=1 \
AcceptPointerEvents=0 SecurityTypes=None  rfbport=5908
</code>

90、关闭 SELinux

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 关闭 SELinux </span>
 
sed -i  <span class="hljs-string">'/^SELINUX/s/=.*/=disabled/'</span> /etc/selinux/config
setenforce 0
</code>

91、查看所有虚拟机磁盘使用量以及CPU使用量信息

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 查看所有虚拟机磁盘使用量以及CPU使用量信息 </span>
 
virt‐df
<span class="hljs-built_in">read</span> -n1 <span class="hljs-string">"按任意键继续"</span> key
virt‐top
</code>

92、使用 shell 脚本打印图形

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用 shell 脚本打印如下图形: </span>
 
<span class="hljs-comment"># 打印第一组图片</span>
<span class="hljs-comment"># for(())为类 C 语言的语法格式,也可以使用 for i  in;do  ;done 的格式替换</span>
<span class="hljs-comment"># for((i=1;i&lt;=9;i++))循环会执行 9 次,i 从 1 开始到 9,每循环一次 i 自加 1</span>
clear
<span class="hljs-keyword">for</span> (( i=1; i&lt;=9; i++ ))
<span class="hljs-keyword">do</span>
  <span class="hljs-keyword">for</span> (( j=1; j&lt;=i; j++ ))
  <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"<span class="hljs-variable">$i</span>"</span>
  <span class="hljs-keyword">done</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">""</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">read</span>  -n1  <span class="hljs-string">"按任意键继续"</span>  key
<span class="hljs-comment">#打印第二组图片</span>
clear
<span class="hljs-keyword">for</span> (( i=1; i&lt;=5; i++ )) 
<span class="hljs-keyword">do</span>
  <span class="hljs-keyword">for</span> (( j=1; j&lt;=i; j++ ))
  <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">" |"</span>
  <span class="hljs-keyword">done</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"_ "</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">read</span>  -n1  <span class="hljs-string">"按任意键继续"</span>  key
<span class="hljs-comment">#打印第三组图片</span>
clear
<span class="hljs-keyword">for</span> (( i=1; i&lt;=5; i++ ))
<span class="hljs-keyword">do</span>
  <span class="hljs-keyword">for</span> (( j=1; j&lt;=i; j++ ))
  <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">" *"</span>
  <span class="hljs-keyword">done</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">""</span>
<span class="hljs-keyword">done</span>
<span class="hljs-keyword">for</span> (( i=5; i&gt;=1; i‐‐ ))
<span class="hljs-keyword">do</span>
  <span class="hljs-keyword">for</span> (( j=1; j&lt;=i; j++ ))
  <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">" *"</span>
  <span class="hljs-keyword">done</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">""</span>
<span class="hljs-keyword">done</span>
</code>

93、根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动 </span>
 
<span class="hljs-comment"># 00‐12 点为早晨,12‐18 点为下午,18‐24 点为晚上</span>
<span class="hljs-comment"># 使用 date 命令获取时间后,if 判断时间的区间,确定问候语内容</span>
tm=$(date +%H)
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$tm</span> -le 12 ];<span class="hljs-keyword">then</span>
  msg=<span class="hljs-string">"Good Morning <span class="hljs-variable">$USER</span>"</span>
<span class="hljs-keyword">elif</span> [ <span class="hljs-variable">$tm</span> -gt 12 -a <span class="hljs-variable">$tm</span> -le 18 ];<span class="hljs-keyword">then</span>
    msg=<span class="hljs-string">"Good Afternoon <span class="hljs-variable">$USER</span>"</span>
<span class="hljs-keyword">else</span>
    msg=<span class="hljs-string">"Good Night <span class="hljs-variable">$USER</span>"</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"当前时间是:<span class="hljs-variable">$(date +"%Y‐%m‐%d %H:%M:%S")</span>"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[34m<span class="hljs-variable">$msg</span>\033[0m"</span>
</code>

94、读取用户输入的账户名称,将账户名写入到数组保存

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 读取用户输入的账户名称,将账户名写入到数组保存 </span>
 
<span class="hljs-comment"># 定义数组名称为 name,数组的下标为 i,小标从 0 开始,每输入一个账户名,下标加 1,继续存下一个账户</span>
<span class="hljs-comment"># 最后,输入 over,脚本输出总结性信息后脚本退出</span>
i=0
<span class="hljs-keyword">while</span> :
<span class="hljs-keyword">do</span>
  <span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入账户名,输入 over 结束:"</span> key
  <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$key</span> == <span class="hljs-string">"over"</span> ];<span class="hljs-keyword">then</span> 
    <span class="hljs-built_in">break</span>
    <span class="hljs-keyword">else</span>
    name[<span class="hljs-variable">$i</span>]=<span class="hljs-variable">$key</span>
    <span class="hljs-built_in">let</span> i++
    <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"总账户名数量:<span class="hljs-variable">${#name[*]}</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">${name[@]}</span>"</span>
</code>

95、判断文件或目录是否存在

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 判断文件或目录是否存在 </span>
 
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$#</span> -eq 0 ] ;<span class="hljs-keyword">then</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"未输入任何参数,请输入参数"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"用法:<span class="hljs-variable">$0</span> [文件名|目录名]"</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-keyword">if</span> [ -f <span class="hljs-variable">$1</span> ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"该文件,存在"</span>
  ls -l <span class="hljs-variable">$1</span>
<span class="hljs-keyword">else</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有该文件"</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-keyword">if</span> [ -d  <span class="hljs-variable">$1</span> ];<span class="hljs-keyword">then</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">"该目录,存在"</span>
     ls -ld  <span class="hljs-variable">$2</span>
<span class="hljs-keyword">else</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">"没有该目录"</span>
<span class="hljs-keyword">fi</span>
</code>

96、打印各种格式的时间

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 打印各种时间格式 </span>
 
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示星期简称(如:Sun)"</span>
date +%a
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示星期全称(如:Sunday)"</span>
date +%A
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示月份简称(如:Jan)"</span>
date +%b
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示月份全称(如:January)"</span>
date +%B
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示数字月份(如:12)"</span>
date +%m
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示数字日期(如:01 号)"</span>
date +%d
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示数字年(如:01 号)"</span>
date +%Y <span class="hljs-built_in">echo</span> <span class="hljs-string">"显示年‐月‐日"</span>
date +%F
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示小时(24 小时制)"</span>
date +%H
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示分钟(00..59)"</span>
date +%M
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示秒"</span>
date +%S
<span class="hljs-built_in">echo</span> <span class="hljs-string">"显示纳秒"</span>
date +%N
<span class="hljs-built_in">echo</span> <span class="hljs-string">"组合显示"</span>
date +<span class="hljs-string">"%Y%m%d %H:%M:%S"</span>
</code>

97、使用 egrep 过滤 MAC 地址

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用 egrep 过滤 MAC 地址 </span>
 
<span class="hljs-comment"># MAC 地址由 16 进制组成,如 AA:BB:CC:DD:EE:FF</span>
<span class="hljs-comment"># [0‐9a‐fA‐F]{2}表示一段十六进制数值,{5}表示连续出现5组前置:的十六进制</span>
egrep <span class="hljs-string">"[0‐9a‐fA‐F]{2}(:[0‐9a‐fA‐F]{2}){5}"</span> <span class="hljs-variable">$1</span>
</code>

98、统计双色球各个数字的中奖概率

<code><span class="hljs-comment">#!/bin/bash</span>
 
<span class="hljs-comment"># 统计双色球各个数字的中奖概率 </span>
 
<span class="hljs-comment"># 往期双色球中奖号码如下:</span>
<span class="hljs-comment"># 01 04 11 28 31 32  16</span>
<span class="hljs-comment"># 04 07 08 18 23 24  02</span>
<span class="hljs-comment"># 02 05 06 16 28 29  04</span>
<span class="hljs-comment"># 04 19 22 27 30 33  01</span>
<span class="hljs-comment"># 05 10 18 19 30 31  03</span>
<span class="hljs-comment"># 02 06 11 12 19 29  06</span>
<span class="hljs-comment"># 统计篮球和红球数据出现的概率次数(篮球不分顺序,统计所有篮球混合在一起的概率)</span>
<span class="hljs-attribute">awk</span> '{print <span class="hljs-number">$1</span><span class="hljs-string">"\n"</span><span class="hljs-number">$2</span><span class="hljs-string">"\n"</span><span class="hljs-number">$3</span><span class="hljs-string">"\n"</span><span class="hljs-number">$4</span><span class="hljs-string">"\n"</span><span class="hljs-number">$5</span><span class="hljs-string">"\n"</span><span class="hljs-number">$6</span>}' 1.txt | sort | uniq -c | sort
<span class="hljs-attribute">awk</span> '{print <span class="hljs-number">$7</span>}' 1.txt | sort | uniq -c | sort
</code>

99、生成签名私钥和证书

<code><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 生成签名私钥和证书 </span>
 
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入存放证书的目录:"</span> dir
<span class="hljs-keyword">if</span> [ ! -d <span class="hljs-variable">$dir</span> ];<span class="hljs-keyword">then</span>
  <span class="hljs-built_in">echo</span> <span class="hljs-string">"该目录不存在"</span>
  <span class="hljs-built_in">exit</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-built_in">read</span> -p <span class="hljs-string">"请输入密钥名称:"</span> name
<span class="hljs-comment"># 使用 openssl 生成私钥</span>
openssl genrsa -out <span class="hljs-variable">${dir}</span>/<span class="hljs-variable">${name}</span>.key
<span class="hljs-comment"># 使用 openssl 生成证书 #subj 选项可以在生成证书时,非交互自动填写 Common Name 信息</span>
openssl req -new -x509 -key <span class="hljs-variable">${dir}</span>/<span class="hljs-variable">${name}</span>.key -subj <span class="hljs-string">"/CN=common"</span> -out <span class="hljs-variable">${dir}</span>/<span class="hljs-variable">${name}</span>.crt
</code>

100、使用awk编写的wc程序

<code>

<span class="bash"><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-comment"># 使用awk编写的wc程序 </span>
 
<span class="hljs-comment"># 自定义变量 chars 变量存储字符个数,自定义变量 words 变量存储单词个数</span>
<span class="hljs-comment"># awk 内置变量 NR 存储行数</span>
<span class="hljs-comment"># length()为 awk 内置函数,用来统计每行的字符数量,因为每行都会有一个隐藏的$,所以每次统计后都+1</span>
<span class="hljs-comment"># wc 程序会把文件结尾符$也统计在内,可以使用 cat ‐A 文件名,查看该隐藏字符</span>
awk <span class="hljs-string">'{chars+=length($0)+1;words+=NF} END{print NR,words,chars}'</span> <span class="hljs-variable">$1</span></span></code>

文章对你有帮助,请点赞转发哦~

发表评论

好哇网,每天提供1000+次下载,做你身边靠谱的下载站!

立刻探索下载

留言咨询