shell處理用戶輸入傳遞參數(shù)的實(shí)現(xiàn)
向shell腳本傳遞數(shù)據(jù)的最基本方法是使用命令行參數(shù)。命令行參數(shù)允許運(yùn)行腳本時(shí)在命令行中添加數(shù)據(jù):
$ ./addem 10 30
本例向腳本addem傳遞了兩個(gè)命令行參數(shù)(10和30)。腳本會(huì)通過(guò)特殊的變量來(lái)處理命令行參數(shù)。接下來(lái)將介紹如何在bash shell腳本中使用命令行參數(shù)。
讀取參數(shù)
bash shell會(huì)將所有的命令行參數(shù)都指派給稱作位置參數(shù)(positional parameter)的特殊變量。這也包括shell腳本名稱。位置變量的名稱都是標(biāo)準(zhǔn)數(shù)字:$0對(duì)應(yīng)腳本名,$1對(duì)應(yīng)第一個(gè)命令行參數(shù),$2對(duì)應(yīng)第二個(gè)命令行參數(shù),以此類推,直到$9。
下面是在shell腳本中使用單個(gè)命令行參數(shù)的簡(jiǎn)單例子:
$ cat positional1.sh #!/bin/bash # Using one command-line parameter # factorial=1 for (( number = 1; number <= $1; number++ )) do factorial=$[ $factorial * $number ] done echo The factorial of $1 is $factorial exit $ $ ./positional1.sh 5 The factorial of 5 is 120
在shell腳本中,可以像使用其他變量一樣使用$1變量。shell腳本會(huì)自動(dòng)將命令行參數(shù)的值分配給位置變量,無(wú)須做任何特殊處理。
如果需要輸入更多的命令行參數(shù),則參數(shù)之間必須用空格分開(kāi)。shell會(huì)將其分配給對(duì)應(yīng)的位置變量:
$ cat positional2.sh #!/bin/bash # Using two command-line parameters # product=$[ $1 * $2 ] echo The first parameter is $1. echo The second parameter is $2. echo The product value is $product. exit $ $ ./positional2.sh 2 5 The first parameter is 2. The second parameter is 5. The product value is 10.
在上面的例子中,用到的命令行參數(shù)都是數(shù)值。你也可以在命令行中用文本字符串作為參數(shù):
$ cat stringparam.sh #!/bin/bash # Using one command-line string parameter # echo Hello $1, glad to meet you. exit $ $ ./stringparam.sh world Hello world, glad to meet you.
shell將作為命令行參數(shù)的字符串值傳給了腳本。但如果碰到含有空格的字符串,則會(huì)出現(xiàn)問(wèn)題:
$ ./stringparam.sh big world
Hello big, glad to meet you.
記住,參數(shù)之間是以空格分隔的所以shell會(huì)將字符串包含的空格視為兩個(gè)參數(shù)的分隔符。要想在參數(shù)值中加入空格,必須使用引號(hào)(單引號(hào)或雙引號(hào)均可)。
$ ./stringparam.sh 'big world' Hello big world, glad to meet you. $ $ ./stringparam.sh "big world" Hello big world, glad to meet you.
注意 將文本字符串作為參數(shù)傳遞時(shí),引號(hào)并不是數(shù)據(jù)的一部分,僅用于表明數(shù)據(jù)的起止位置。
如果腳本需要的命令行參數(shù)不止9個(gè),則仍可以繼續(xù)加入更多的參數(shù),但是需要稍微修改一下位置變量名。在 第9個(gè)位置變量之后,必須在變量名兩側(cè)加上花括號(hào),比如${10}。來(lái)看一個(gè)例子:
$ cat positional10.sh #!/bin/bash # Handling lots of command-line parameters # product=$[ ${10} * ${11} ] echo The tenth parameter is ${10}. echo The eleventh parameter is ${11}. echo The product value is $product. exit $ $ ./positional10.sh 1 2 3 4 5 6 7 8 9 10 11 12 The tenth parameter is 10. The eleventh parameter is 11. The product value is 110.
這樣你就可以根據(jù)需要向腳本中添加任意多的命令行參數(shù)了。
讀取腳本名
可以使用位置變量$0獲取在命令行中運(yùn)行的shell腳本名。這在編寫(xiě)包含多種功能或生成日志消息的工具時(shí)非常方便。
$ cat positional0.sh #!/bin/bash # Handling the $0 command-line parameter # echo This script name is $0. exit $ $ bash positional0.sh This script name is positional0.sh.
但這里有一個(gè)潛在的問(wèn)題。如果使用另一個(gè)命令來(lái)運(yùn)行shell腳本,則命令名會(huì)和腳本名混在一起,出現(xiàn)在位置變量$0中:
$ ./positional0.sh This script name is ./positional0.sh.
這還不是唯一的問(wèn)題。如果運(yùn)行腳本時(shí)使用的是絕對(duì)路徑,那么位置變量$0就會(huì)包含整個(gè)路徑:
$ $HOME/scripts/positional0.sh This script name is /home/christine/scripts/positional0.sh.
如果你編寫(xiě)的腳本中只打算使用腳本名,那就得做點(diǎn)兒額外工作,剝離腳本的運(yùn)行路徑。好在有個(gè)方便的小命令可以幫到我們。basename命令可以返回不包含路徑的腳本名:
$ cat posbasename.sh # Using basename with the $0 command-line parameter # name=$(basename $0) # echo This script name is $name. exit $ $ ./posbasename.sh This script name is posbasename.sh.
現(xiàn)在好多了。你可以使用此技術(shù)編寫(xiě)一個(gè)腳本,生成能標(biāo)識(shí)運(yùn)行時(shí)間的日志消息:
$ cat checksystem.sh #!/bin/bash # Using the $0 command-line parameter in messages # scriptname=$(basename $0) # echo The $scriptname ran at $(date) >> $HOME/scripttrack.log exit $ $ ./checksystem.sh $ cat $HOME/scripttrack.log The checksystem.sh ran at Wed 17 Jul 2024 19:00:53 AM EDT
擁有一個(gè)能識(shí)別自己的腳本對(duì)于追蹤腳本問(wèn)題、系統(tǒng)審計(jì)和生成日志信息是非常有用的。
參數(shù)測(cè)試
在shell腳本中使用命令行參數(shù)時(shí)要當(dāng)心。如果運(yùn)行腳本時(shí)沒(méi)有指定所需的參數(shù),則可能會(huì)出問(wèn)題:
$ ./positional1.sh ./positional1.sh: line 5: (( number <= : syntax error: operand expected (error token is "<= ") The factorial of is 1
當(dāng)腳本認(rèn)為位置變量中應(yīng)該有數(shù)據(jù),而實(shí)際上根本沒(méi)有的時(shí)候,腳本很可能會(huì)產(chǎn)生錯(cuò)誤消息。這種編寫(xiě)腳本的方法并不可取。在使用位置變量之前一定要檢查是否為空:
$ cat checkpositional1.sh #!/bin/bash # Using one command-line parameter # if [ -n "$1" ] then factorial=1 for (( number = 1; number <= $1; number++ )) do factorial=$[ $factorial * $number ] done echo The factorial of $1 is $factorial else echo "You did not provide a parameter." fi exit $ $ ./checkpositional1.sh You did not provide a parameter. $ $ ./checkpositional1.sh 3 The factorial of 3 is 6
上面這個(gè)例子使用了-n測(cè)試來(lái)檢查命令行參數(shù)$1中是否為空。
到此這篇關(guān)于shell處理用戶輸入傳遞參數(shù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)shell 用戶輸入傳遞參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Shell腳本導(dǎo)入導(dǎo)出數(shù)據(jù)的項(xiàng)目示例
在工作中,很多場(chǎng)景都會(huì)涉及到數(shù)據(jù)的導(dǎo)入導(dǎo)出,本文就介紹一下使用Shell腳本導(dǎo)入導(dǎo)出數(shù)據(jù)的項(xiàng)目示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04檢查L(zhǎng)inux系統(tǒng)中文件大小的方法總結(jié)
在Linux操作系統(tǒng)中,掌握如何高效檢查文件大小是每位開(kāi)發(fā)者和系統(tǒng)管理員的必備技能,本文詳細(xì)介紹了四種檢查L(zhǎng)inux文件大小的方法,感興趣的朋友可以參考下2024-03-03linux 驅(qū)動(dòng)之Kconfig文件和Makefile文件實(shí)例
這篇文章主要介紹了linux 驅(qū)動(dòng)之Kconfig文件和Makefile文件實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01如何解決 shell 腳本重復(fù)執(zhí)行的問(wèn)題
假如執(zhí)行備份腳本消耗的時(shí)間遠(yuǎn)大于設(shè)置的備份間隔的話,系統(tǒng)會(huì)出現(xiàn)多個(gè)同時(shí)在執(zhí)行腳本的Bash實(shí)例,會(huì)占用大量的系統(tǒng)資源,進(jìn)而影響正常業(yè)務(wù)程序的運(yùn)行,那如何解決上述shell腳本重復(fù)執(zhí)行的問(wèn)題呢,本文將要介紹的 flock 命令可以解決這個(gè)問(wèn)題2021-05-05Linux 中可重入函數(shù)與不可重入函數(shù)詳解
這篇文章主要介紹了Linux 中可重入函數(shù)與不可重入函數(shù)詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06fedora 23 lvm2格式 根目錄磁盤(pán)空間不足 擴(kuò)容方法
下面小編就為大家?guī)?lái)一篇fedora 23 lvm2格式 根目錄磁盤(pán)空間不足 擴(kuò)容方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02bash腳本如何監(jiān)控cup/內(nèi)存/磁盤(pán)使用率
本文主要介紹了bash腳本如何監(jiān)控cup/內(nèi)存/磁盤(pán)使用率,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10