case結(jié)構(gòu)條件句的語(yǔ)法格式為:
case "變量" in
? ? 值1)
? ? ? ? 指令1...
? ? ? ? ;;
? ? 值2)
? ? ? ? 指令2...
? ? ? ? ;;
? ? *)
? ? ? ? 指令3...
esac
相當(dāng)于if多分支語(yǔ)句股缸。
if [ "變量"? ="值1"? ]
then
? ? 指令1...
elif? [ "變量"? ="值2"? ]
then
? ? 指令2...
else
? ? 指令3...
fi
將下面語(yǔ)句改成case實(shí)現(xiàn)掘宪。
[root@web01 /server/scripts]# cat like_meinv_fi.sh
cat <<END
? 1.panxiaoting
? 2.gongli
? 3.fanbinbing
END
read -p "Which do you like?Pls input the num:" a
if [ "$a" = "1" ]
then
? ? echo "I guess,you like panxiaoting"? #<==根據(jù)用戶(hù)選擇的結(jié)果,回應(yīng)應(yīng)用輸出溺蕉。
? ? exit 0? ? ? ? ? #<==退出腳本步做,不在向下執(zhí)行凤跑。
elif [ "$a" = "2" ]
then
? ? echo "I guess,you like gongli"
? ? exit 0? ? ? ? ? #<==退出腳本蓝晒,不在向下執(zhí)行。
elif [ "$a" = "3" ]
then
? ? echo "I guess,you like fangbingbing"
? ? exit 0? ? ? ? ? #<==退出腳本城侧,不在向下執(zhí)行。
else
? ? echo "I guess,you are not man."
fi
河南? 賈順平
#!/bin/bash
function menu () {
? ? cat << END
? ? 1.panxiaoting
? ? 2.gongli
? ? 3.fanbinbing
END
}
function chose () {
read -p "Which do you like?Pls input the num:" number
case "$number" in
? ? 1)
? ? ? ? echo "I guess,you like panxiaoting"
? ? ? ? ;;
? ? 2)
? ? ? ? echo "I guess,you like gongli"
? ? ? ? ;;
? ? 3)
? ? ? ? echo "I guess,you like fangbingbing"
? ? ? ? ;;
? ? *)
? ? ? ? echo "請(qǐng)輸入正確的選項(xiàng)"
? ? ? ? exit 1
esac
}
function main () {
? ? menu
? ? chose
}
main
焦向博 河南南陽(yáng)
cat <<END
? 1.panxiaoting
? 2.gongli
? 3.fanbinbing
END
read -p "Which do you like?Pls input the num:" a
case $a in
? ? 1)
? ? echo "I guess,you like panxiaoting"
? ? ;;
? ? 2)
? ? echo "I guess,you like gongli"
? ? ;;? ?
? ? 3)
? ? echo "I guess,you like fangbingbing"
? ? ;;? ?
? ? *)
? ? echo "I guess,you are not man."
esac
謝堂圣 重慶 2019/5/31 8:50:10
#!/bin/bash
cat<<EOF
歡迎來(lái)到老男孩教育!現(xiàn)有以下專(zhuān)業(yè)選擇彼妻。
1. Linux云計(jì)算運(yùn)維(老男孩王牌課程)
2. Python全棧開(kāi)發(fā)
3. 新媒體運(yùn)營(yíng)
EOF
read -p "請(qǐng)選擇你喜歡的專(zhuān)業(yè):" a
case "$a" in
? 1)
? ? echo "你選擇的專(zhuān)業(yè)是:${a}. Linux云計(jì)算運(yùn)維(老男孩王牌課程)"
? ? ;;
? 2)
? ? echo "你選擇的專(zhuān)業(yè)是:${a}. Python全棧開(kāi)發(fā)"
? ? ;;
? 3)
? ? echo "你選擇的專(zhuān)業(yè)是:${a}. 新媒體運(yùn)營(yíng)"
? ? ;;
? *)
? ? echo "對(duì)不起嫌佑,目前只有3個(gè)專(zhuān)業(yè)供你選擇!"
? ? exit;
esac? ? ? ? ?
Linux命令行給字體加顏色命令為:
[root@oldboy scripts]# echo -e "\E[1;31m紅色字oldboy\E[0m"
紅色字oldboy
[root@oldboy scripts]# echo -e "\033[31m紅色字oldboy \033[0m"
紅色字oldboy
在上述命令中:
? echo -e可以識(shí)別轉(zhuǎn)義字符侨歉,這里將識(shí)別特殊字符的含義屋摇,并輸出。
? \E也可以使用\033替代幽邓。
? [1數(shù)字1表示加粗顯示(這個(gè)位置可以加不同的數(shù)字代表不同的意思炮温,詳細(xì)信息可man console_codes獲得)。
? 31m表示為紅色字體牵舵,這個(gè)位置可以換不同的數(shù)字柒啤,以代表不同的意思。
? “紅色字oldboy”表示待設(shè)置的內(nèi)容棋枕。
? [0m表示關(guān)閉所有屬性,這個(gè)位置可以換不同的數(shù)字妒峦,以代表不同的意思重斑。
[root@web01 /server/scripts]# cat plus_color.sh
#!/bin/bash
##############################################################
# File Name: plus_color.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
##############################################################
#!/bin/sh
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
echo -e "${RED_COLOR}oldboy$RES"? #<==變量中間就是待加顏色的字符串。
echo -e "${YELLOW_COLOR}oldgirl$RES"
[root@web01 /server/scripts]# cat 9_2_1.sh
#!/bin/bash
##############################################################
# File Name: 9_2_1.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
##############################################################
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
echo '=====================
1.apple
2.pear
3.banana
4.cherry
====================='
read -p "pls select a num:" num
case "$num" in
? ? 1)? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? echo -e "${RED_COLOR}apple${RES}"
? ? ? ? ;;
? ? 2)
? ? ? ? echo -e "${GREEN_COLOR}pear${RES}"
? ? ? ? ;;
? ? 3)
? ? ? ? echo -e "${YELLOW_COLOR}banana${RES}"
? ? ? ? ;;
? ? 4)
? ? ? ? echo -e "${BLUE_COLOR}cherry${RES}"
? ? ? ? ;;
? ? *)
? ? ? ? echo "muse be {1|2|3|4}"
esac
[root@web01 /server/scripts]# cat 9_2_3.sh
#!/bin/bash
##############################################################
# File Name: 9_2_3.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
##############################################################
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 "must be {1|2|3}"
? ? 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 "${YELLOW_COLOR}banana${RES}"
? ? ? ? ;;
? ? *)
? ? ? ? usage
esac
}
function main(){
? ? menu
? ? chose
}
main
function AddColor(){
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK='\E[1;35m'
RES='\E[0m'
if [ $# -ne 2 ];then
? ? echo "Usage $0 content {red|yellow|blue|green}"
? ? exit
fi
case "$2" in
? ? red|RED)
? ? ? ? echo -e? "${RED_COLOR}$1${RES}"
? ? ? ? ;;
? ? yellow|YELLOW)
? ? ? ? echo -e? "${YELLOW_COLOR}$1${RES}"
? ? ? ? ;;
? ? green|GREEN)
? ? ? ? echo -e? "${GREEN_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|green}"
? ? ? ? exit
esac
}
function main(){
? ? AddColor $1 $2?
}
main $*
[root@web01 /etc/init.d]# cat rsyncd2
#!/bin/bash
# chkconfig: 2345 21 81
# description: startup rsync scripts
PID=/var/run/rsyncd.pid
start(){
if [ -f $PID -a -s $PID ]
? then
? ? ? :
? else
? ? ? rsync --daemon
fi
? ? return $?
}
stop(){
? ? if [ -f $PID -a -s $PID ]
? ? then
? ? ? ? kill `cat $PID`
? ? fi
? ? return $?
}
case "$1" in
? ? start)
? ? ? ? start
? ? ? ? retval=$?
? ? ? ? ;;
? ? stop)
? ? ? ? stop
? ? ? ? retval=$?
? ? ? ? ;;
? ? restart)
? ? ? ? stop
? ? ? ? sleep 2
? ? ? ? start
? ? ? ? retval=$?
? ? ? ? ;;
? ? *)
? ? ? ? echo "Usage肯骇;$0 {start|stop|restart}"
esac
exit $retval
最專(zhuān)業(yè)腳本
https://blog.51cto.com/oldboy/2155931
范例9_7:實(shí)現(xiàn)通過(guò)傳參的方式往/etc/openvpn_authfile.conf里添加用戶(hù)窥浪,具體要求如下祖很。
1)命令用法為:
USAGE: sh adduser {-add|-del|-search} username
2)傳參要求為:
如果參數(shù)為-add時(shí),表示添加后面接的用戶(hù)名漾脂。
如果參數(shù)為-del時(shí)假颇,表示刪除后面接的用戶(hù)名。
如果參數(shù)為-search時(shí)骨稿,表示查找后面接的用戶(hù)名笨鸡。
3)如果有同名的用戶(hù)則不能添加,沒(méi)有對(duì)應(yīng)用戶(hù)則無(wú)需刪除坦冠,查找到用戶(hù)以及沒(méi)有用戶(hù)時(shí)給出明確提示形耗。
4)/etc/openvpn_authfile.conf不能被所有外部用戶(hù)直接刪除及修改
范例9_8:已知Nginx Web服務(wù)的管理命令如下——
啟動(dòng)服務(wù)命令為/application/nginx/sbin/nginx
停止服務(wù)命令為/application/nginx/sbin/nginx -s stop
請(qǐng)用case語(yǔ)句開(kāi)發(fā)腳本實(shí)現(xiàn)Nginx服務(wù)啟動(dòng)及關(guān)閉功能,具體腳本命令為/etc/init.d/nginx {start|stop|restart}辙浑,并實(shí)現(xiàn)可通過(guò)chkconfig進(jìn)行開(kāi)機(jī)自啟動(dòng)管理(CentOS6),
同時(shí)實(shí)現(xiàn)通過(guò)systemctl進(jìn)行開(kāi)機(jī)自啟動(dòng)管理(CentOS7)激涤。
[root@web01 /server/scripts]# cat while1.sh
#!/bin/bash
##############################################################
# File Name: while1.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
##############################################################
while true
do
? ? uptime
? ? usleep 1000000
done
[root@web01 /server/scripts]# cat like_meinv_case_while.sh
while true
do
? ? sleep 2
? ? clear
? ? cat <<END
? ? ? ==============================
? ? ? 1.panxiaoting
? ? ? 2.gongli
? ? ? 3.fanbinbing
? ? ? ==============================
END
? ? read -p "Which do you like?Pls input the num:" a
? ? if [ "$a" = "1" ]
? ? then
? ? ? ? echo "I guess,you like panxiaoting"? #<==根據(jù)用戶(hù)選擇的結(jié)果,回應(yīng)應(yīng)用輸出判呕。
? ? elif [ "$a" = "2" ]
? ? then
? ? ? ? echo "I guess,you like gongli"
? ? elif [ "$a" = "3" ]
? ? then
? ? ? ? echo "I guess,you like fangbingbing"
? ? else
? ? ? ? echo "I guess,you are not man."
? ? fi
done
[root@web01 /server/scripts]# cat check_url_while.sh
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
usage(){
? ? if [[ ! $1 =~ http://www.*com ]]
? ? then
? ? ? ? echo "Usage:$0 http://www.xx.com"
? ? ? ? exit 1
? ? fi
}
check_url(){
wget -q $1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
? ? action? "url is ok." /bin/true
else
? ? action "url is no." /bin/false
fi
}
main(){
? ? while true
? ? do
? ? ? ? usage $1
? ? ? ? check_url $1
? ? ? ? sleep 5
? ? done
}
main $*
Linux系統(tǒng)計(jì)算從1加到100之和
http://oldboy.blog.51cto.com/2561410/767862
作業(yè):
范例10_4:猜數(shù)字游戲倦踢。首先讓系統(tǒng)隨機(jī)生成一個(gè)數(shù)字,給這個(gè)數(shù)字定一個(gè)范圍(1-60)侠草,讓用戶(hù)輸入猜的數(shù)字辱挥,對(duì)輸入進(jìn)行判斷,如果不符合要求梦抢,就給予高或低的提示般贼,猜對(duì)后則給出猜對(duì)用的次數(shù),請(qǐng)用while語(yǔ)句實(shí)現(xiàn)奥吩。
超越了全國(guó)多少用戶(hù)哼蛆。
范例10_5:手機(jī)充值10元,每發(fā)一次短信(輸出當(dāng)前余額)花費(fèi)1角5分錢(qián)霞赫,當(dāng)余額低于1角5分錢(qián)時(shí)不能發(fā)短信腮介,提示“余額不足,請(qǐng)充值”(允許用戶(hù)充值后繼續(xù)發(fā)短信)端衰,請(qǐng)用while語(yǔ)句實(shí)現(xiàn)叠洗。
范例10_6:使用while守護(hù)進(jìn)程方式監(jiān)控網(wǎng)站,每隔10秒確定網(wǎng)站是否正常旅东。
方式1:采用exec讀取文件后灭抑,然后進(jìn)入while循環(huán)處理。
exec<FILE
sum=0
while read line
do
? ? cmd
done
方式2:使用cat讀取文件內(nèi)容抵代,然后通過(guò)管道進(jìn)入while循環(huán)處理腾节。
cat FILE_PATH|while read line
do
? ? cmd
done
方式3:在while循環(huán)結(jié)尾done通過(guò)輸入重定向指定讀取的文件。
while read line
do
? ? cmd
done<FILE
[root@web01 /server/scripts]# cat while2.sh
#!/bin/bash
##############################################################
# File Name: while2.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
##############################################################
sum=0
while read line
do
? ? i=`echo $line|awk '{print $NF}'`
? ? let sum=sum+i
done<./stu.txt
echo $sum