欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

shell腳本的流程控制語(yǔ)句的實(shí)現(xiàn)

 更新時(shí)間:2022年08月22日 08:10:07   作者:lambda-小張  
本文主要介紹了shell腳本的流程控制語(yǔ)句的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、if 判斷

1)基本語(yǔ)法

(1)單分支

if [ 條件判斷式 ];then
程序
fi

或者

if [ 條件判斷式 ]
then
程序
fi

 大于18,命令行輸出OK

[root@hadoop ~]# a=25
[root@hadoop ~]# if [ $a -gt 18 ];then echo OK; fi
OK
[root@hadoop ~]# a=15
[root@hadoop ~]# if [ $a -gt 18 ]; then echo OK; fi

或者

[root@hadoop scripts]# a=25
[root@hadoop scripts]# if [ $a -gt 18 ] && [ $a -lt 35 ];then echo OK;fi
OK

 判斷是否張三,是輸出張三

[root@hadoop scripts]# vim if_test.sh
#!/bin/bash
if [ "$1"x = "zhangsan"x ]    #等號(hào)左右加x,避免輸入空值報(bào)錯(cuò)
then
    echo "zhangsan"
fi
[root@hadoop scripts]# . if_test.sh zhangsan
zhangsan
[root@hadoop scripts]# . if_test.sh  
[root@hadoop scripts]# . if_test.sh lisi

邏輯與-a,邏輯或-o

