濮阳杆衣贸易有限公司

主頁 > 知識庫 > 詳解shell腳本中的case條件語句介紹和使用案例

詳解shell腳本中的case條件語句介紹和使用案例

熱門標(biāo)簽:AI智能電銷機(jī)器人壞處 沈陽ai電銷智能機(jī)器人 電話機(jī)器人對家居行業(yè)幫助大嗎 如何申請400的電話呀 地圖標(biāo)注審核周期 蘭州電銷機(jī)器人加盟 合肥電銷外呼系統(tǒng)供應(yīng)商 黑暗之魂3地圖標(biāo)注 電商外呼系統(tǒng)排名

#前言:這篇我們接著寫shell的另外一個(gè)條件語句case,上篇講解了if條件語句。case條件語句我們常用于實(shí)現(xiàn)系統(tǒng)服務(wù)啟動(dòng)腳本等場景,case條件語句也相當(dāng)于if條件語句多分支結(jié)構(gòu),多個(gè)選擇,case看起來更規(guī)范和易讀

#case條件語句的語法格式

case "變量" in
 值1)
 指令1...
 ;;
 值2)
 指令2...
 ;;
 *)
 指令3...
esac

#說明:當(dāng)變量的值等于1時(shí),那么就會相應(yīng)的執(zhí)行指令1的相關(guān)命令輸出,值等于2時(shí)就執(zhí)行指令2的命令,以此類推,如果都不符合的話,則執(zhí)行*后面的指令,要注意內(nèi)容的縮進(jìn)距離

#簡單記憶

case "找工作條件" in
 給的錢多)
 給你工作...
 ;;
 給股份)
 給你工作...
 ;;
 有發(fā)展前景)
 可以試試...
 ;;
 *)
 bye bye !!
esac

實(shí)踐1.根據(jù)用戶的輸入判斷用戶輸入的是哪個(gè)數(shù)字,執(zhí)行相應(yīng)動(dòng)作

#如果用戶輸入的是1-9的任意一個(gè)數(shù)字,則輸出對應(yīng)輸入的數(shù)字,如果是別的字符,則提示輸出不正確并退出程序

[root@shell scripts]# cat num.sh 
#!/bin/bash

#create by guoke
#function number input

read -p "please input a number:" num #打印信息提示用戶輸入,輸入信息賦值給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

#說明:使用read讀取用戶輸入的數(shù)據(jù),然后使用case條件語句進(jìn)行判斷,根據(jù)用戶輸入的值執(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

#腳本編寫

