1.给脚本service.sh进行修改,当执行的时候要求输入(1、2、3、4、5)时安装对应的httpd、vim、wget、更换aliyum等功能,当输入错误 时提示应该输入正确的值但是不会退出。
[root@bogon yy]# cat service.sh
#!/bin/bash
while :
do
cat <<-EOF
+-------------------------------------------------------------------------+
| System_tools V1.0 |
+-------------------------------------------------------------------------+
| 1. 更换阿里源. |
| 2. 安装http. |
| 3. 安装vim. |
| 4. 安装wget. |
| 5. exit. |
+-------------------------------------------------------------------------+
EOF
read -p "请输入你的选项:" cce
case $cce in1)rm -rf /etc/yum.repos.d/* &> /dev/nullcurl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/nullif [ $? -eq 0 ];thenecho "yum源更换成功"elseecho "yum源更换失败,请手动安装"fi;;2)yum -y remove httpd &> /dev/nullyum -y install httpd &> /dev/null systemctl restart httpd &> /dev/nullif [ $? -eq 0 ];thenecho "http ok!"elseecho "http fialed,请手动安装"fi;;3)yum -y install vim &> /dev/nullif [ $? -eq 0 ];thenecho "vim ok!"elseecho "vim nooo!!!"fi;;4)yum -y install wget &> /dev/nullif [ $? -eq 0 ];thenecho "wget ok"elseecho "wget nooo!!!"fi;;5)exit 0;;
esac
done
2.建立批量删除用户脚本
#!/bin/bash
read -p "请输入用户号:" nm
read -p "请输入要删除的个数:" num
read -p "确认要删除$na[y|Y|n]" x
if [ $x = y ] || [ $x = Y ];then
for i in $(seq 1 $num)
doecho "$i"user=$nm"$i"id $user &> /dev/nullif [ $? -eq 0 ];thenuserdel -r $userecho "删好了"elseexit 1fi
done
elseexit 0
fi
3.随机数实现猜数字游戏
#!/bin/bash
num=$[RANDOM % 10] 0-9
i=1 #定义i为循环条件
a=0 #定义a为猜的次数变量
while [ $i -eq 1 ]
do
read -p "请输入你猜的数字(0~10):" cif [ $c -eq $num ];thenecho "恭喜你猜对了!"let a++let i++elif [ $c -gt $num ];thenecho "你猜高了,请继续!"let a++elif [ $c -lt $num ];thenecho "你猜低了,请继续!"let a++fi
done
echo "你一共猜了${a}次。"