shell脚本——七个小实验

100 阅读5分钟

实验一

实验要求:检查用户家目录中的 test.sh 文件是否存在,并且检查是否有执行权限。

方法一:

[root@yuji ~]# [ -e ~/test.sh ] && echo "存在" || echo "不存在"
存在
[root@yuji ~]#  [ -x ~/test.sh ] && echo "有执行权限" || echo "没有执行权限"
有执行权限

image.png 方法二:

创建脚本

 [root@localhost ~]# vim cunzai.sh
 #!/bin/bash
 #author:yu
 #date:2022/10/27
 ​
 [ -e ~/test.sh ]
 ​
 if [ $? -eq 0 ];then
    echo "test.sh存在"
    [ -x ~/test.sh ]
    if [ $? -eq 0 ]
    then
        echo "有执行权限"
    else
        echo "没有执行权限"
     fi
 else
    echo "test.sh不存在"
 fi
 ​
 [root@localhost ~]# bash cunzai.sh
 test.sh不存在
 [root@localhost ~]# touch test.sh
 [root@localhost ~]# bash cunzai.sh
 test.sh存在
 没有执行权限
 [root@localhost ~]# chmod +x test.sh
 [root@localhost ~]# bash cunzai.sh
 test.sh存在
 有执行权限

image.png

image.png

实验二

实验要求:提示用户输入100米赛跑的秒数,要求判断秒数大于0且小于等于10秒的进入决赛,大于10秒的都淘汰,如果输入其它字符则提示重新输入;进入决赛的成员再进一步判断男女性别,男生进男生组,女生进女生组,如果输入错误请提示错误。

创建脚本:

 [root@yuji ~]# vim paobu.sh
 #!/bin/bash
 #提示用户输入100米赛跑的秒数,要求判断秒数大于0且小于等于10秒的进入选拔赛,大于10秒的都淘汰,如果输入其它字符则提示重新输入;进入选拔赛的成员再进一步判断男女性别,男生进男生组,女生进女生组,如果输入错误请提示错误。
 ​
 read -p "请输入您的100米赛跑秒数:" time
 ​
 if [ $time -gt 0 ] && [ $time -le 10 ]
 then
     echo "恭喜您进入决赛"
     read -p "请输入您的性别(男、女):" gender
     if [ $gender = "男" ]
     then
         echo "您进入男生组"
     elif [ $gender = "女" ]
     then
         echo "您进入女生组"
     else
         echo "输入有误,请重新输入"
     fi
 elif [ $time -gt 10 ]
 then
     echo "很遗憾,您无缘决赛"
 else
     echo "输入有误,请重新输入"
     bash $0
 fi

image.png 执行脚本:

[root@yuji ~]# bash paobu.sh
请输入您的100米赛跑秒数:8
恭喜您进入决赛
请输入您的性别(男、女):女      
您进入女生组
[root@yuji ~]# bash paobu.sh
请输入您的100米赛跑秒数:0
输入有误,请重新输入
请输入您的100米赛跑秒数:9
恭喜您进入决赛
请输入您的性别(男、女):男
您进入男生组
复制代码

微信图片_20220410171959.png

实验三

实验要求:用case语句解压根据后缀名为 .tar.gz 或 .tar.bz2 的压缩包到 /opt 目录

创建脚本

 [root@localhost ~]# vim jieya.sh
 #!/bin/bash
 #判断压缩包使用的是gz还是bz2,之后使用对应的程序解压缩到/opt目录下。
 ​
 case $1 in
 *.tar.gz)
   echo "解压gz格式的压缩包"
   tar -zxvf $1 -C /opt/
   ;;
 *.tar.bz2)
   echo "解压bz2格式的压缩包"
   tar -jxvf $1 -C /opt/
   ;;
 *)
   echo "文件格式有误,无法解压"
 esac
复制代码

微信图片_20220410172449.png

执行脚本

 [root@localhost ~]# bash jieya.sh  f1.tar.gz
 解压gz格式的压缩包
 f1.txt
 [root@localhost ~]# bash jieya.sh  f2.tar.bz2
 解压bz2格式的压缩包
 f2.txt
 [root@localhost ~]# bash jieya.sh  fa.txt
 文件格式有误,无法解压
复制代码

实验3解压2.png

实验四

实验要求:提示用户输入内容,使用if 语句判断输入的内容是否为整数。

创建脚本:

 [root@localhost ~]# vim zhengshu.sh
 #!/bin/bash
 #判断用户输入的数是否为整数
 ​
 read -p "请输入一个数字:" a
 let i=$a+1 &> /dev/null
 ​
 if [ $? -eq 0 ]
 then
      echo "${a}是整数"
 else
      echo "${a}不是整数"
 fi
复制代码

实验4 整数1 (2).png

执行脚本:

 [root@localhost ~]# bash zhengshu.sh
 请输入一个数字:0
 0是整数
 [root@localhost ~]# bash zhengshu.sh
 请输入一个数字:12
 12是整数
 [root@localhost ~]# bash zhengshu.sh
 请输入一个数字:3.4
 3.4不是整数
复制代码

实验4 整数2.png

实验五

实验要求:根据上一题,在整数的情况下再判断输入的内容是奇数还是偶数。

