shell腳本循環,linux 如何跳出循環函數,(三)Linux Shell編程——Shell常用命令(輸出、判斷、循環、函數、包含)(示例代碼)...

 2023-11-18 阅读 27 评论 0

摘要:3. 常用命令3.1 輸出shell腳本循環。3.1.1 echo命令echo是Shell的一個內部指令,用于在屏幕上打印出指定的字符串。命令格式:echo argshell 退出循環,name="coding"echo ‘$name"‘+"${name}" #原樣輸出 $name"+ codingech

3. 常用命令

3.1 輸出

shell腳本循環。3.1.1 echo命令

echo是Shell的一個內部指令,用于在屏幕上打印出指定的字符串。命令格式:

echo arg

shell 退出循環,name="coding"

echo ‘$name"‘+"${name}" #原樣輸出 $name"+ coding

echo `date` #當前日期

linux內核函數、3.1.2 printf命令

printf 命令用于格式化輸出, 是echo命令的增強版。它是C語言printf()庫函數的一個有限的變形,并且在語法上有些不同。

printf format-string [arguments...] #format-string 為格式控制字符串,arguments 為參數列表

ioctl函數?printf "Hello, Shell" #printf 不像 echo那樣會自動換行,必須顯式添加換行符(

)

printf"%d %s" 1 "abc" #輸出 1 abc

3.2 if else語句

if 語句通過關系運算符判斷表達式的真假來決定執行哪個分支。Shell 有三種 if ... else 語句:

if ... fi 語句;

if ... else ... fi 語句;

if ... elif ... else ... fi 語句。

3.2.1 if ... fi語句

if[ expression ]thenStatement(s) to be executedif expression is true

fi

注意:expression 和方括號([ ])之間必須有空格,否則會有語法錯誤。

3.2.2?if ... else ... fi 語句

if[ expression ]thenStatement(s) to be executedif expression is true

elseStatement(s) to be executedif expression is not true

fi

a=10b=20

if [ $a ==$b ]then

echo "a is equal to b"

else

echo "a is not equal to b"

fi

if ... else 語句也可以寫成一行,以命令的方式來運行,像這樣:

if test $[2*3] -eq $[1+5]; then echo ‘The two numbers are equal!‘; fi;

if ... else 語句也經常與 test 命令結合使用,test 命令用于檢查某個條件是否成立,與方括號([ ])類似。

a=10b=20

if [ ${a} ==${b} ]#if test $[a] -eq $[b] #數值類型比較 $[num]

3.2.3?if ... elif ... fi 語句

if ... elif ... fi 語句可以對多個條件進行判斷,語法為:

if [ expression 1]thenStatement(s) to be executedif expression 1 is true

elif [ expression 2]thenStatement(s) to be executedif expression 2 is true

elif [ expression 3]thenStatement(s) to be executedif expression 3 is true

elseStatement(s) to be executedif no expression is true

fi

哪一個?expression 的值為 true,就執行哪個 expression 后面的語句;如果都為 false,那么不執行任何語句。

a=10b=20

if [ $a ==$b ]then

echo "a is equal to b"

elif [ $a -gt $b ]then

echo "a is greater than b"

elif [ $a -lt $b ]then

echo "a is less than b"

else

echo "None of the condition met"

fi

3.3 test命令

test 命令用于檢查某個條件是否成立,它可以進行數值、字符和文件三個方面的測試。

3.3.1 數值比較

20180618163329222947.png

語法:

if test $[num1] -eq $[num2]

3.3.2 字符串比較

20180618163330088214.png

語法:

if test str1=str2

3.3.3 文件比較

20180618163330788436.png

語法:

if test -e ./bash

另外,Shell還提供了與( ! )、或( -o )、非( -a )三個邏輯操作符用于將測試條件連接起來,其優先級為:“!”最高,“-a”次之,“-o”最低。

3.4?case ... esac語句

case ... esac 與其他語言中的 switch ... case 語句類似,是一種多分枝選擇結構。

語法:

case 值 in模式1)

command1

command2

command3

;;

模式2)

command1

command2

command3

;;*)

command1

command2

command3

;;esac

case工作方式如上所示。取值后面必須為關鍵字 in,每一模式必須以右括號結束。取值可以為變量或常數。匹配發現取值符合某一模式后,其間所有命令開始執行直至 ;;。;; 與其他語言中的 break 類似,意思是跳到整個 case 語句的最后。

取值將檢測匹配的每一個模式。一旦模式匹配,則執行完匹配模式相應命令后不再繼續其他模式。如果無一匹配模式,使用星號 * 捕獲該值,再執行后面的命令。

