Shell腳本位置參數(shù)的具體使用
1.訪問命令行
Shell提供了一組名為位置參數(shù)的變了,其中包含了命令行上的各個(gè)單詞,這些變量按照0-9分別命名,
[sysadmin@ansible bin]$ cat posit-param.sh #!/bin/bash echo " \$0 = $0 \$1 = $1 \$2 = $2 \$3 = $3 \$4 = $4 \$5 = $5 \$6 = $6 \$7 = $7 \$8 = $8 \$9 = $9 " [sysadmin@ansible bin]$ posit-param.sh $0 = /home/sysadmin/bin/posit-param.sh $1 = $2 = $3 = $4 = $5 = $6 = $7 = $8 = $9 =
就算沒有提供參數(shù)值,$0始終出現(xiàn)在命令行中的第一項(xiàng),表示執(zhí)行程序的路徑。如果提供了參數(shù)值,會(huì)看到下列執(zhí)行結(jié)果:
[sysadmin@ansible bin]$ posit-param.sh a b c d
$0 = /home/sysadmin/bin/posit-param.sh
$1 = a
$2 = b
$3 = c
$4 = d
$5 =
$6 =
$7 =
$8 =
$9 =
能通過參數(shù)擴(kuò)展訪問的位置參數(shù)不止9個(gè),要想指定第9個(gè)之后的參數(shù),將數(shù)字放入花括號(hào)中即可。即${10}、${211}等
2 確定參數(shù)個(gè)數(shù)
Shell還提供了變量$#,其中包含了命令行中的參數(shù)個(gè)數(shù)
[sysadmin@ansible bin]$ cat posit-param.sh #!/bin/bash echo " Number of arguments: $# \$0 = $0 \$1 = $1 \$2 = $2 \$3 = $3 \$4 = $4 \$5 = $5 \$6 = $6 \$7 = $7 \$8 = $8 \$9 = $9 " [sysadmin@ansible bin]$ posit-param.sh a b c d Number of arguments: 4 $0 = /home/sysadmin/bin/posit-param.sh $1 = a $2 = b $3 = c $4 = d $5 = $6 = $7 = $8 = $9 =
3 shift-訪問多個(gè)參數(shù)
每執(zhí)行一次shift命令,就將所有的參數(shù)“左移一個(gè)位置”。實(shí)際上,通過shift命令,我們可以從始至終只和一個(gè)參數(shù)打交道(除了$0):
[sysadmin@ansible bin]$ cat posit-param2.sh
#!/bin/bash
count=1
while [[ $# -gt 0 ]]; do
echo "Argument $count = $1"
count=$((count + 1))
shift
done
[sysadmin@ansible bin]$ posit-param2.sh a b c d
Argument 1 = a
Argument 2 = b
Argument 3 = c
Argument 4 = d
每次執(zhí)行shift,$2的值就會(huì)移入$1,然后$3的值移入$2,依次類推。與此同時(shí),$#的值也會(huì)相應(yīng)減一。
4 簡(jiǎn)單應(yīng)用
[sysadmin@ansible bin]$ cat file-info #!/bin/bash #file-info PROGNAME="$(basename "$0")" if [[ -e "$1" ]]; then ? ? ? ? echo -e "\nFile Type:" ? ? ? ? file "$1" ? ? ? ? echo -e "\nFile Status:" ? ? ? ? stat "$1" else ? ? ? ? echo "$PROGNAME: usage: $PROGNAME file" >&2 ? ? ? ? exit 1 fi
5 在Shell函數(shù)中使用位置參數(shù)
位置參數(shù)既可以向Shell腳本傳遞參數(shù),也可以向Shell函數(shù)傳遞參數(shù)。作為演示,我們將file_info腳本改寫成Shell函數(shù):
[sysadmin@ansible bin]$ cat file-info
#!/bin/bash
#file-info
file_info () {
? ? ? ? if [[ -e "$1" ]]; then
? ? ? ? echo -e "\nFile Type:"
? ? ? ? ? ? file "$1"
? ? ? ? ? ? ? ? echo -e "\nFile Status:"
? ? ? ? ? ? ? ? stat "$1"
? ? ? ? else
? ? ? ? ? ? ? ? echo "$FUNCNAME: usage: $FUNCNAME file" >&2
? ? ? ? ? ? ? ? return 1
? ? ? ? fi
}
file_info "$1"6 批量處理位置參數(shù)
有時(shí)候批量處理所有位置參數(shù)更為實(shí)用,Shell為此提供了兩個(gè)特殊參數(shù)*和@,兩者均可擴(kuò)展成完整的位置參數(shù)列表,但其區(qū)別有些微妙。
| 參數(shù) | 描述 |
|---|---|
| $* | 擴(kuò)展成從1開始的位置參數(shù)列表。如果它出現(xiàn)在雙引號(hào)內(nèi)部,則擴(kuò)展成由雙引號(hào)引用的字符串,其中包含了所有的位置參數(shù),彼此之間以Shell變量IFS的第一個(gè)字符分割(默認(rèn)是空格符) |
| $@ | 擴(kuò)展成從1開始的位置參數(shù)列表,如果它出現(xiàn)在雙引號(hào)內(nèi)部,則將每個(gè)位置參數(shù)擴(kuò)展成獨(dú)立的單詞 |
[sysadmin@ansible bin]$ cat posit-params3
#!/bin/bash
# posit-params3
print_params () {
? ? ? ? echo "\$1 = $1"
? ? ? ? echo "\$2 = $2"
? ? ? ? echo "\$3 = $3"
? ? ? ? echo "\$4 = $4"
}
pass_params () {
? ? ? ? echo -e "\n" '$* :';print_params $*
? ? ? ? echo -e "\n" '"$*" :';print_params "$*"
? ? ? ? echo -e "\n" '$@ :';print_params $@
? ? ? ? echo -e "\n" '"$@" :';print_params "$@"
}
pass_params "word" "words with spaces"
[sysadmin@ansible bin]$ posit-params3
?$* :
$1 = word
$2 = words
$3 = with
$4 = spaces
?"$*" :
$1 = word words with spaces
$2 =
$3 =
$4 =
?$@ :
$1 = word
$2 = words
$3 = with
$4 = spaces
?"$@" :
$1 = word
$2 = words with spaces
$3 =
$4 =到目前為止,“$@”適用于大部分情況,因?yàn)槠浔A袅嗣總€(gè)位置參數(shù)的整體性。為了保證安全性,應(yīng)該堅(jiān)持使用這種方法。
到此這篇關(guān)于Shell腳本位置參數(shù)的具體使用的文章就介紹到這了,更多相關(guān)Shell腳本位置參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
shell基礎(chǔ)學(xué)習(xí)中的字符串操作、for循環(huán)語句示例
這篇文章主要介紹了shell基礎(chǔ)學(xué)習(xí)中的字符串操作、for循環(huán)語句示例2014-04-04
解決linux?shell中傳遞包含空格的參數(shù)問題
這篇文章主要介紹了如何解決linux?shell中傳遞包含空格的參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Linux shell查找文件顯示行號(hào)和對(duì)應(yīng)區(qū)間的內(nèi)容
今天小編就為大家分享一篇關(guān)于Linux shell查找文件顯示行號(hào)和對(duì)應(yīng)區(qū)間的內(nèi)容,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
使用Linux shell腳本實(shí)現(xiàn)FTP定時(shí)執(zhí)行批量下載指定文件
使用FTP定時(shí)批量下載指定文件的shell腳本,具體實(shí)例介紹如下所示,需要的朋友參考下吧2017-04-04
Shell腳本實(shí)現(xiàn)監(jiān)視指定進(jìn)程的運(yùn)行狀態(tài)
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)監(jiān)視指定進(jìn)程的運(yùn)行狀態(tài),本文直接給出腳本代碼,需要的朋友可以參考下2015-07-07

