日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux——shell 中常用的控制语句 for、while、if、case、expect、exit、break、continue

發(fā)布時間:2025/3/19 linux 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux——shell 中常用的控制语句 for、while、if、case、expect、exit、break、continue 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、 for 語句

命令語法如下:

for NUM in 1 2 3 for NUM in {1..3} for NUM in `seq 1 3`或者for NUM in `seq 1 2 10` for (( 表達式1;表達式2;表達式3)) do done

for 語句演示

[root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bash for NUM in 1 2 3 doecho $NUM done [root@desktop27 mnt]# sh for.sh 1 2 3 [root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bash for NUM in {1..3} doecho $NUM done [root@desktop27 mnt]# sh for.sh 1 2 3 [root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bash for NUM in `seq 1 3` doecho $NUM done [root@desktop27 mnt]# sh for.sh 1 2 3 [root@desktop27 mnt]# [root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bash for NUM in `seq 1 2 5` doecho $NUM done [root@desktop27 mnt]# sh for.sh 1 3 5 [root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bash for ((NUM=1;NUM<=3;NUM++)) doecho $NUM done [root@desktop27 mnt]# sh for.sh 1 2 3 [root@desktop27 mnt]#

for 語句——備份數(shù)據(jù)庫

[root@desktop27 mnt]# yum install mariadb-server -y [root@desktop27 mnt]# systemctl start mariadb [root@desktop27 mnt]# mysql -uroot ##進行數(shù)據(jù)庫設置 [root@desktop27 mnt]# mysql -uroot -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | linux | | mysql | | performance_schema | | test | | westos | +--------------------+ [root@desktop27 mnt]# mysql -uroot -EN -e "show databases;" *************************** 1. row *************************** information_schema *************************** 2. row *************************** linux *************************** 3. row *************************** mysql *************************** 4. row *************************** performance_schema *************************** 5. row *************************** test *************************** 6. row *************************** westos [root@desktop27 mnt]# vim dump_mysql.sh [root@desktop27 mnt]# cat dump_mysql.sh #!/bin/bash DATABASE_MESSAGE=`mysql -uroot -EN -e "show databases;" | grep -E "^\*|schema$" -v` mkdir -p /mnt/mysql_dump for DATABASE_NAME in $DATABASE_MESSAGE do mysqldump -uroot $DATABASE_NAME > /mnt/mysql_dump/${DATABASE_NAME}.sql [ "$?" -eq "0" ]&&{ echo -e "\033[32m$DATABASE_NAME is backuped !!\033[0m" } done [root@desktop27 mnt]# sh dump_mysql.sh linux is backuped !! mysql is backuped !! test is backuped !! westos is backuped !! [root@desktop27 mnt]# ls mysql_dump/ linux.sql mysql.sql test.sql westos.sql [root@desktop27 mnt]#

二、while

命令語法如下:

while test_commod ;douser_commods; done

只要test_commod命令返回0,則執(zhí)行user_commods命令塊;
while循環(huán)結束后,整個命令塊的返回值,為user_commods命令最后一個命令的返回值,如果user_commods命令塊沒有執(zhí)行,則整個返回值為0

while語句演示

##兩種寫法,效果一樣 [root@desktop27 mnt]# vim while.sh [root@desktop27 mnt]# cat while.sh #!/bin/bash while true doecho -n `uptime`echo -ne "\r \r"sleep 5 5秒刷新一次 done [root@desktop27 mnt]# sh while.sh ^C:14:09 up 30 min, 2 users, load average: 0.00, 0.01, 0.05 [root@desktop27 mnt]# [root@desktop27 mnt]# cat while.sh #!/bin/bash while true doecho -ne "\r`uptime` \r"sleep 5 done [root@desktop27 mnt]# sh while.sh ^C2:18:52 up 34 min, 2 users, load average: 0.07, 0.07, 0.05 [root@desktop27 mnt]#

三、if

命令語法如下:

if then elif then . . . elif then esle fi

IF 語法演示

[root@desktop27 mnt]# vim if.sh [root@desktop27 mnt]# cat if.sh #!/bin/bash if [ "$1" = "a" ] thenecho '$1' is a elif [ "$1" = "b" ] thenecho '$1' is b elif [ "$1" = "c" ] thenecho '$1' is c elseecho unknown $1 fi [root@desktop27 mnt]# sh if.sh a $1 is a [root@desktop27 mnt]# sh if.sh b $1 is b [root@desktop27 mnt]# sh if.sh c $1 is c [root@desktop27 mnt]# sh if.sh d unknown d [root@desktop27 mnt]# sh if.sh h unknown h [root@desktop27 mnt]#

1、字符串判斷
str1 = str2      當兩個串有相同內容、長度時為真
str1 != str2      當串str1和str2不等時為真
-n str1        當串的長度大于0時為真(串非空)
-z str1        當串的長度為0時為真(空串)
str1         當串str1為非空時為真
2、數(shù)字的判斷
int1 -eq int2    兩數(shù)相等為真
int1 -ne int2    兩數(shù)不等為真
int1 -gt int2    int1大于int2為真
int1 -ge int2    int1大于等于int2為真
int1 -lt int2    int1小于int2為真
int1 -le int2    int1小于等于int2為真
3、文件的判斷
-r file     用戶可讀為真
-w file     用戶可寫為真
-x file     用戶可執(zhí)行為真
-f file     文件為正規(guī)文件為真
-d file     文件為目錄為真
-c file     文件為字符特殊文件為真
-b file     文件為塊特殊文件為真
-s file     文件大小非0時為真
-t file     當文件描述符(默認為1)指定的設備為終端時為真
4、復雜邏輯判斷
-a         與
-o        或
!        非

四、case

case 語句匹配一個值或一個模式,如果匹配成功,執(zhí)行相匹配的命令。

命令語法如下:

casein 模式1)command1command2command3 ;; 模式2)command1command2command3 ;; *)command1command2command3 ;; esac

