shell腳本的流程控制語(yǔ)句的實(shí)現(xiàn)
一、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)文章
詳解Linux Shell 實(shí)現(xiàn)一個(gè)獲取任意位數(shù)的隨機(jī)密碼的腳本
這篇文章主要介紹了詳解Linux Shell 實(shí)現(xiàn)一個(gè)獲取任意位數(shù)的隨機(jī)密碼的腳本的相關(guān)資料,本文提供實(shí)現(xiàn)方法及實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08shell腳本測(cè)試某網(wǎng)段內(nèi)主機(jī)連通性
這篇文章主要介紹了shell腳本測(cè)試某網(wǎng)段內(nèi)主機(jī)連通性,需要的朋友可以參考下2017-10-10Linux中文件權(quán)限目錄權(quán)限的意義及權(quán)限對(duì)文件目錄的意義
本文給大家介紹Linux中文件目錄權(quán)限的意義及Linux的權(quán)限對(duì)于文件與目錄的意義,涉及到linux 文件、目錄、權(quán)限相關(guān)知識(shí),對(duì)本文感興趣的朋友一起學(xué)習(xí)吧2016-01-01shell腳本中執(zhí)行時(shí)提示“沒(méi)有那個(gè)文件或目錄”的解決辦法
故障現(xiàn)象:在終端直接cd /var正常,在shell腳本中執(zhí)行則報(bào)錯(cuò)。原因是腳本是在windows平臺(tái)下寫(xiě)的,換行符與linux不同,造成腳本不能正確執(zhí)行2014-04-04深入理解Linux shell中2>&1的含義(全網(wǎng)最全,看完就懂)
這篇文章主要介紹了深入理解Linux shell中2>&1的含義,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09shell中的數(shù)組操作小結(jié)和冒泡排序?qū)崿F(xiàn)腳本分享
這篇文章主要介紹了shell中的數(shù)組操作小結(jié)和冒泡排序?qū)崿F(xiàn)腳本分享,需要的朋友可以參考下2014-12-12