目录
1:#检查用户家目录中的 test.sh 文件是否存在,并且检查是否有执行权限
3:3.用case语句解压根据后缀名为 .tar.gz 或 .tar.bz2 的压缩包到 opt 目录
4:.提示用户输入内容,使用if 语句判断输入的内容是否为整数。
1:#检查用户家目录中的 test.sh 文件是否存在,并且检查是否有执行权限
#/bin/bash
[ -f test.sh ] && echo "exist"
[ -x test.sh ] && echo "exist" || echo "no Execute permissions"
2:#提示用户输入100米赛跑的秒数,要求判断秒数大于0且小于等于10秒的进入选拔赛,大于10秒的都淘汰,
如果输入其它字符则提示重新输入;进入选拔赛的成员再进一步判断男女性别,男生进男生组,女生进女生组,如果输入错误请提示错误
#/bin/bash
read -p "input your time:" s
if [ $s -gt 0 ] && [ $s -le 10 ]
then
read -p "gender:" sex
case $sex in
boy)
echo "you join boyteam"
;;
girl)
echo "you join girlteam"
;;
*)
echo "you input wrong"
esac
else
echo "you eliminate"
fi
~
3:3.用case语句解压根据后缀名为 .tar.gz 或 .tar.bz2 的压缩包到 opt 目录
#/bin/bash
gz=`find /|grep .tar.gz`
gz1=$?
case $gz1 in
[0])
tar -zxf $gz -C /opt
;;
esac
bz=`find /|grep .tar.bz2`
bz1=$?
case $bz1 in
[0])
tar -zxf $bz -C /opt
;;
esac
4:.提示用户输入内容,使用if 语句判断输入的内容是否为整数。
#bin/bash
read -p "you input:" n
let i=n+0 &>/dev/null
if [ $? -ne 0 ]
then
echo "you input decimal fraction "
else
echo "you input integer"
fi
5:5.根据上一题再判断输入的内容是奇数还是偶数。
#bin/bash
read -p "you input:" n
let i=n+0 &>/dev/null
if [ $? -ne 0 ]
then
echo "you input decimal fraction "
else
echo "you input integer"
[ $[ $n % 2 ] = 0 ] && echo "oushu" || echo "jishu"
fi
6:用if 语句判断主机是否存活
#!/bin/bash
ping -c 3 -i 1 $1 &>/dev/null
if [ $? -eq 0 ]
then
echo "yes"
else
echo "no"
fi
7.用case语句在/etc/init.d/目录中写一个firewalld脚本,并加入到系统服务管理中(#chkconfig: 2345 99 20)
使能够使用 service firewalld start|stop|restart|status 来管理firewalld服务,
要求如果命令选项不对,则提示 “用法: $0 {start|stop|status|restart}”。
cd /etc/init.d
vim zy7.sh
#!/bin/bash
#chkconfig: 2345 99 20
read -p “input:start|stop|restart|status: " fw
case $fw in
start)
systemctl start firewalld
echo "open"
;;
stop)
systemctl stop firewalld
echo "off"
;;
restart)
systemctl restart firewalld
echo "restart"
;;
status)
systemctl status firewalld
echo "look"
;;
*)
echo " $0 {start|stop|status|restart}"
;;
esac
chkconfig --add zy7.sh
chkconfig --level 2345 zy7.sh on