日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

?shell的各種循環(huán)語句:for、while、until、select?

1、for循環(huán)

#語法結(jié)構(gòu)

#第一種:取值變量

for 變量名 in 變量取值表
do
    指令 
done

#例子:

#示例
for a in {1..9}
do
    mkdir dir$a
done
#說明:創(chuàng)建9個目錄

#第二種:C語言型for循環(huán)

for ((exp1; exp2; exp3))
do
    指令
done

#例子:

#示例
for ((i=1;i<=3;i++))
do
    echo $i
done
#解釋:i從1開始,當(dāng)i<=3就可以運行,如果運行的值大于3,就退出循環(huán)

#語法結(jié)構(gòu)講解
for關(guān)鍵字后的雙括號是三個表達式,
第一個是變量初始化(例如:i=1),第二個為變量的范圍(例如i<=3),第三個為變量自增或自減(例如i++)。
當(dāng)?shù)谝粋€表達式的初始化值符合第二個變量的范圍時,就進行如循環(huán)執(zhí)行,當(dāng)條件不滿足時就退出循環(huán)

#簡單示例

#1.豎向打印10 9 8 7 6 5幾個數(shù)字
#第一種方法:直接列出元素

[root@game scripts]# cat for1.sh 
#!/bin/bash

for i in 1 2 3 4 5
do
    echo $i
done

#效果
[root@game scripts]# sh for1.sh 
1
2
3
4
5

第二種方法:使用大括號{}生成數(shù)字序列

[root@game scripts]# cat for2.sh 
#!/bin/bash

for i in {1..5}
do
    echo $i
done

#效果
[root@game scripts]# sh for2.sh 
1
2
3
4
5

#第三種方法:使用seq生成數(shù)字序列

[root@game scripts]# cat for3.sh 
#!/bin/bash

for i in `seq 1 5`
do
    echo $i
done

#效果
[root@game scripts]# sh for3.sh 
1
2
3
4
5

#2.獲取當(dāng)前目錄下的目錄或文件名,并將其作為變量列表打印輸出

#數(shù)據(jù)
[root@game ~]# mkdir -p /test/{test1.txt,test2.txt,guo.txt,ke.txt}
[root@game ~]# ls -l /test/
total 0
drwxr-xr-x. 2 root root 6 Aug 21 22:14 guo.txt
drwxr-xr-x. 2 root root 6 Aug 21 22:14 ke.txt
drwxr-xr-x. 2 root root 6 Aug 21 22:14 test1.txt
drwxr-xr-x. 2 root root 6 Aug 21 22:14 test2.txt

#編寫腳本
[root@game scripts]# cat for4.sh 
#!/bin/bash
usage(){
    echo "directory not found"
}

[ ! -d /test ] && usage && exit 1
cd /test

for i in `ls`
do
    echo $i
done

效果
[root@game scripts]# sh for4.sh 
guo.txt
ke.txt
test1.txt
test2.txt

2、while循環(huán)

#while循環(huán)一般應(yīng)用于守護進程程序或一直循環(huán)執(zhí)行

#語法格式

while <條件表達式>
do
    指令
done

#簡單示例

每隔2秒在屏幕上輸出一次負載值
[root@game scripts]# cat while1.sh 
#!/bin/bash

while true
do
    uptime
    sleep 2 #暫停2秒再執(zhí)行
done
#提示:while true表示條件永遠為真,因此會一直運行,像死循環(huán)一樣,稱為守護進程

#效果:每隔2秒就輸出一次
[root@game scripts]# sh while1.sh 
 23:11:35 up 2 days,  2:00,  2 users,  load average: 0.00, 0.01, 0.05
 23:11:37 up 2 days,  2:00,  2 users,  load average: 0.00, 0.01, 0.05
 23:11:39 up 2 days,  2:00,  2 users,  load average: 0.00, 0.01, 0.05

3、until循環(huán)

#until循環(huán)是當(dāng)條件表達式不成立時,就會進入循環(huán),當(dāng)條件表達式成立時,就會終止循環(huán)

#語法格式

until <條件表達式>
do
    指令
done

#示例

#如果用戶輸出的是guoke就符合條件,退出循環(huán),如果不是,用戶輸入3次之后就退出循環(huán)
[root@game scripts]# cat until1.sh
#!/bin/bash

i=1
until [ "$user" = "guoke" -o "$i" -gt 3 ]
do
    read -p "please enter you username:" user
    let i++
done

#效果
[root@game scripts]# sh until1.sh 
please enter you username:guoke
[root@game scripts]# sh until1.sh 
please enter you username:1
please enter you username:1
please enter you username:1
[root@game scripts]#

4、select循環(huán)

#語法格式

select 變量名 in [菜單取值列表]
do
    指令
done

#示例

#第一種:直接使用列表字符串
[root@game scripts]# cat select1.sh 
#!/bin/bash

select name in Apache httpd Nginx Tomcat
do
    echo $name
done

#效果
[root@game scripts]# sh select1.sh 
1) apache
2) httpd
3) nginx
4) tomcat
#? 1
apache
#? 3
nginx
#? 4
tomcat
#? ^C


#第二種:采用數(shù)組做變量列表
[root@game scripts]# cat select2.sh 
#!/bin/bash
?
array=(aache nginx tomcat lighttpd)
select name in "${array[@]}"
do
    echo $name
done
#效果
[root@game scripts]# sh select2.sh 
1) apache
2) nginx
3) tomcat
4) lighttpd
#? 3
tomcat
#? 4
lighttpd
#? ^C

5.循環(huán)控制及狀態(tài)返回值

break (循環(huán)控制)
continue (循環(huán)控制)
exit (退出腳本)
return (退出函數(shù))

#區(qū)別

break continue在條件語句及循環(huán)語句(for if while等)中用于控制程序的走向
exit是終止所有語句并退出腳本
return:僅用于在函數(shù)內(nèi)部返回函數(shù)執(zhí)行的狀態(tài)值

#break示例

#如果i等于3,那么就終止循環(huán)
[root@game scripts]# cat break1.sh 
#!/bin/bash

for ((i=0;i<=5;i++))
do
    if [ $i -eq 3 ];then
    break
    else
    echo $i
    fi
done
echo "1111"
yum install net-tools -y > /dev/null
[ $? -eq 0 ] && echo "already install"


#效果
[root@game scripts]# sh break1.sh 
0
1
2
1111
already install
#說明:i等于3的時候就終止循環(huán),但是沒有跳出腳本

#exit示例

[root@game scripts]# cat exit1.sh
#!/bin/bash

for ((i=0;i<=5;i++))
do
    if [ $i -eq 3 ];then
    exit 1
    fi
    echo $i
done
echo "ok"

#執(zhí)行效果
[root@game scripts]# sh exit1.sh
0
1
2
#說明:當(dāng)i等于3的時候就會退出腳本了,就不會執(zhí)行后面的語句

#continue示例

[root@game scripts]# cat con1.sh 
#!/bin/bash

for ((i=0;i<=5;i++))
do
    if [ $i -eq 3 ];then
    continue
    else
    echo $i
    fi
done
echo "ok"

#執(zhí)行效果
[root@game scripts]# sh con1.sh 
0

#獲取更多學(xué)習(xí)資料,點擊了解更多,關(guān)注公眾號老油條IT記

分享到:
標(biāo)簽:腳本 shell
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定