case工作方式如上所示。取值后面必須為關鍵字 in,每一模式必須以右括號結束。取值可以為變量或常數(shù)。匹配發(fā)現(xiàn)取值符合某一模式后,其間所有命令開始執(zhí)行直至 ;;。;; 與其他語言中的 break 類似,意思是跳到整個 case 語句的最后。
取值將檢測匹配的每一個模式。一旦模式匹配,則執(zhí)行完匹配模式相應命令后不再繼續(xù)其他模式。如果無一匹配模式,使用星號 * 捕獲該值,再執(zhí)行后面的命令。

case語句演示

##if——字符匹配,有先后順序 [root@desktop27 mnt]# vim if.sh [root@desktop27 mnt]# cat if.sh #!/bin/bash if [ "$1" = "dog" ] thenecho "cat" elif [ "$1" = "cat" ] thenecho "dog" elseecho -e "\033[31mERROR: unknown $1\033[0m" fi [root@desktop27 mnt]# sh -x if.sh cat + '[' cat = dog ']' + '[' cat = cat ']' + echo dog dog [root@desktop27 mnt]# sh -x if.sh dog + '[' dog = dog ']' + echo cat cat [root@desktop27 mnt]# sh -x if.sh boy + '[' boy = dog ']' + '[' boy = cat ']' + echo -e '\033[31mERROR: unknown boy\033[0m' ERROR: unknown boy [root@desktop27 mnt]# ##case——匹配字符,沒有先后順序,效率更高 [root@desktop27 mnt]# vim case.sh [root@desktop27 mnt]# cat case.sh #!/bin/bash case $1 indog)echo cat;;cat)echo dog;;*)echo error esac [root@desktop27 mnt]# sh -x case.sh cat + case $1 in + echo dog dog [root@desktop27 mnt]# sh -x case.sh dog + case $1 in + echo cat cat [root@desktop27 mnt]# sh -x case.sh boy + case $1 in + echo error error [root@desktop27 mnt]#

五、expect

yum install expect -y 安裝 expect 工具
expect 是自動應答命令,用于交互式命令的自動執(zhí)行
spawn 是 expect 中的監(jiān)控程序,其運行后會監(jiān)控命令提出的交互問題
send 發(fā)送問題答案給交互命令
“\r” 表示會車
exp_continue 表示當問題不存在時繼續(xù)回答下面的問題
expect eof 表示問題回答完畢后退出 expect 環(huán)境
interact 表示問題回答完畢后留在交互頁面
set NAME [ lindex $argv n ] 定義變量

寫成兩個文件 .sh 和 .exp 的例子