[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

#說明:定義顏色,使用read讀取用戶輸入的數(shù)據(jù),然后使用case條件語句進(jìn)行判斷,根據(jù)用戶輸入的值執(zhí)行相關(guān)的操作,給用戶輸入的水果添加顏色

#擴(kuò)展:輸出菜單的另外種方式

cat-EOF
===============================
 1.banana
 2.apple
 3.pear
 4.cherry
===============================
EOF

#執(zhí)行效果

#如果輸入不正確或者不輸入的話就打印幫助

[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.開發(fā)nginx啟動(dòng)腳本

#主要思路:

#1.主要通過判斷nginx的pid文件有無存在,通過返回值查看有沒有運(yùn)行

#2.通過case語句獲取參數(shù)進(jìn)行判斷

#3.引入系統(tǒng)函數(shù)庫functions中的action函數(shù)

#4.對函數(shù)及命令運(yùn)行的返回值進(jìn)行處理

#5.設(shè)置開機(jī)自啟動(dòng)

#附上nginx編譯安裝過程

#!/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 #檢查語法
/application/nginx/sbin/nginx #啟動(dòng)服務(wù)

#腳本編寫

[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ī)第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ù)庫

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 #也是通過判斷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.開發(fā)跳板機(jī)

#要求用戶登錄到跳板機(jī)后只能執(zhí)行管理員給定的選項(xiàng)動(dòng)作,不能中斷腳本而到跳板機(jī)服務(wù)器上執(zhí)行任何系統(tǒng)命令

#思路

1.首先做好ssh key驗(yàn)證登錄
2.實(shí)現(xiàn)遠(yuǎn)程連接菜單選擇腳本
3.利用Linux信號防止用戶在跳板機(jī)上操作
4.用戶登錄后就調(diào)用腳本

#操作過程

3.1.做ssh免密鑰登錄,發(fā)送到各個(gè)主機(jī),如果機(jī)器多的話可以使用腳本進(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]-----+
#命令說明:一鍵生成密鑰,不用按回車。-t:指定要?jiǎng)?chuàng)建的密鑰類型,-P:提供舊密碼,空表示不需要密碼,-f:指定位置

#將公鑰拷貝到其他服務(wù)器的demo用戶
[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.編寫腳本

[root@shell scripts]# cat tiaobanji.sh 
#!/bin/bash

trapper(){ #定義屏蔽信號函數(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 #獲取用戶輸入
 host $num #調(diào)用主機(jī)列表函數(shù)和傳入的參數(shù),進(jìn)行遠(yuǎn)程登錄
 done
}
main #調(diào)用主函數(shù)

#3.3.編寫腳本進(jìn)行判斷,判斷是否是root用戶登錄,如果不是root用戶就執(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.測試

#登錄demo普通用戶輸入密碼的時(shí)候就會直接跳到選項(xiàng)卡頁面了

#選項(xiàng)卡頁面

==============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)證,將密碼登錄禁止,通過免密碼登錄到其他服務(wù)器

#總結(jié):if條件語句主要用于取值判斷、比較,應(yīng)用比較廣,case條件語句主要是寫服務(wù)的啟動(dòng)腳本,各有各的優(yōu)勢。好了,shell腳本的條件語句就講解到這里了,接下來會繼續(xù)寫shell腳本的循環(huán)(包括for,while等),如果寫的不好的地方還望指出,多多交流提高,下次再會。。。

到此這篇關(guān)于詳解shell腳本中的case條件語句介紹和使用案例的文章就介紹到這了,更多相關(guān)shell case條件語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 解決java.lang.ClassCastException的java類型轉(zhuǎn)換異常的問題
  • 淺談java switch如果case后面沒有break,會出現(xiàn)什么情況?
  • Java CAS基本實(shí)現(xiàn)原理代碼實(shí)例解析
  • Element Cascader 級聯(lián)選擇器的使用示例
  • Python基于字典實(shí)現(xiàn)switch case函數(shù)調(diào)用
  • 淺談keras中的后端backend及其相關(guān)函數(shù)(K.prod,K.cast)
  • springboot集成CAS實(shí)現(xiàn)單點(diǎn)登錄的示例代碼
  • Python Switch Case三種實(shí)現(xiàn)方法代碼實(shí)例
  • Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式
  • Compare And Swap底層原理及代碼示例詳解

標(biāo)簽:黔南 通遼 河北 隴南 淮南 河池 常州 黔南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解shell腳本中的case條件語句介紹和使用案例》,本文關(guān)鍵詞  詳解,shell,腳本,中的,case,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳解shell腳本中的case條件語句介紹和使用案例》相關(guān)的同類信息!
  • 本頁收集關(guān)于詳解shell腳本中的case條件語句介紹和使用案例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    饶河县| 栾川县| 南昌县| 英山县| 怀远县| 白银市| 收藏| 阿城市| 瑞金市| 梧州市| 新平| 宁乡县| 通山县| 安宁市| 乌拉特前旗| 仁怀市| 浏阳市| 咸宁市| 上栗县| 永州市| 麻城市| 张家界市| 徐州市| 蒙自县| 息烽县| 仙游县| 平邑县| 桃江县| 英吉沙县| 皮山县| 商水县| 天等县| 淮北市| 肇庆市| 星座| 洞口县| 包头市| 平谷区| 连南| 射洪县| 丰顺县|