[root@hadoop scripts]# a=36
[root@hadoop scripts]# if [ $a -gt 18 ] && [ $a -lt 35 ]; then echo OK; fi
[root@hadoop scripts]# if [ $a -gt 18  &&  $a -lt 35 ]; then echo OK; fi
-bash: [: missing `]'    #不能直接用
[root@hadoop scripts]# if [ $a -gt 18  -a  $a -lt 35 ]; then echo OK; fi
[root@hadoop scripts]# a=20
[root@hadoop scripts]# if [ $a -gt 18  -a  $a -lt 35 ]; then echo OK; fi
OK

(2)多分支

if [ 條件判斷式 ]
then
程序
elif [ 條件判斷式 ]
then
程序
else
程序
fi

 判斷屬于哪個(gè)年齡段,雙分支

[root@hadoop scripts]# vim if_test.sh
#!/bin/bash
if [ "$1"x = "zhangsan"x ]
then
    echo "zhangsan"
fi
#輸入第二個(gè)參數(shù),表示年齡,判斷屬于哪個(gè)年齡段
if [ $2 -lt 18 ]
then
    echo "未成年人"
else
    echo "成年人"
fi
[root@hadoop scripts]# . if_test.sh zhangsan 15
zhangsan
未成年人
[root@hadoop scripts]# . if_test.sh zhangsan 25
zhangsan
成年人

判斷屬于哪個(gè)年齡段,多分支

注意事項(xiàng):

①[ 條件判斷式 ],中括號(hào)和條件判斷式之間必須有空格

②if 后要有空格 

二、case 語(yǔ)句

1)基本語(yǔ)法

case $變量名 in

"值 1")

如果變量的值等于值 1,則執(zhí)行程序 1

;;

"值 2")

如果變量的值等于值 2,則執(zhí)行程序 2

;;

…省略其他分支…

*) 如果變量的值都不是以上的值,則執(zhí)行此程序

;;

esac

輸入數(shù)字,輸出對(duì)應(yīng)語(yǔ)句

[root@hadoop ~]# vim case_test.sh
#!/bin/bash
case $1 in
1)
?? ?echo "one"
;;
2)
?? ?echo "two"
;;
3)
?? ?echo "three"
;;
*)
?? ?echo "number else"
;;
esac
[root@hadoop ~]# chmod +x case_test.sh
[root@hadoop ~]# . case_test.sh
number else
[root@hadoop ~]# . case_test.sh 2
two
[root@hadoop ~]# . case_test.sh 1
one
[root@hadoop ~]# . case_test.sh 6
number else

注意事項(xiàng):

(1)case 行尾必須為單詞“in”,每一個(gè)模式匹配必須以右括號(hào)“)”結(jié)束。

(2)雙分號(hào)“;;”表示命令序列結(jié)束,相當(dāng)于 java 中的 break。

(3)最后的“*)”表示默認(rèn)模式,相當(dāng)于 java 中的 default。 

三、for 循環(huán)

1)基本語(yǔ)法1

for (( 初始值;循環(huán)控制條件;變量變化 ))

do

        程序

done

 2)案例實(shí)操

從 1 加到 100,注意在for循環(huán)里面可以直接用數(shù)學(xué)運(yùn)算符

[root@hadoop ~]# vim sum_to.sh
[root@hadoop ~]# cat sum_to.sh
#!/bin/bash
 
sum=0
for (( i=1; i <= $1; i++ ))
do
    sum=$[ $sum + $i ]
done
echo $sum
[root@hadoop ~]# chmod +x sum_to.sh
[root@hadoop ~]# . sum_to.sh 100
5050

 3)基本語(yǔ)法 2

for 變量 in 值 1 值 2 值 3…

do

        程序

done

[root@hadoop ~]# for i in {1..100}; do sum=$[$sum+$i]; done; echo $sum
5050

(4)比較$*和$@區(qū)別

$*和$@都表示傳遞給函數(shù)或腳本的所有參數(shù),不被雙引號(hào)“”包含時(shí),都以$1 $2 …$n 的形式輸出所有參數(shù)。 

沒(méi)加雙引號(hào),兩種輸出結(jié)果沒(méi)有區(qū)別

[root@hadoop scripts]# vim parameter_for_test.sh
#!/bin/bash
 
echo '============$*================'
for para in $*
do
    echo $para
done
 
echo '============$@================'
for para in $@
do
        echo $para
done
 
[root@hadoop scripts]# chmod +x parameter_for_test.sh 
[root@hadoop scripts]# . parameter_for_test.sh a b c d e
============$*================
a
b
c
d
e
============$@================
a
b
c
d
e

加雙引號(hào),兩種輸出結(jié)區(qū)別,一種有換行,一種沒(méi)有換行。

當(dāng)它們被雙引號(hào)“”包含時(shí),$*會(huì)將所有的參數(shù)作為一個(gè)整體,以“$1 $2 …$n”的形式輸 出所有參數(shù);$@會(huì)將各個(gè)參數(shù)分開(kāi),以“$1” “$2”…“$n”的形式輸出所有參數(shù)。

[root@hadoop scripts]# vim parameter_for_test.sh
#!/bin/bash
 
echo '============$*================'
for para in "$*"
do
    echo $para
done
 
echo '============$@================'
for para in "$@"
do
        echo $para
done
 
[root@hadoop scripts]# . parameter_for_test.sh a b c d e
============$*================
a b c d e
============$@================
a
b
c
d
e

四、while循環(huán)

1)基本語(yǔ)法

while [ 條件判斷式 ]

do

        程序

done

2)案例實(shí)操

從1加到100

[root@hadoop scripts]# vim sum_to.sh 
#!/bin/bash
 
#用for進(jìn)行實(shí)現(xiàn)
sum=0
for (( i=1; i <= $1; i++ ))
do
    sum=$[ $sum + $i ]
done
echo $sum
 
#用while做一個(gè)實(shí)現(xiàn)
a=1
while [ $a -le $1 ]
do
    sum2=$[ $sum2 + $a ]
    a=$[ $a + 1 ]
done
echo $sum2 
[root@hadoop scripts]# . sum_to.sh 100
5050
5050

更簡(jiǎn)單語(yǔ)法用shell內(nèi)嵌命令let

[root@hadoop scripts]# vim sum_to.sh 
#用while做一個(gè)實(shí)現(xiàn)
sum2=0
a=1
while [ $a -le $1 ]
do
#    sum2=$[ $sum2 + $a ]
#    a=$[ $a + 1 ]
    let sum2+=a
    let a++
done
echo $sum2 
[root@hadoop scripts]# . sum_to.sh 100
5050
5050

到此這篇關(guān)于shell腳本的流程控制語(yǔ)句的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)shell 流程控制語(yǔ)句 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論