詳解shell腳本中的case條件語(yǔ)句介紹和使用案例
#前言:這篇我們接著寫(xiě)shell的另外一個(gè)條件語(yǔ)句case,上篇講解了if條件語(yǔ)句。case條件語(yǔ)句我們常用于實(shí)現(xiàn)系統(tǒng)服務(wù)啟動(dòng)腳本等場(chǎng)景,case條件語(yǔ)句也相當(dāng)于if條件語(yǔ)句多分支結(jié)構(gòu),多個(gè)選擇,case看起來(lái)更規(guī)范和易讀
#case條件語(yǔ)句的語(yǔ)法格式
case "變量" in 值1) 指令1... ;; 值2) 指令2... ;; *) 指令3... esac
#說(shuō)明:當(dāng)變量的值等于1時(shí),那么就會(huì)相應(yīng)的執(zhí)行指令1的相關(guān)命令輸出,值等于2時(shí)就執(zhí)行指令2的命令,以此類(lèi)推,如果都不符合的話(huà),則執(zhí)行*后面的指令,要注意內(nèi)容的縮進(jìn)距離
#簡(jiǎn)單記憶
case "找工作條件" in 給的錢(qián)多) 給你工作... ;; 給股份) 給你工作... ;; 有發(fā)展前景) 可以試試... ;; *) bye bye !! esac
實(shí)踐1.根據(jù)用戶(hù)的輸入判斷用戶(hù)輸入的是哪個(gè)數(shù)字,執(zhí)行相應(yīng)動(dòng)作
#如果用戶(hù)輸入的是1-9的任意一個(gè)數(shù)字,則輸出對(duì)應(yīng)輸入的數(shù)字,如果是別的字符,則提示輸出不正確并退出程序
[root@shell scripts]# cat num.sh #!/bin/bash #create by guoke #function number input read -p "please input a number:" num #打印信息提示用戶(hù)輸入,輸入信息賦值給num變量 case "$num" in 1) echo "The num you input is 1" ;; [2-5]) echo "The num you input is 2-5" ;; [6-9]) echo "The num you input is 6-9" ;; *) echo "please input number[1-9] int" exit; esac
#說(shuō)明:使用read讀取用戶(hù)輸入的數(shù)據(jù),然后使用case條件語(yǔ)句進(jìn)行判斷,根據(jù)用戶(hù)輸入的值執(zhí)行相關(guān)的操作
#執(zhí)行效果
[root@shell scripts]# sh num.sh
please input a number:1
The num you input is 1
[root@shell scripts]# sh num.sh
please input a number:3
The num you input is 2-5
[root@shell scripts]# sh num.sh
please input a number:4
The num you input is 2-5
[root@shell scripts]# sh num.sh
please input a number:8
The num you input is 6-9
[root@shell scripts]# sh num.sh
please input a number:a
please input number[1-9] int
實(shí)踐2.打印一個(gè)如下的水果菜單
(1) banana
(2) apple
(3)orange
(4) cherry
#腳本編寫(xiě)
[root@shell scripts]# cat menu.sh #!/bin/bash #create by guoke #function print menu RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' RES='\E[0m' echo ' #使用echo打印菜單 ############################# 1.banana 2.apple 3.pear 4.cherry ############################# ' read -p "please select a num:" num case "$num" in 1) echo -e "${YELLOW_COLOR} banana ${RES}" ;; 2) echo -e "${RED_COLOR} apple ${RES}" ;; 3) echo -e "${GREEN_COLOR} pear ${RES}" ;; 4) echo -e "${BLUE_COLOR} cherry ${RES}" ;; *) echo "please input {1|2|3|4}" esac
#說(shuō)明:定義顏色,使用read讀取用戶(hù)輸入的數(shù)據(jù),然后使用case條件語(yǔ)句進(jìn)行判斷,根據(jù)用戶(hù)輸入的值執(zhí)行相關(guān)的操作,給用戶(hù)輸入的水果添加顏色
#擴(kuò)展:輸出菜單的另外種方式
cat<<-EOF =============================== 1.banana 2.apple 3.pear 4.cherry =============================== EOF
#執(zhí)行效果
#如果輸入不正確或者不輸入的話(huà)就打印幫助
[root@shell scripts]# sh menu.sh ############################# 1.banana 2.apple 3.pear 4.cherry ############################# please select a num: please input {1|2|3|4}
#輸入選項(xiàng)中的數(shù)字,打印相關(guān)信息
實(shí)踐3.開(kāi)發(fā)nginx啟動(dòng)腳本
#主要思路:
#1.主要通過(guò)判斷nginx的pid文件有無(wú)存在,通過(guò)返回值查看有沒(méi)有運(yùn)行
#2.通過(guò)case語(yǔ)句獲取參數(shù)進(jìn)行判斷
#3.引入系統(tǒng)函數(shù)庫(kù)functions中的action函數(shù)
#4.對(duì)函數(shù)及命令運(yùn)行的返回值進(jìn)行處理
#5.設(shè)置開(kāi)機(jī)自啟動(dòng)
#附上nginx編譯安裝過(guò)程
#!/bin/bash yum install gcc pcre pcre-devel wget openssl openssl-devel.x86_64 -y mkdir -p /home/demo/tools cd /home/demo/tools/ wget -q http://nginx.org/download/nginx-1.6.3.tar.gz useradd nginx -s /sbin/nologin -M tar xf nginx-1.6.3.tar.gz cd nginx-1.6.3/ ./configure --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module make make install ln -s /application/nginx-1.6.3 /application/nginx/ #做軟連接 /application/nginx/sbin/nginx -t #檢查語(yǔ)法 /application/nginx/sbin/nginx #啟動(dòng)服務(wù)
#腳本編寫(xiě)
[root@shell init.d]# chmod +x /etc/init.d/nginxd [root@shell init.d]# cat nginxd #!/bin/bash #chkconfig: 2345 40 98 #設(shè)定2345級(jí)別,開(kāi)機(jī)第40位啟動(dòng)腳本,關(guān)機(jī)第98位關(guān)閉腳本 #create by guoke #email:1075792988@qq.com #function nginx start scripts [ -f /etc/init.d/functions ] && source /etc/init.d/functions #引入系統(tǒng)函數(shù)庫(kù) PIDFILE=/application/nginx/logs/nginx.pid #定義PID文件路徑 NGINX=/application/nginx/sbin/nginx #定義啟動(dòng)命令路徑 value(){ #定義返回值函數(shù) RETVAL=$? if [ $RETVAL -eq 0 ];then action "Nginx is $1" /bin/true else action "Nginx is $1" /bin/true fi } start(){ #定義啟動(dòng)函數(shù) if [ -f $PIDFILE ];then #判斷PIDFILE存不存在,存在就打印運(yùn)行,否則就啟動(dòng) echo "Nginx is running" else $NGINX value start #調(diào)用返回值函數(shù) fi } stop(){ #定義停止函數(shù) if [ ! -f $PIDFILE ];then #也是通過(guò)判斷PID文件是否存在然后進(jìn)行相關(guān)操作 echo "Nginx not running" else $NGINX -s stop value stop fi } reload(){ #定義重啟函數(shù) if [ ! -f $PIDFILE ];then echo "not open $PIDFILE no such directory" else $nginx -s reload value reload fi } case "$1" in #使用case接收腳本傳參的字符串 start) #如果第一個(gè)參數(shù)為start,調(diào)用start函數(shù) start ;; stop) #如果第一個(gè)參數(shù)為stop,調(diào)用stop函數(shù) stop ;; reload) stop sleep 1 start ;; *) echo "USAGE:$0 {stop|start|reload}" exit 1 esac
#執(zhí)行效果
[root@shell init.d]# sh nginx stop
Nginx is stop [ OK ]
[root@shell init.d]# sh nginx start
Nginx is start [ OK ]
[root@shell init.d]# sh nginx reload
Nginx is stop [ OK ]
Nginx is start [ OK ]
實(shí)踐4.開(kāi)發(fā)跳板機(jī)
#要求用戶(hù)登錄到跳板機(jī)后只能執(zhí)行管理員給定的選項(xiàng)動(dòng)作,不能中斷腳本而到跳板機(jī)服務(wù)器上執(zhí)行任何系統(tǒng)命令
#思路
1.首先做好ssh key驗(yàn)證登錄
2.實(shí)現(xiàn)遠(yuǎn)程連接菜單選擇腳本
3.利用Linux信號(hào)防止用戶(hù)在跳板機(jī)上操作
4.用戶(hù)登錄后就調(diào)用腳本
#操作過(guò)程
3.1.做ssh免密鑰登錄,發(fā)送到各個(gè)主機(jī),如果機(jī)器多的話(huà)可以使用腳本進(jìn)行循環(huán)發(fā)送
[demo@shell ~]$ ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa Generating public/private dsa key pair. Enter file in which to save the key (/home/demo/.ssh/id_dsa): Created directory '/home/demo/.ssh'. Your identification has been saved in /home/demo/.ssh/id_dsa. Your public key has been saved in /home/demo/.ssh/id_dsa.pub. The key fingerprint is: SHA256:BTFfcC2hMKBzuZeUYylC3qgza7z4X6j3RBlwq8Beoak demo@shell The key's randomart image is: +---[DSA 1024]----+ | + o.*...+o | | . = B o O +. . | | = B B * + . | | o + = B + | |E = . + S | | . + o . | | + . o | | o o.o | |..+o... | +----[SHA256]-----+ #命令說(shuō)明:一鍵生成密鑰,不用按回車(chē)。-t:指定要?jiǎng)?chuàng)建的密鑰類(lèi)型,-P:提供舊密碼,空表示不需要密碼,-f:指定位置 #將公鑰拷貝到其他服務(wù)器的demo用戶(hù) [demo@shell ~]$ ssh-copy-id -i .ssh/id_dsa.pub "demo@192.168.86.129" [demo@shell ~]$ ssh-copy-id -i .ssh/id_dsa.pub "demo@192.168.86.130" [demo@shell ~]$ ssh-copy-id -i .ssh/id_dsa.pub demo@192.168.86.131
#3.2.編寫(xiě)腳本
[root@shell scripts]# cat tiaobanji.sh #!/bin/bash trapper(){ #定義屏蔽信號(hào)函數(shù) trap '' INT QUIT TSTP TERM HUB } menu(){ #定義菜單列表函數(shù) cat<<-EOF #加-后面的EOF就可以不用頂格 ==============Host List============== 1) 192.168.86.129 2) 192.168.86.130 3) 192.168.86.131 4) 192.168.86.132 5) exit ===================================== EOF } USER=demo host(){ #定義主機(jī)列表函數(shù) case "$1" in 1) ssh $USER@192.168.86.129 ;; 2) ssh $USER@192.168.86.130 ;; 3) ssh $USER@192.168.86.131 ;; 4) ssh $USER@192.168.86.132 ;; 5) exit esac } main(){ #定義主函數(shù) while : #while循環(huán),一直循環(huán) do trapper #調(diào)用trapper函數(shù) clear #清屏 menu #調(diào)用菜單函數(shù) read -p "please select a num:" num #獲取用戶(hù)輸入 host $num #調(diào)用主機(jī)列表函數(shù)和傳入的參數(shù),進(jìn)行遠(yuǎn)程登錄 done } main #調(diào)用主函數(shù)
#3.3.編寫(xiě)腳本進(jìn)行判斷,判斷是否是root用戶(hù)登錄,如果不是root用戶(hù)就執(zhí)行腳本,彈出跳板機(jī)界面
[root@shell ~]# cd /etc/profile.d/ [root@shell profile.d]# cat jump.sh #!/bin/bash [ $UID -ne 0 ] && . /scripts/tiaobanji.sh
#3.4.測(cè)試
#登錄demo普通用戶(hù)輸入密碼的時(shí)候就會(huì)直接跳到選項(xiàng)卡頁(yè)面了
#選項(xiàng)卡頁(yè)面
==============Host List============== 1) 192.168.86.129 2) 192.168.86.130 3) 192.168.86.131 4) 192.168.86.132 5) exit ===================================== please select a num:1 #進(jìn)行選擇 Last login: Tue Mar 31 23:48:33 2020 from 192.168.86.128 [demo@mysql ~]$
#3.5.提示:跳板機(jī)的安全
1.禁止跳板機(jī)可以從外網(wǎng)IP進(jìn)行登錄,只能從內(nèi)網(wǎng)IP登錄
2.其他服務(wù)器也限制只能內(nèi)網(wǎng)IP登錄,同時(shí)禁止root登錄,做完ssh key認(rèn)證,將密碼登錄禁止,通過(guò)免密碼登錄到其他服務(wù)器
#總結(jié):if條件語(yǔ)句主要用于取值判斷、比較,應(yīng)用比較廣,case條件語(yǔ)句主要是寫(xiě)服務(wù)的啟動(dòng)腳本,各有各的優(yōu)勢(shì)。好了,shell腳本的條件語(yǔ)句就講解到這里了,接下來(lái)會(huì)繼續(xù)寫(xiě)shell腳本的循環(huán)(包括for,while等),如果寫(xiě)的不好的地方還望指出,多多交流提高,下次再會(huì)。。。
到此這篇關(guān)于詳解shell腳本中的case條件語(yǔ)句介紹和使用案例的文章就介紹到這了,更多相關(guān)shell case條件語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
shell 中數(shù)學(xué)計(jì)算總結(jié)
shell中的賦值和操作默認(rèn)都是字符串處理,在此記下shell中進(jìn)行數(shù)學(xué)運(yùn)算的幾個(gè)特殊方法,以后用到的時(shí)候可以來(lái)看,呵呵2012-09-09Shell腳本制作的終端會(huì)話(huà)回放功能腳本分享
這篇文章主要介紹了Shell腳本制作的終端會(huì)話(huà)回放功能腳本分享,本文分兩個(gè)文件,Record.sh是用來(lái)記錄你所執(zhí)行的命令和屏幕的輸出的,而Replay.sh是用來(lái)回放Record.sh所錄制的內(nèi)容的,需要的朋友可以參考下2014-11-11Linux下實(shí)現(xiàn)SSH免密碼登錄和實(shí)現(xiàn)秘鑰的管理、分發(fā)、部署SHELL腳本分享
這篇文章主要介紹了Linux下實(shí)現(xiàn)SSH免密碼登錄和實(shí)現(xiàn)秘鑰的管理、分發(fā)、部署SHELL腳本分享,本文先是講解了SSH免密碼登錄的創(chuàng)建過(guò)程,然后給出了可以分發(fā)、部署密鑰的Shell腳本,需要的朋友可以參考下2014-09-093000字掃盲shell基礎(chǔ)知識(shí)(新手必備)
這篇文章主要介紹了3000字掃盲shell基礎(chǔ)知識(shí),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Linux 按時(shí)間批量刪除文件命令(刪除N天前文件)
這篇文章主要介紹了Linux 按時(shí)間批量刪除文件的命令寫(xiě)法(刪除N天前文件),需要的朋友可以參考下2017-05-05Linux C線(xiàn)程池簡(jiǎn)單實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Linux C線(xiàn)程池簡(jiǎn)單實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07Linux Shell編程繪制國(guó)際象棋棋盤(pán)
這篇文章主要介紹了Linux Shell編程繪制國(guó)際象棋棋盤(pán),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05利用Shell腳本循環(huán)讀取文件中每一行的方法詳解
讀取文件是我們?cè)谌粘9ぷ髦薪?jīng)常遇到的一個(gè)需求,下面這篇文章主要給大家介紹了關(guān)于利用Shell腳本循環(huán)讀取文件中每一行的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09