echo ‘Input a number between 1 to 4‘

echo ‘Your number is:c‘read aNumcase $aNum in

1) echo ‘You select 1‘;;2) echo ‘You select 2‘;;3) echo ‘You select 3‘;;4) echo ‘You select 4‘;;*) echo ‘You do not select a number between 1 to 4‘;;esac

3.5 循環

3.5.1 for循環

語法:

for 變量 in列表docommand1

command2

...

commandNdone

列表是一組值(數字、字符串等)組成的序列,每個值通過空格分隔。每循環一次,就將列表中的下一個值賦給變量。

in 列表是可選的,如果不用它,for 循環使用命令行的位置參數。

for loop in 1 2 3 4 5#for str in ‘I love Spring‘

do

echo "The value is: $loop"#echo${str}done

3.5.2 while循環

while循環用于不斷執行一系列命令,也用于從輸入文件中讀取數據;命令通常為測試條件。

語法:

whilecommanddoStatement(s) to be executedif command is true

done

COUNTER=0

while [ $COUNTER -lt 5]doCOUNTER=‘expr $COUNTER+1‘

echo$COUNTERdone

3.5.3 util循環

until 循環執行一系列命令直至條件為 true 時停止。until 循環與 while 循環在處理方式上剛好相反。一般while循環優于until循環,但在某些時候,也只是極少數情況下,until 循環更加有用。

語法:

untilcommanddoStatement(s) to be executeduntil command is true

done

a=0

until [ ! $a -lt 10]do

echo $a #輸出1~9a=`expr $a + 1`done

3.5.4 break和continue命令

break命令允許跳出所有循環(終止執行后面的所有循環);continue命令會跳出當前循環。

在嵌套循環中,這兩個命令還有較高級的用法:

break 2#跳出2層循環

continue2

3.6 Shell函數

3.6.1 函數定義

函數可以讓我們將一個復雜功能劃分成若干模塊,讓程序結構更加清晰,代碼重復利用率更高。像其他編程語言一樣,Shell 也支持函數。Shell 函數必須先定義后使用。

函數的定義語法如下:

[ function] function_name () {

list of commands

[ return value ]

}

函數名前可加上關鍵字 function,也可不加,效果一樣。

函數返回值,可以顯式增加return語句;如果不加,會將最后一條命令運行結果作為返回值。

funWithReturn(){echo "The function is to get the sum of two numbers..."

echo -n "Input first number:"read aNumecho -n "Input another number:"read anotherNumecho "The two numbers are $aNum and $anotherNum !"return $(($aNum+$anotherNum))

}

funWithReturn

# Capture value returnd bylastcommand

ret=$?

echo "The sum of two numbers is $ret !"

結果:

[[email?protected] testShell]# ./myShell.shThefunction is to get the sumof two numbers...

Input first number:4Input another number:5The two numbers are4 and 5 !Thesum of two numbers is 9 !

像刪除變量一樣,刪除函數也可以使用 unset 命令,不過要加上 .f 選項

unset .f funWithReturn

3.6.2 函數參數

在Shell中,調用函數時可以向其傳遞參數。在函數體內部,通過 $n 的形式來獲取參數的值,例如,$1表示第一個參數,$2表示第二個參數...

注意,$10 不能獲取第十個參數,獲取第十個參數需要${10}。當n>=10時,需要使用${n}來獲取參數。

funWithParam(){echo "The value of the first parameter is $1 !"

echo "The value of the second parameter is $2 !"

echo "The value of the tenth parameter is $10 !"

echo "The value of the tenth parameter is ${10} !"

echo "The value of the eleventh parameter is ${11} !"

echo "The amount of the parameters is $# !"# 參數個數echo "The string of the parameters is $* !"# 傳遞給函數的所有參數

}

funWithParam1 2 3 4 5 6 7 8 9 18 77

輸出

[[email?protected] testShell]# ./myShell.shThe value of the first parameter is1 !The value of the second parameter is2 !The value of the tenth parameter is10 !The value of the tenth parameter is18 !The value of the eleventh parameter is77 !The amount of the parameters is11 !Thestring of the parameters is 1 2 3 4 5 6 7 8 9 18 77 !

3.7 Shell文件包含

像其他語言一樣,Shell 也可以包含外部腳本,將外部腳本的內容合并到當前腳本。

兩種語法:

. filename

source filename

創建被調用腳本 test.sh

name="Java Web"

使用主文件 myShell.sh來引用該腳本

. ./test.sh

echo ${name} #輸出Java Web

需要注意的是,被包含腳本(test.sh)不需要有執行權限。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/179178.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息