Linux shell脚本案例

案列:实现通过传参的方式往/etc/user.conf里添加用户

具体要求:

(1)命令用法:

USAGE:sh adduser { -add| -del | -search} username

(2)传参要求:

如果参数为-add时,表示添加后面接的用户

如果参数为-del时,表示删除后面接的用户名

如果参数为 -search 时,表示查找后面接的用户名

(3)如果有同名的用户则不能添加,没有对应用户则无需删除,查找到用户以及没有用户时给出明确提示

(4)/etc/user.conf 不能被所有外部用户直接删除及修改(可以用过UID判断)

如:if [ $UID -ne 0 ];then

​ echo “only root to execute!!!”

# 用于和账户绑定,用于大量用户管理
#!/bin/bash
#判断文件模块:
Path=/etc/user.conf
if [ ! -f $Path ]
then
	touch $Path
fi
chmod 644 $Path
#参数格式模式:
if [ $# -ne 2 ]
then
	echo "USAGE: bash adduser {-add|-del|-sear} username"
	exit 1
fi
Opt=$1
User=$2
if [ `echo $User | egrep "[^a-z,A-Z,0-9,_]" | wc -l` -eq 1 ]
then
	echo "the User format is error"
	exit 2
fi
#功能模块:
FunCheckUser(){
    check=$(grep "$User" $Path|wc -l)
    if [ $check -eq 0 ];then
    	return 0
    else
    	return 1;
    fi
}
# 添加模块
FunAddUser(){
    FunCheckUser
    if [ $? -eq 0 ];then
    	echo "$User" >>$Path
    else
    	echo "THe user is exist"
    	exit 3
    fi
}
# 搜索模块
FunFindUser(){
    FunCheckUser
    if [ $? -eq 0 ];then
    	echo "no user match"
    else
    	echo "$User is having"
    fi
}
# 删除模块
FunDelUser(){
    FunCheckUser
    if [ $? -eq 0 ];then
    	echo "There is no user"
    else
    	sed -i /$User/d $Path
    	exit 5
}

case $Opt in
		-add)
		FunAddUser
		;;
		-del)
		FunDelUser
		;;
		-search)
		FunFindUser
		;;
		*)
		echo "USAGE: bash adduser {-add|-del|-search}"
		;;
esac		

企业级案例:Nginx启动脚本

1、已知nginx管理命令为:

启动:/application/nginx/sbin/nginx

停止:/application/nginx/sbin/nginx -s stop

重新加载:/application/nginx/sbin/nginx -s reload

请用case脚本模拟nginx服务的启动关闭,并可以通过chkconfig(加载到开机自启动项)管理

首先 ps -ef|grep nginx 查看nginx服务是否开启
通过定义三个模块来完成本脚本
 #!/bin/bash
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions#看是否有系统函数,有加载
 pidfile=/application/nginx/logs/nginx.pid
 Start_Nginx(){  #启动模块
     if [ -f $pidfile ];then
     	echo "Nginx is runing"
     else
     	/application/nginx/sbin/nginx &>/dev/null
     	RETVAL=$?
     	[ $RETVAL -eq 0 ] && action "Nginx is Started" /bin/true
     fi
     return $RETVAl
 }
 Stop_Nginx(){
     if [ -f $pidfile ];then
     	/application/nginx/sbin/nginx -s stop &>/dev/null
     	action "Nginx is Stoppped" /bin/true
     else
     	RETVAL=$?
     	[ $RETVAl -eq 0 ] && action "Nginx is Stopped" /bin/false
     fi
     $RETVAL
 }
 Reload_Nginx(){
     if [ -f $pidfile ];then
     	/application/nginx/sbin/nginx -s reload &>/dev/null
     	action "Nginx is Reloaded" /bin/true
     else
     	RETVAl=$?
     	[ $RETVAL -eq 0 ] && echo "Can't open $pisfile .no such file or directory"
     fi
     return $RETVAl
 }
 case $1 in
 	start)
 	Start_Nginx
 	RETVAL=$?
 	;;
 	stop)
 	Stop_Nginx
 	RETVAL=$?
 	;;
 	restart)
 	Stop_Nginx
 	sleep 3
 	Start_Nginx
 	RETVAL=$?
 	;;
 	reload)
 	Reload_Nginx
 	RETVAL=$? #接函数返回值
 	;;
 	*)
 	echo "USAGE:$0 {start|stop|restart|reload}" 
 	exit 1
 	;;
 esac
 exit $RETVAL
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Star星屹程序设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值