前端之家收集整理的这篇文章主要介绍了
shell学习二十二--case语句,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一、case结构条件句语法 case语句适用于需要进行多重分支的应用情况。 case分支语句的格式如下: case $变量名 in 模式1)命令序列1 ;; 模式2)命令序列2 ;; *)默认执行的命令序列 ;; esac 二、颜色的使用 让echo
输出带颜色的
方法有两种 1.在shell脚本中定义颜色变量,使用echo -e
调用变量 2.定义变量的时候,把echo -e定义到变量中,然后
输出变量 可以让echo
输出显示颜色的shell脚本 #!/bin/bash #定义颜色的变量 RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK='\E[1;35m' RES='\E[0m' #需要使用echo -e echo -e "${RED_COLOR}======red color======${RES}" echo -e "${YELOW_COLOR}======yelow color======${RES}" echo -e "${BLUE_COLOR}======green color======${RES}" echo -e "${GREEN_COLOR}======green color======${RES}" echo -e "${PINK}======pink color======${RES}" echo "#############################################################" #直接把echo -e放到变量里面,使用的时候直接
输出变量即可 SET COLOR_SUCCESS="echo -en \\033[1;32m" SET COLOR_FAILURE="echo -en \\033[1;31m" SET COLOR_WARNING="echo -en \\033[1;33m" SET COLOR_NORMAL="echo -en \\033[0;39m" echo ----oldboy trainning----- && $SETCOLOR_SUCCESS echo ----oldboy trainning----- && $SETCOLOR_FAILURE echo ----oldboy trainning----- && $SETCOLOR_WARNING echo ----oldboy trainning----- && $SETCOLOR_NORMAL [root@node01 day9]# vi echo-color01.sh #!/bin/bash blackcolor='\E[1;30m' redcolor='\E[1;31m' greencolor='\E[1;32m' yellowcolor='\E[1;33m' bluecolor='\E[1;34m' purplecolor='\E[1;35m' cyancolor='\E[1;36m' whitecolor='\E[1;37m' echo -e "${blackcolor}black" echo -e "${redcolor}red" echo -e "${greencolor}green" echo -e "${yellowcolor}yellow" echo -e "${bluecolor}blue" echo -e "${purplecolor}purple" echo -e "${cyancolor}cyan" echo -e "${whitecolor}white" 三、例子:根据
用户的选择输入是那种水果并
加上不同的颜色 执行脚本打印一个水果
菜单如下: 1、apple 2、pear 3、banana 4、cherry 当
用户选择水果的时候,打印告诉它选择的水果是什么,并给说过单词
加上一种颜色,要求用case语句实现 [root@node01 day9]# vi fruit_memu.sh #!/bin/sh redcolor='\E[1;31m' greencolor='\E[1;32m' yellowcolor='\E[1;33m' RES='\E[0m' function fruit_menu(){ cat <<END ======================= 1、apple 2、pear 3、banana 4、exit ======================= END } while true do fruit_menu read -p "pls input your choice:" fruit case "$fruit" in 1) echo -e "$redcolor apple $RES" ;; 2) echo -e "$greencolor pear $RES" ;; 3) echo -e "$yellowcolor banana $RES" ;; 4) exit 0 ;; *) echo -e "no fruit you choose." esac done ~ "fruit_memu.sh" 36L,527C written [root@node01 day9]# sh fruit_memu.sh ======================= 1、apple 2、pear 3、banana 4、exit ======================= pls input your choice:1 apple ======================= 1、apple 2、pear 3、banana 4、exit ======================= pls input your choice:2 pear ======================= 1、apple 2、pear 3、banana 4、exit ======================= pls input your choice:3 banana ======================= 1、apple 2、pear 3、banana 4、exit ======================= pls input your choice:4 [root@node01 day9]# [root@node01 day9]# [root@node01 day9]# [root@node01 day9]# sh fruit_memu.sh ======================= 1、apple 2、pear 3、banana 4、exit ======================= pls input your choice:5 no fruit you choose. ======================= 1、apple 2、pear 3、banana 4、exit ======================= pls input your choice: 四、开发一个给
指定内容加指定颜色的脚本 要求: 1、使用read或者传参实现。 2、以传参为例:在脚本行传2个参数,给
指定内容(是第一个参数)加指定的颜色(是第二个参数)。 [root@node01 day9]# cat fruit_memu.sh #!/bin/sh 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 centent {red|yellow|blue|green}" exit fi case "$2" in red|RED) echo -e "${red_color}$1${RES}" ;; yellow|YELLOW) echo -e "${green_color}$1${RES}" ;; blue|BLUE) echo -e "${yellow_color}$1${RES}" ;; green|GREEN) echo -e "${blue_color}$1${RES}" ;; pink|PINK) echo -e "${pink_color}$1${RES}" ;; *) echo "Usage $0 centent {red|yellow|blue|green}" exit esac [root@node01 day9]# sh plus_color.sh wolf red wolf [root@node01 day9]# sh plus_color.sh laolang yellow laolang [root@node01 day9]# sh plus_color.sh zhangs blue zhangs [root@node01 day9]# sh plus_color.sh dd pink dd 使用以上脚本改为
函数加颜色 [root@node01 day9]# vi plus_color_fun.sh #!/bin/sh plus_color(){ 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 centent {red|yellow|blue|green}" exit fi case "$2" in red|RED) echo -e "${red_color}$1${RES}" ;; yellow|YELLOW) echo -e "${green_color}$1${RES}" ;; blue|BLUE) echo -e "${yellow_color}$1${RES}" ;; green|GREEN) echo -e "${blue_color}$1${RES}" ;; pink|PINK) echo -e "${pink_color}$1${RES}" ;; *) echo "Usage $0 centent {red|yellow|blue|green}" exit esac } plus_color "I" red plus_color "am" blue plus_color "wolf" green "plus_color_fun.sh" 37L,681C written You have new mail in /var/spool/mail/root [root@node01 day9]# sh plus_color_fun.sh I am wolf 五、利用case语句开发类是系统启动rsync服务的脚本(可参考系统的rpcbind/nfs/crond) 例如:/etc/init.d/rsyncd (start|stop|restart) rsync --daemon lsof -i :873 ========================================= [root@node01 day10]# cat /etc/rsyncd.conf uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsync.pid lock file = /var/run/rsync.lock log file = /var/log/rsync.log [wolf] path = /data/ ignore errors = yes read only = false write only = false hosts allow = 192.168.0.0/16 hosts deny = * list = false auth users = rsync secrets file = /etc/rsync.passwd ========================================= [root@node01 day10]# cat rsyncd.sh #!/bin/sh . /etc/init.d/functions pidfile=/var/run/rsync.pid function start_rsync(){ if [ -f "$pidfile" ] then echo "rsync is running" else rsync --daemon echo "rsync is started" fi } function stop_rsync(){ if [ -f "$pidfile" ] then kill -9 `cat $pidfile` rm -f $pidfile echo "rsync is stopped" else echo "rsync no running" fi } function usage(){ echo "USAGE:$0 {start|stop|restart}" exit 1 } case "$1" in start) start_rsync RETVAL=$? ;; stop) stop_rsync RETVAL=$? ;; restart) stop_rsync sleep 2 start_rsync RETVAL=$? ;; *) usage esac [root@node01 day10]# sh rsyncd.sh start rsync is started [root@node01 day10]# lsof -i :873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 46561 root 4u IPv4 1244539 0t0 TCP *:rsync (LISTEN) rsync 46561 root 5u IPv6 1244540 0t0 TCP *:rsync (LISTEN) [root@node01 day10]# sh rsyncd.sh stop rsync is stopped [root@node01 day10]# lsof -i :873 You have new mail in /var/spool/mail/root [root@node01 day10]# sh rsyncd.sh rstart USAGE:rsyncd.sh {start|stop|restart} [root@node01 day10]# sh rsyncd.sh restart rsync is running rsync is started [root@node01 day10]# lsof -i :873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 46577 root 4u IPv4 1244705 0t0 TCP *:rsync (LISTEN) rsync 46577 root 5u IPv6 1244706 0t0 TCP *:rsync (LISTEN) 优化脚本 #!/bin/sh # chkconfig: 2345 34 61 # description: start rsync and stop rsync scripts. . /etc/init.d/functions pidfile=/var/run/rsync.pid function start_rsync(){ if [ -f "$pidfile" ] then echo "rsync is running" else rsync --daemon action "rsync is started" /bin/true fi } function stop_rsync(){ if [ -f "$pidfile" ] then kill -9 `cat $pidfile` rm -f $pidfile action "rsync is stopped" /bin/true else action "rsync no running" /bin/false fi } function usage(){ echo "USAGE:$0 {start|stop|restart}" exit 1 } case "$1" in start) start_rsync RETVAL=$? ;; stop) stop_rsync RETVAL=$? ;; restart) stop_rsync sleep 2 start_rsync RETVAL=$? ;; *) usage esac [root@node01 day10]# sh rsyncd.sh restart rsync is stopped [ OK ] rsync is started [ OK ] [root@node01 day10]# sh rsyncd.sh stop rsync is stopped [ OK ] [root@node01 day10]# sh rsyncd.sh stop rsync no running [
Failed] [root@node01 day10]# sh rsyncd.sh stop rsync no running [
Failed] [root@node01 day10]# sh rsyncd.sh start rsync is started [ OK ] 加入自启动 cp rsyncd.sh /etc/init.d/rsyncd [root@node01 day10]# chkconfig --add rsyncd [root@node01 day10]# chkconfig --list [root@node01 day10]# ll /etc/rc.d/rc3.d/|grep 34
原文链接:https://www.f2er.com/bash/389608.html