菜鸟学Linux 第022篇笔记 case语句
面向过程
控制结构
顺序结构
选择结构
循环结构
面向对象
选择结构
if:单分支、双分支、多分支
单: if condition; then
statement
...
fi
多: if condition; then
statement1
...
elif condition2; then
statement2
...
fi
case 语句:
case SWITCH in
value1)
statement1
...
;;
value2)
statement2
...
;;
*)
statement*
...
;;
esac
e.g.
#!/bin/bash
#
case $1 in
'start')
echo "Start server...";;
'stop')
echo "Stopping server...";;
'status')
echo "Running...";;
'restart')
echo "Restarting server...";;
*)
echo "`basename $0` {start|stop|restart:status}";;
esac
script 1
使脚本接受参数-v|verbalse -add user,user... 添加并显示添加信息
不加-v|verbal 则不显示,并添加-h|--help功能(注 此脚本不可接受错误参数)
#!/bin/bash
#
DEBUG=0
ADD=0
DEL=0
for S in `seq 1 $#`; do
case $1 in
-v|--verbouse)
DEBUG=1
shift ;;
-h|--help)
echo "Usage: `basename $0` --add USER_LIST --del USER_LIST -v|--verbouse -h|--help"
exit 0
;;
-add)
ADD=1
ADDUSERS=$2
shift 2
;;
-del)
DEL=1
DELUSERS=$2
shift 2
#*)
# echo "Usage: `basename $0` --add USER_LIST --del USER_LIST -v|--verbouse -h|--help"
# exit 0
# ;;
esac
done
if [ $ADD -eq 1 ]; then
for USER in `echo $ADDUSERS | sed 's/,/ /g'`; do
if ! id $USER &> /dev/null; then
[ $DEBUG -eq 1 ] && echo "ADD $USER finished."
useradd $USER
else
echo "$USER is exist."
fi
done
fi
if [ $DEL -eq 1 ]; then
for USER in `echo $DELUSERS | sed 's/,/ /g'`; do
if id $USER &> /dev/null; then
[ $DEBUG -eq 1 ] && echo "DEL $USER "
userdel -r $USER
else
echo "No such $USER "
fi
done
fi
本文转自Winthcloud博客51CTO博客,原文链接http://blog.51cto.com/winthcloud/1867717如需转载请自行联系原作者
Winthcloud