基本傳參
基本應(yīng)用如下:
[test@localhost ~]$ cat var1_test.sh
#!/bin/bash
echo 參數(shù)0:$0
echo 參數(shù)1:$1
echo 參數(shù)2:$2
echo 參數(shù)3:$3
[test@localhost ~]$ sh var1_test.sh 1 2 3
參數(shù)0:var1_test.sh
參數(shù)1:1
參數(shù)2:2
參數(shù)3:3
[test@localhost ~]$
shell中參數(shù)處理相關(guān)說明:
參數(shù)處理 | 說明 |
---|---|
$# | 傳遞到腳本的參數(shù)個(gè)數(shù) |
$* | 以一個(gè)單字符串顯示所有向腳本傳遞的參數(shù) |
$$ | 腳本運(yùn)行的當(dāng)前進(jìn)程ID號(hào) |
$! | 后臺(tái)運(yùn)行 的組后一個(gè)進(jìn)程的ID號(hào) |
$@ | 與$*相同顿肺,但是使用時(shí)加引號(hào),并在引號(hào)中返回每個(gè)參數(shù),如"$@"用「"」括起來的情況竿开、以"$1" "$2" … "$n" 的形式輸出所有參數(shù) |
$- | 顯示shell使用的當(dāng)前選項(xiàng),與set命令功能相同 |
$? | 顯示最后命令的退出狀態(tài)玻熙。0表示沒有錯(cuò)誤否彩,其他任何值表明有錯(cuò)誤 |
$*與$@區(qū)別
- 相同點(diǎn):都是引用所有變量
- 不同點(diǎn):只有在雙引號(hào)中提現(xiàn)出來。假設(shè)在腳本運(yùn)行時(shí)寫了三個(gè)參數(shù)1嗦随、2列荔、3,則"*"等價(jià)于"1 2 3"(傳遞了一個(gè)參數(shù))枚尼,而"@"等價(jià)于"1" "2" "3"(傳遞了三個(gè)參數(shù))
[test@localhost ~]$ cat var2_test.sh
#!/bin/bash
echo "-------\$*----------"
for i in "$*"
do
echo $i
done
echo "--------\$@---------"
for i in "$@"
do
echo $i
done
[test@localhost ~]$ chmod +x var2_test.sh
[test@localhost ~]$ ./var2_test.sh 1 2 3 4
-------$*----------
1 2 3 4
--------$@---------
1
2
3
4
[test@localhost ~]$
默認(rèn)參數(shù)(變量默認(rèn)值)
-變量為null
取默認(rèn)值
- 變量為null時(shí)贴浙,輸出默認(rèn)值
${var-defaultval}
[test@localhost ~]$ unset var
[test@localhost ~]$ echo ${var}
[test@localhost ~]$ echo ${var-test}
test
[test@localhost ~]$ echo ${var}
[test@localhost ~]$ var=""
[test@localhost ~]$ echo ${var-test}
[test@localhost ~]$ echo ${var}
[test@localhost ~]$
=變量為null時(shí),同時(shí)改變變量值
[test@localhost ~]$ unset var1
[test@localhost ~]$ echo ${var1}
[test@localhost ~]$ echo ${var1=test}
test
[test@localhost ~]$ echo ${var1}
test
[test@localhost ~]$ var1=""
[test@localhost ~]$ echo ${var1}
[test@localhost ~]$ echo ${var1=test}
[test@localhost ~]$ echo ${var1}
[test@localhost ~]$
:-變量為null或空字符串署恍,取默認(rèn)值
${var:-defaultval}
[test@localhost ~]$ unset var2
[test@localhost ~]$ echo ${var2}
[test@localhost ~]$ echo ${var2:-test} #驗(yàn)證變量為null
test
[test@localhost ~]$ echo ${var2}
[test@localhost ~]$ var2=""
[test@localhost ~]$ echo ${var2}
[test@localhost ~]$ echo ${var2:-test} #驗(yàn)證變量為空字符串
test
[test@localhost ~]$ echo ${var2}
[test@localhost ~]$
:=變量為null或空字符串崎溃,同時(shí)改變變量值
${var:=defaultval}
[test@localhost ~]$ unset var3
[test@localhost ~]$ echo ${var3}
[test@localhost ~]$ echo ${var3:=test} #驗(yàn)證變量為null
test
[test@localhost ~]$ echo ${var3}
test
[test@localhost ~]$ var3=""
[test@localhost ~]$ echo ${var3}
[test@localhost ~]$ echo ${var3:=test} #驗(yàn)證變量為空字符串
test
[test@localhost ~]$ echo ${var3}
test
[test@localhost ~]$
:?變量為null或空字符串報(bào)錯(cuò)并退出
[test@localhost ~]$ unset var4
[test@localhost ~]$ echo ${var4}
[test@localhost ~]$ echo ${var4:?error}
-bash: var4: error
[test@localhost ~]$ echo ${var4}
[test@localhost ~]$ var4=""
[test@localhost ~]$ echo ${var4}
[test@localhost ~]$ echo ${var4:?error}
-bash: var4: error
[test@localhost ~]$ echo ${var4}
[test@localhost ~]$
:+變量不為空時(shí)使用默認(rèn)值
與:-相反
[test@localhost ~]$ var5="test"
[test@localhost ~]$ echo ${var5:+defaultest}
defaultest
[test@localhost ~]$ echo ${var5}
test
[test@localhost ~]$
參考文獻(xiàn):https://www.linuxidc.com/Linux/2018-11/155618.htm