九、case結構條件句應用實踐
(一)case語法結構
case結構條件句相當于多分支if條件陳述句,但是它比這些條件句看起來更規范工整,常被用于實作系統服務腳本等應用場景中,
case陳述句的語法結構:
case "變數" in
值1)
指令1
;;
值2)
指令2
;;
值3)
指令3
;;
*)
指令4
esac
(二)實體,case應用:
[root@centos6-kvm3 scripts]# cat 09-03.sh
#!/bin/bash
cat <<EOF
1.install lnmp
2.install lamp
3.exit
EOF
read -p "請輸入一個數字{1|2|3}:" num
expr $num + 2 &>/dev/null
if [ $? -ne 0 ]
then
echo "usage:$0{1|2|3}"
exit 1
fi
case $num in
1)
echo "install lnmp"
;;
2)
echo "install lamp"
;;
3)
echo "exit"
exit
;;
*)
echo "usage:$0{1|2|3}"
exit 1
esac
(三)實體,不同的分支字體顏色不同:
當用戶輸入對應的數字選擇水果的時候,告訴他選擇的水果是什么,并給水果單詞加上一種顏色(隨意),要求用case陳述句實作,
內容的顏色用數字表示,范圍為30-37,每個數字代表一種顏色,
echo -e "\033[30m 黑色字oldboy trainning \033[0m" #<==30m表示黑色字,
echo -e "\033[31m 紅色字oldboy trainning \033[0m" #<==31m表示紅色字,
echo -e "\033[32m 綠色字oldboy trainning \033[0m" #<==32m表示綠色字,
echo -e "\033[33m 棕色字oldboy trainning \033[0m" #<==33m表示棕色字(brown),和黃色字相近,
echo -e "\033[34m 藍色字oldboy trainning \033[0m" #<==34m表示藍色字,
echo -e "\033[35m 洋紅字oldboy trainning \033[0m" #<==35m表示洋紅色字(magenta),和紫色字相近,
echo -e "\033[36m 藍綠色oldboy trainning \033[0m" #<==36m表示藍綠色字(cyan),和淺藍色字相近,
echo -e "\033[37m 白色字oldboy trainning \033[0m" #<==37m表示白色字,
1、基礎腳本1:
[root@centos6-kvm3 scripts]# cat 09-04.sh
#!/bin/bash
cat <<EOF
1.apple
2.pear
3.banana
4.cherry
EOF
read -p "請輸入一個數字{1|2|3|4}:" num
expr $num + 2 &>/dev/null
if [ $? -ne 0 ]
then
echo "usage:$0 {1|2|3|4}"
exit 1
fi
case $num in
1)
echo -e "\033[31m apple \033[0m"
;;
2)
echo -e "\033[32m pear \033[0m"
;;
3)
echo -e "\033[33m banana \033[0m"
;;
4)
echo -e "\033[34m cherry \033[0m"
;;
*)
echo "usage:$0 {1|2|3|4}"
exit
esac
[root@centos6-kvm3 scripts]#
2、高級腳本2(帶顏色):
顏色函式:
[root@centos6-kvm3 scripts]# cat color.sh
#!/bin/bash
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
tail="\033[0m"
color(){
case $1 in
red)
echo -e "${red}$2${tail}"
;;
green)
echo -e "${green}$2${tail}"
;;
yellow)
echo -e "${yellow}$2${tail}"
;;
blue)
echo -e "${blue}$2${tail}"
;;
*)
echo "usage:$0 please input right content"
esac
}
color $*
[root@centos6-kvm3 scripts]#
功能呼叫顏色函式:
[root@centos6-kvm3 scripts]# cat 09-04.sh
#!/bin/bash
. ./color.sh
cat <<EOF
1.apple
2.pear
3.banana
4.cherry
EOF
read -p "請輸入一個數字{1|2|3|4}:" num
expr $num + 2 &>/dev/null
if [ $? -ne 0 ]
then
echo "usage:$0 {1|2|3|4}"
exit 1
fi
case $num in
1)
color red apple
;;
2)
color green pear
;;
3)
color yellow banana
;;
4)
color blue cheryy
;;
*)
echo "usage:$0 {1|2|3|4}"
exit
esac
[root@centos6-kvm3 scripts]#
字體背景顏色
字的背景顏色對應的數字范圍為40-47,代碼如下,
echo -e "\033[40;37m 黑底白字oldboy\033[0m" #<==40m表示黑色背景,
echo -e "\033[41;37m 紅底白字oldboy\033[0m" #<==41m表示紅色背景,
echo -e "\033[42;37m 綠底白字oldboy\033[0m" #<==42m表示綠色背景,
echo -e "\033[43;37m 棕底白字oldboy\033[0m" #<==43m表示棕色背景(brown),和黃色背景相近,
echo -e "\033[44;37m 藍底白字oldboy\033[0m" #<==44m表示藍色背景,
echo -e "\033[45;37m 洋紅底白字oldboy\033[0m" #<==45m表示洋紅色背景(magenta),和紫色背景相近,
echo -e "\033[46;37m藍綠底白字oldboy\033[0m" #<==46m表示藍綠色背景(cyan),和淺藍色背景相近,
echo -e "\033[47;30m 白底黑字oldboy\033[0m" #<==47m表示白色背景,
(四)rsync啟動基本腳本實體:
[root@centos6-kvm3 scripts]# cat rsync.sh
#!/bin/bash
case $1 in
start)
rsync --daemon
if [ $? -eq 0 ]
then
echo "rsync $1 ok"
else
echo "rsync $1 fail"
fi
;;
stop)
killall rsync
if [ $? -eq 0 ]
then
echo "rsync $1 ok"
else
echo "rsync $1 fail"
fi
;;
restart)
killall rsync && sleep 1 && rsync --daemon
if [ $? -eq 0 ]
then
echo "rsync $1 ok"
else
echo "rsync $1 fail"
fi
;;
*)
echo "usage:$0 {start|stop|restart}"
esac
查看行程:lsof -i:873
rsync啟動高級腳本:
cp rsyncd.sh /etc/init.d/rsyncd
chkconfig --list rsyncd
chkconfig --add rsyncd
chmod +x /etc/init.d/rsyncd
(五)rsync啟動腳本之高階
[root@centos6-kvm3 scripts]# cat rsyncd.sh
# chkconfig: 2345 20 80
# description: rsync start stop
#!/bin/bash
. /etc/init.d/functions
start(){
rsync --daemon
retval=$?
if [ $retval -eq 0 ]
then
action "rsync start ok" /bin/true
return $retval
else
action "rsync start fail" /bin/false
return $retval
fi
}
stop(){
killall rsync &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
action "rsync stop ok" /bin/true
return $retval
else
action "rsync stop fail" /bin/false
return $retval
fi
}
case $1 in
start)
start
retval=$?
;;
stop)
stop
retval=$?
;;
restart)
stop
sleep 2
start
retval=$?
;;
*)
echo "usage:$0 {start|stop|restart}"
esac
exit $retval
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/91657.html
標籤:Linux