[root@desktop27 mnt]# vim ask.sh [root@desktop27 mnt]# cat ask.sh #!/bin/bash read -p "What's your name: " NAME read -p "How old are you: " AGE read -p "Which obj you study: " OBJ read -p "Are you happy? " FEEL echo "$NAME is $AGE's old and study $OBJ feel $FEEL" [root@desktop27 mnt]# sh ask.sh What's your name: tutu How old are you: 18 Which obj you study: linux Are you happy? happy tutu is 18's old and study linux feel happy [root@desktop27 mnt]# vim answer.exp [root@desktop27 mnt]# cat answer.exp #!/usr/bin/expect set timeout 2 spawn /mnt/ask.sh expect "name:" send "tutu\r" expect "old:" send "18\r" expect "study:" send "linux\r" expect "happy:" send "happy\r" expect eof [root@desktop27 mnt]# expect answer.exp spawn /mnt/ask.sh couldn't execute "/mnt/ask.sh": permission deniedwhile executing "spawn /mnt/ask.sh"(file "answer.exp" line 2) [root@desktop27 mnt]# chmod -x /mnt/ask.sh [root@desktop27 mnt]# expect answer.exp spawn /mnt/ask.sh What's your name: tutu How old are you: 18 Which obj you study: linux Are you happy? happy tutu is 18's old and study linux feel happy [root@desktop27 mnt]# vim answer.exp [root@desktop27 mnt]# cat answer.exp #!/usr/bin/expect set timeout 2 spawn /mnt/ask.sh expect { name { send "tutu\r";exp_continue }old { send "18\r";exp_continue }study { send "linux\r";exp_continue }happy { send "happy\r" } } expect eof [root@desktop27 mnt]# chmod +x answer.exp [root@desktop27 mnt]# /mnt/answer.exp spawn /mnt/ask.sh What's your name: tutu How old are you: 18 Which obj you study: linux Are you happy? happy tutu is 18's old and study linux feel happy [root@desktop27 mnt]# vim answer.exp [root@desktop27 mnt]# cat answer.exp #!/usr/bin/expect set timeout 2 set NAME [ lindex $argv 0] set AGE [ lindex $argv 1] set OBJ [ lindex $argv 2] set FEEL [ lindex $argv 3] spawn /mnt/ask.sh expect { name { send "$NAME\r";exp_continue }old { send "$AGE\r";exp_continue }study { send "$OBJ\r";exp_continue }happy { send "$FEEL\r" } } expect eof [root@desktop27 mnt]# /mnt/answer.exp tutu 19 linux happy spawn /mnt/ask.sh What's your name: tutu How old are you: 19 Which obj you study: linux Are you happy? happy tutu is 19's old and study linux feel happy [root@desktop27 mnt]# /mnt/answer.exp butterfly 19 linux bad spawn /mnt/ask.sh What's your name: butterfly How old are you: 19 Which obj you study: linux Are you happy? bad butterfly is 19's old and study linux feel bad [root@desktop27 mnt]#

寫成一個文件 .sh 的例子(和上面例子效果一樣)

[root@desktop27 mnt]# vim answer.exp [root@desktop27 mnt]# mv answer.exp answer.sh [root@desktop27 mnt]# cat answer.sh #!/bin/bash /usr/bin/expect <<EOF set timeout 2 spawn /mnt/ask.sh expect { name { send "$1\r";exp_continue }old { send "$2\r";exp_continue }study { send "$3\r";exp_continue }happy { send "$4\r" } } expect eof EOF [root@desktop27 mnt]# sh answer.sh tutu 18 linux happy spawn /mnt/ask.sh What's your name: tutu How old are you: 18 Which obj you study: linux Are you happy? happy tutu is 18's old and study linux feel happy [root@desktop27 mnt]#

六、exit、break、continue

exit n 腳本退出,退出值為 n
break 退出當前循環(huán)
continue 提前結束循環(huán)內部的命令,但不終止循環(huán)

[root@desktop27 mnt]# vim test.sh [root@desktop27 mnt]# cat test.sh #!/bin/bash for NUM in {1..5} dowhile [ "$NUM" -eq "4" ]do continue 4 doneecho $NUM done [root@desktop27 mnt]# sh test.sh 1 2 3 5 [root@desktop27 mnt]#

總結

以上是生活随笔為你收集整理的linux——shell 中常用的控制语句 for、while、if、case、expect、exit、break、continue的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。