第五章 流程控制之case語句

2022-06-23 09:15:09 字數 4659 閱讀 4381

case 變·量 in

模式一)

命令序列1

;;模式二)

命令序列2

;;...

*) 無匹配後命令序列

esac

#判斷使用者的型別

[root@db04 /scripts/day05]# vim user.sh

#!/bin/bash

# author:jh

# time:2020-11-20 10:39:42

# name:user.sh

# version: 1.0

# discription: to

read -p "please input user name:" name

[ -z "$name" ] && echo "the user name must exist" && exit

case $name in

"root")

echo "admin"

;;"jh")

echo "com user"

;;*) echo "other user"

esac

[root@db04 /scripts/day05]# chmod +x user.sh

#編寫nginx啟動指令碼

[root@db04 /scripts/day05]# vim nginx.sh

#!/bin/bash

# author:jh

# time:2020-11-20 11:02:54

# name:nginx.sh

# version: 1.0

# discription: to

[ $# -ne 1 ] && echo "usage: $0 " && exit

function start()

function stop()

case $1 in

"start")

start

;;"stop")

stop

;;"restart")

stop

start

;;"reload")

stop

start

;;"status")

ps aux |grep [n]ginx |grep -q master

if [ $? -eq 0 ];then

echo "nginx is up"

else

echo "ngins is down"

fi;;

*) echo "usage: $0 "

esac

[root@db04 /scripts/day05]# chmod +x nginx.sh

[root@jh shell]# vim nginx_stat.sh

#!/bin/bash

. /etc/init.d/functions

if [ $# -ne 1 ]

then

echo "usage $0 "

exit 1

fiif [ "$1" == "start" ]

then

action "start nginx" /bin/true

elif [ "$1" == "stop" ]

then

action "stop nginx" /bin/true

elif [ "$1" == "restart" ]

then

action "restart nginx" /bin/true

else

echo "usage $0 "

exit 1

fi[root@jh shell]# chmod +x nginx_stat.sh

[root@jh shell]# ./nginx_stat.sh start

start nginx [ 確定 ]

[root@jh shell]# ./nginx_stat.sh restart

restart nginx [ 確定 ]

[root@jh shell]# ./nginx_stat.sh

usage ./nginx_stat.sh

# 儲備知識1

netstat -lntup|grep  ":80\b"  # \b錨定單詞的結尾

# 儲備知識2

action:列印一段資訊並執行給定的命令,然後根據給定命令的執行的結果來呼叫 success,failure方

法,確定最終顯示的內容

[root@jh shell]# action "nginx start is" :

nginx start is                       [  確定 ]

[root@jh shell]# action "nginx start is" /bin/true

nginx start is                       [  確定 ]

[root@jh shell]# action "nginx start is" /bin/false

nginx start is                       [失敗]

# **

[root@jh shell]# vim nginx_stat.sh

#!/bin/bash

. /etc/init.d/functions

args=$1

fun()

case $1 in

start)

netstat -lntup|grep  ":80\b" &>/dev/null

if [ $? -eq 0 ]

then

echo "nginx is runing..."

else

/usr/sbin/nginx

funfi

;;stop)

/usr/sbin/nginx -s stop

fun;;

reload)

/usr/sbin/nginx -s reload

fun;;

restart)

netstat -lntup|grep  ":80\b" &>/dev/null

if [ $? -ne 0 ]

then

/usr/sbin/nginx

[ $? -eq 0 ] && echo "nginx start is ok" || echo "nginx start is

failed"

else

/usr/sbin/nginx -s stop              

[ $? -eq 0 ] && echo "nginx stop is ok" || echo "nginx stop is failed"

sleep 2

/usr/sbin/nginx

funfi

;;status)

netstat -lntup|grep  ":80\b" &>/dev/null

if [ $? -eq 0 ]

then

echo "nginx is runing ..."

else

echo "nginx is not runing ..."

fi;;

*)echo "usage: $0 "

exit 2

esac

#編寫乙個簡易跳板機指令碼

# 儲備知識

linux中斷訊號區別為:鍵入不同、對應操作不同、啟用不同。

1、hup中斷訊號:hup中斷訊號的對應操作為讓程序掛起,睡眠。同2、int中斷訊號:int中斷訊號的對應操作為正常關閉所有程序。同3、term中斷訊號 15:term中斷訊號的對應操作為正常的退出程序。

4、kill中斷訊號 9:kill中斷訊號的對應操作為強制關閉程序。

5、stop 19暫停(同 ctrl + z)

6、cont 18繼續(與stop相反, fg/bg命令)

7、tstp中斷訊號:tstp中斷訊號的對應操作為暫時停用程序。

# **

[root@jh shell]# cat jumpserver.sh

#!/bin/bash

cat<1. backup 10.0.0.41

2. web02  192.168.12.21

3. web03  10.0.0.9

eoftrap "echo 不要亂按鍵盤,否則伺服器將會**" hup int tstp

while true

do  read -p "請輸入連線主機編號資訊: " num

read -p "請輸入賬號: " user

case $num in

1)ssh [email protected]

[ $? -ne 0 ] && echo "connect faild"

;;2)

ssh [email protected]

[ $? -ne 0 ] && echo "connect faild"

;;*)

echo "請輸入連線主機資訊"

esac

done

第五章 PHP流程控制

1.順序結構 2.分支結構 2.1 if.else 1 2 today date w 獲取今天星期幾 3if today 0 else 8 2.2 if.elseif 2.3 switch 3.迴圈結構 3.1 while 1 2 num 1 3while num 100 7 3.2 do.whil...

shell 流程控制 case語句

shell case語句為多選擇語句。可以用case語句匹配乙個值與乙個模式,如果匹配成功,執行相匹配的命令。case語句格式如下 case 值 in 模式1 command1 command2 commandn 模式2 command1 command2 commandn esaccase工作方式...

第五章 語句

由於c primer第五版採用的c 11新標準,手頭沒有支援c 11的編譯器,推薦大家用ideone,支援c 14並且可以輸入輸出。從今天開始整理自己做的課後習題,網上答案很多,但自己寫的也是一種不一樣的思路。exercise 5.20 include include using namespace...