1、案例1
- /opt/useradd.sh,要求提示用户输入用户名,输入的用户名zhangsan存在的话,提示user is exist,不存在的话,user not exist ,创建zhangsan用户,并且创建一个/opt/zhangsan目录,修改权限666,如果输入的不是zhangsan,则输出please input right username,并且退出脚本
read -p "input user zhangsan " get_user
if [[ "$get_user" != "zhangsan" ]];thenecho "please input right username"exit 100
fi
if id $get_user &> /dev/null;thenecho "user is exist"
elseecho "user not exist"useradd $get_userdir_path="/opt/$get_user"if [ ! -d $dir_path ];then # 目录不存在,这个表达式取反就是为真,执行代码块,目录存在这个,返回的结果为假,不执行代码块,解决了这个目录是否存在的问题mkdir /opt/zhangsanfichmod 666 /opt/zhangsan
fi
-
编写一个/opt/service_status,用于管理vsftpd服务的状态
-
start,如果已经启动了输出vsftpd is active,否则启动,vsftp start
-
stop,停止了,输出vsftpd is inactive,否则 vsftp stop
-
restart,输出vsftpd
-
exist,退出脚本,只有输入exist才能退出脚本
-
# 首先使用 [[ ]] 这个条件测试的语句,这个的话,就需要比较,# 如果直接使用命令的话,也就是 &> 正确和错误的输出都会接收,然后有一个返回值为真或者假
[root@master opt]# cat service_status.sh
while true
doread -p "intput stats:|start|stop|restart|exit " get_statuscase $get_status instart)if systemctl is-active vsftpd &> /dev/null;thenecho "vsftpd is active"elsesystemctl start vsftpdfi;;stop)if ! systemctl is-active vsftpd &> /dev/null;thenecho "vsftpd is inactive"elsesystemctl stop vsftpdfi;;restart)if systemctl restart vsftpd;thenecho "vsftpd restart"fi;;exit)break;;*)echo "输入正确的";;esacdone
-
/opt/file_status.sh ,提示用户输入文件的路径/tmp/tmpfile,不存在的话输出file is not exist,并且退出脚本,如果文件存在的话,继续下面操作
-
判断文件是否存在redhat,出现了话,修改bob用户的描述信息为mysql
-
不存在的话,输出redhat not exist
-
[root@master opt]# cat file_status.sh
read -p "input /tmp/tmpfile: " get_file
if [ ! -f $get_file ];thenecho "file not exist"exit 100
elseif grep redhat "$get_file" &> /dev/null;thenusermod -c "mysql" bobelseecho "redhat not exist"fi
fi
2、网络检查脚本
3、查看指定服务是否运行
-
需求就是输入服务,如果已经启动了,就不需要了
-
如果就是服务没有启动的话,就会启动了
-
$1 参数用来控制
[root@master shell]# cat 10.sh
echo "请输入一个服务:"if [ ! $# -eq 1 ];thenecho "没有输入参数error"exit 100
fiif systemctl is-active --quiet $1;thenecho "$1 is active"
elsesystemctl start $1echo "### vsftpd start ###"
fi
4、用户登录脚本
[root@master shell]# cat 11.sh
echo "欢迎回来: $(whoami)"
echo "当前时间: $(date)"
echo "系统已运行: $(uptime -p)"
echo "登录用户:"
who