创建脚本:

 [root@localhost ~]# vim jishu.sh
 #!/bin/bash
 #判断用户输入的数是否为整数
 #如果是整数,则继续判断是奇数还是偶数。
 ​
 read -p "请输入一个数字:" a
 let i=$a+1 &> /dev/null
 if [ $? -eq 0 ]
 then
      echo "$a是整数"
      b=$[${a}%2]
      case $b in
      0)
         echo "$a是偶数"
         ;;
      *)
         echo "$a是奇数"
      esac
 else
      echo "$a不是整数"
 fi
复制代码

实验5 奇数偶数1.png

执行脚本:

 [root@localhost ~]# bash jishu.sh
 请输入一个数字:0
 0是整数
 0是偶数
 [root@localhost ~]# bash jishu.sh
 请输入一个数字:7
 7是整数
 7是奇数
 [root@localhost ~]# bash jishu.sh
 请输入一个数字:102
 102是整数
 102是偶数
 [root@localhost ~]# bash jishu.sh
 请输入一个数字:5.66
 5.66不是整数
复制代码

实验5 奇数偶数2 (2).png

实验六

实验要求:用if 语句判断主机是否存活。

创建脚本

 [root@localhost ~]# vim ping01.sh
 #!/bin/bash
 #判断主机是否在线
 ​
 read -p "请输入IP地址:" ip
 ​
 ping -c 4 -i 0.5 -w 5 $ip &>/dev/null
 ​
 if [ $? -eq 0 ];then
   echo "$ip 在线"
 else
    echo "$ip 不在线"
 fi
复制代码

实验6.png

执行脚本:

 [root@localhost ~]# bash ping01.sh
 请输入IP地址:192.168.72.10
 192.168.72.10 在线
 [root@localhost ~]# bash ping01.sh
 请输入IP地址:192.168.72.22
 192.168.72.22 不在线
复制代码

实验6 主机在线2.png

实验七

实验要求:用case语句在/etc/init.d/目录中写一个firewalld脚本,并加入到系统服务管理中(#chkconfig: 2345 99 20),使能够使用 service firewalld start|stop|restart|status 来管理firewalld服务;如果命令选项不对,则提示 “用法: $0 {start|stop|status|restart}”。

创建脚本:

 [root@localhost ~]# cd /etc/init.d
 [root@localhost init.d]# vim firewalld
 #!/bin/bash
 #chkconfig: 2345 99 20   //必须加这一行,该脚本才能被chkconfig管理
 ​
 case $1 in
 start)
    echo "正在启动firewalld"
    systemctl start firewalld
    ;;
 stop)
    echo "正在关闭firewalld"
    systemctl stop firewalld
    ;;
 status)
    echo "查看firewalld的状态"
    systemctl status firewalld
    ;;
 restart)
    echo "正在重启firewalld"
    systemctl restart firewalld
    ;;
 *)
    echo "用法: $0 {start|stop|status|restart}"
 esac
复制代码

实验7 服务1 (2).png

执行脚本:

 [root@localhost init.d]# chmod +x firewalld           //为脚本增加执行权限
 [root@localhost init.d]# chkconfig --add firewalld    //把该脚本加入系统服务管理中
 [root@localhost init.d]# chkconfig --list firewalld
 ​
 注:该输出结果只显示 SysV 服务,并不包含
 原生 systemd 服务。SysV 配置数据
 可能被原生 systemd 配置覆盖。
 ​
       要列出 systemd 服务,请执行 'systemctl list-unit-files'。
       查看在具体 target 启用的服务请执行
       'systemctl list-dependencies [target]'。
 ​
 firewalld       0:关    1:关    2:开    3:开    4:开    5:开 6:关
 ​
 [root@localhost init.d]# service firewalld start
 正在启动firewalld
 [root@localhost init.d]# service firewalld stop
 正在关闭firewalld
 [root@localhost init.d]# service firewalld status
 查看firewalld的状态
 ● firewalld.service - firewalld - dynamic firewall daemon
    Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
    Active: inactive (dead) since Fri 2022-04-08 16:29:21 CST; 3s ago
      Docs: man:firewalld(1)
  Main PID: 4813 (code=exited, status=0/SUCCESS)
复制代码

微信图片_20220409011918.png 微信图片_20220409011921.png

小贴士:

编写chkconfig脚本说明

 #!/bin/bash
 # chkconfig: 2345 99 20
 # description: manage firewalld
 # file: /etc/init.d/firewalld
复制代码

第二行:

# chkconfig: 2345 99 20 这一行必须要写,该脚本才能被chkconfig管理,不然执行chkconfig --add xxxx 时(即把该服务加入系统服务管理中),会提示"xxxx 服务不支持chkconfig"。

  • 2345:表示在哪种运行级别下启动,如果是-代表默认的级别(2345)

  • 99:在开机启动脚本时的启动顺序(第99个启动)。S99firewalld(数字越小优先权越高),格式:S+数字+服务名,S代表开机Start。

  • 20:在关机时第20个停止。K20firewalld(数字越小优先权越高),格式:K+数字+服务名,K代表Kill。

作者:聂鲁达的邮差
链接:juejin.cn/post/708490…
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。