@yanglt7
        
        2018-10-28T09:37:29.000000Z
        字数 4266
        阅读 716
    Shell
case "变量" in值 1)指令 1...;;值 2)指令 2...;;*)指令 3esac
例 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/bashRED_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 <<END1. apple2. pear3. bananaEND}function chose(){read -p "pls input your choice:" fruitcase "$fruit" in1)echo -e "${RED_COLOR}apple${RES}";;2)echo -e "${GREEN_COLOR}pear${RES}";;3)echo -e "${BLUE_COLOR}banana${RES}";;*)usageesac}function main(){menuchose}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/bashfunction 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 ]; thenecho "Usage: $0 content {red|yellow|blue|pink}"exitficase "$2" inred|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}"exitesac}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 pathFILE_PATH=/etc/openvpn_authfile.conf[ ! -f $FILE_PATH ] && touch $FILE_PATHusage(){cat <<EOFUSAGE: `basename $0` {-add|-del|-search} usernameEOF}#judge run userif [ $UID -ne 0 ]; thenecho "You are not supper user,pls call root!"exit 1;fi#judge arg numbersif [ $# -ne 2 ]; thenusageexit 2ficase "$1" in-a|-add)shift #<== remove $1,$2-->$1if grep "^$1$" ${FILE_PATH} >/dev/null 2&>1; thenaction $"vpnuser,$1 is exist." /bin/falseexitelsechattr -i ${FILE_PATH} #<==lock file/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)echo "$1" >> ${FILE_PATH}[ $? -eq 0 ] && action $"Add $1" /bin/truechattr -i ${FILE_PATH}fi;;-d|del)shiftif [ `grep "\b$1\b" ${FILE_PATH}|wc -l` -lt 1 ]; thenaction $"vpnuser,$1 is not exist," /bin/falseexitelsechattr -i ${FILE_PATH}/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)sed -i "/^${1}$/d" ${FILE_PATH}[ $? -eq 0 ] && action $"Del $1" /bin/truechattr -i ${FILE_PATH}exitfi;;-s|-search)shiftif [ `grep -w "$1" ${FILE_PATH}|wc -l` -lt 1 ]; thenecho $"vpuser,$1 is not exist."; exitelseecho $"vpnuser,$1 is exist."; exitfi;;*)usageexit;;esac
例 8-5 实现 Nginx 服务的启动和关闭的功能
[root@web001 scripts]# cat nginx.sh#!bin/bashpath=/application/nginx/sbinpid=/application/nginx/logs/nginx.pidRETVAL=0. /etc/init.d/functionsstart(){if [ ! -f $pid ];then$path/nginxRETVAL=$?if [ $RETVAL -eq 0 ]; thenaction "nginx is started." /bin/truereturn $RETVALfielseecho "nginx is running"return 0fi}stop(){if [ -f $pid ]; then$path/nginx -s stopRETVAL=$?if [ $RETVAL -eq 0 ]; thenaction "nginx is stopped." /bin/truereturn $RETVALelseaction "nginx is stopped." /bin/falsereturn $RETVALfielseecho "nginx is not running"return $RETVALfi}case "$1" instart)startRETVAL=$?;;stop)stopRETVAL=$?;;restart)stopsleep 1startRETVAl=$?;;*)echo $"Usage: $0 {start|stop|restart}"exit 1esacexit $RETVAL