@yanglt7
2018-10-28T09:37:29.000000Z
字数 4266
阅读 611
Shell
case "变量" in
值 1)
指令 1...
;;
值 2)
指令 2...
;;
*)
指令 3
esac
例 8-1
执行 Shell 脚本,打印一个如下的水果菜单:
(1)apple
(2)pear
(3)banana
(4)cherry
当用户输入数字选择水果时,告知选择的水果并给水果单词加上一种颜色。
给字体加颜色的命令为:
[root@web001 ~]# echo -e "\E[1;31m red color ylt\E[0m"
red color ylt #<== 打印的字为红色
[root@web001 ~]# echo -e "\033[1;31m red color ylt\033[0m"
red color ylt
在上述命令中:
有关 ANSI 控制码说明如下:
开发脚本:
[root@web001 scripts]# cat fruit.sh
#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
function usage(){
echo "USAGE: $0 {1|2|3|4}"
exit 1
}
function menu(){
cat <<END
1. apple
2. pear
3. banana
END
}
function chose(){
read -p "pls input your choice:" fruit
case "$fruit" in
1)
echo -e "${RED_COLOR}apple${RES}"
;;
2)
echo -e "${GREEN_COLOR}pear${RES}"
;;
3)
echo -e "${BLUE_COLOR}banana${RES}"
;;
*)
usage
esac
}
function main(){
menu
chose
}
main
例 8-2
开发给指定内容加指定颜色的脚本
[root@web001 ~]# echo -e "033[1;30m black color\033[0m"
[root@web001 ~]# echo -e "\033[1;31m red color\033[0m"
[root@web001 ~]# echo -e "\033[1;32m green color\033[0m"
[root@web001 ~]# echo -e "\033[1;33m brown color\033[0m"
[root@web001 ~]# echo -e "\033[1;34m blue color\033[0m"
[root@web001 ~]# echo -e "\033[1;35m magenta color\033[0m"
[root@web001 ~]# echo -e "\033[1;36m cyan color\033[0m"
[root@web001 ~]# echo -e "\033[1;37m white color\033[0m"
[root@web001 scripts]# cat color.sh
#!/bin/bash
function Addcolor(){
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK_COLOR='\E[1;35m'
RES='\E[0m'
if [ $# -ne 2 ]; then
echo "Usage: $0 content {red|yellow|blue|pink}"
exit
fi
case "$2" in
red|RED)
echo -e "${RED_COLOR}$1${RES}"
;;
green|GREEN)
echo -e "${GREEN_COLOR}$1${RES}"
;;
yellow|YELLOW)
echo -e "${YELLOW_COLOR}$1${RES}"
;;
blue|BLUE)
echo -e "${BLUE_COLOR}$1${RES}"
;;
pink|PINK)
echo -e "${PINK_COLOR}$1${RES}"
;;
*)
echo "Usage: $0 content {red|yellow|blue|pink}"
exit
esac
}
function main(){
Addcolor $1 $2
}
main $*
例 8-3
给输出的字符串加背景颜色
echo -e "\033[40;37m 黑底白字 ylt\033[0m"
echo -e "\033[41;37m 红底白字 ylt\033[0m"
echo -e "\033[42;37m 绿底白字 ylt\033[0m"
echo -e "\033[43;37m 棕底白字 ylt\033[0m"
echo -e "\033[44;37m 蓝底白字 ylt\033[0m"
echo -e "\033[45;37m 洋红底白字 ylt\033[0m"
echo -e "\033[46;37m 蓝绿底白字 ylt\033[0m"
echo -e "\033[47;30m 白底黑字 ylt\033[0m"
例 8-4
实现通过传参的方式往 /etc/openvpn_authfile.conf 添加用户:
1)命令用法为:
USAGE: sh adduser {-add|-del|-search} username
2)如果有同名的用户,则不能添加,如果没有对应用户,则无需删除,查找到用户或没有用户时应给出明确的提示。
3)/etc/openvpn_authfile.conf 不能被所有外部用户直接删除及修改。
[root@web001 scripts]# cat auth.sh
#!/bin/bash
#Source function library.
. /etc/init.d/functions
#config file path
FILE_PATH=/etc/openvpn_authfile.conf
[ ! -f $FILE_PATH ] && touch $FILE_PATH
usage(){
cat <<EOF
USAGE: `basename $0` {-add|-del|-search} username
EOF
}
#judge run user
if [ $UID -ne 0 ]; then
echo "You are not supper user,pls call root!"
exit 1;
fi
#judge arg numbers
if [ $# -ne 2 ]; then
usage
exit 2
fi
case "$1" in
-a|-add)
shift #<== remove $1,$2-->$1
if grep "^$1$" ${FILE_PATH} >/dev/null 2&>1; then
action $"vpnuser,$1 is exist." /bin/false
exit
else
chattr -i ${FILE_PATH} #<==lock file
/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
echo "$1" >> ${FILE_PATH}
[ $? -eq 0 ] && action $"Add $1" /bin/true
chattr -i ${FILE_PATH}
fi
;;
-d|del)
shift
if [ `grep "\b$1\b" ${FILE_PATH}|wc -l` -lt 1 ]; then
action $"vpnuser,$1 is not exist," /bin/false
exit
else
chattr -i ${FILE_PATH}
/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
sed -i "/^${1}$/d" ${FILE_PATH}
[ $? -eq 0 ] && action $"Del $1" /bin/true
chattr -i ${FILE_PATH}
exit
fi
;;
-s|-search)
shift
if [ `grep -w "$1" ${FILE_PATH}|wc -l` -lt 1 ]; then
echo $"vpuser,$1 is not exist."; exit
else
echo $"vpnuser,$1 is exist."; exit
fi
;;
*)
usage
exit
;;
esac
例 8-5
实现 Nginx 服务的启动和关闭的功能
[root@web001 scripts]# cat nginx.sh
#!bin/bash
path=/application/nginx/sbin
pid=/application/nginx/logs/nginx.pid
RETVAL=0
. /etc/init.d/functions
start(){
if [ ! -f $pid ];then
$path/nginx
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
action "nginx is started." /bin/true
return $RETVAL
fi
else
echo "nginx is running"
return 0
fi
}
stop(){
if [ -f $pid ]; then
$path/nginx -s stop
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
action "nginx is stopped." /bin/true
return $RETVAL
else
action "nginx is stopped." /bin/false
return $RETVAL
fi
else
echo "nginx is not running"
return $RETVAL
fi
}
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart)
stop
sleep 1
start
RETVAl=$?
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL