shell處理用戶輸入傳遞參數(shù)的實現(xiàn)
向shell腳本傳遞數(shù)據(jù)的最基本方法是使用命令行參數(shù)。命令行參數(shù)允許運行腳本時在命令行中添加數(shù)據(jù):
$ ./addem 10 30
本例向腳本addem傳遞了兩個命令行參數(shù)(10和30)。腳本會通過特殊的變量來處理命令行參數(shù)。接下來將介紹如何在bash shell腳本中使用命令行參數(shù)。
讀取參數(shù)
bash shell會將所有的命令行參數(shù)都指派給稱作位置參數(shù)(positional parameter)的特殊變量。這也包括shell腳本名稱。位置變量的名稱都是標(biāo)準(zhǔn)數(shù)字:$0對應(yīng)腳本名,$1對應(yīng)第一個命令行參數(shù),$2對應(yīng)第二個命令行參數(shù),以此類推,直到$9。
下面是在shell腳本中使用單個命令行參數(shù)的簡單例子:
$ 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腳本會自動將命令行參數(shù)的值分配給位置變量,無須做任何特殊處理。
如果需要輸入更多的命令行參數(shù),則參數(shù)之間必須用空格分開。shell會將其分配給對應(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ù)的字符串值傳給了腳本。但如果碰到含有空格的字符串,則會出現(xiàn)問題:
$ ./stringparam.sh big world
Hello big, glad to meet you.
記住,參數(shù)之間是以空格分隔的所以shell會將字符串包含的空格視為兩個參數(shù)的分隔符。要想在參數(shù)值中加入空格,必須使用引號(單引號或雙引號均可)。
$ ./stringparam.sh 'big world' Hello big world, glad to meet you. $ $ ./stringparam.sh "big world" Hello big world, glad to meet you.
注意 將文本字符串作為參數(shù)傳遞時,引號并不是數(shù)據(jù)的一部分,僅用于表明數(shù)據(jù)的起止位置。
如果腳本需要的命令行參數(shù)不止9個,則仍可以繼續(xù)加入更多的參數(shù),但是需要稍微修改一下位置變量名。在 第9個位置變量之后,必須在變量名兩側(cè)加上花括號,比如${10}。來看一個例子:
$ 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獲取在命令行中運行的shell腳本名。這在編寫包含多種功能或生成日志消息的工具時非常方便。
$ 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.
但這里有一個潛在的問題。如果使用另一個命令來運行shell腳本,則命令名會和腳本名混在一起,出現(xiàn)在位置變量$0中:
$ ./positional0.sh This script name is ./positional0.sh.
這還不是唯一的問題。如果運行腳本時使用的是絕對路徑,那么位置變量$0就會包含整個路徑:
$ $HOME/scripts/positional0.sh This script name is /home/christine/scripts/positional0.sh.
如果你編寫的腳本中只打算使用腳本名,那就得做點兒額外工作,剝離腳本的運行路徑。好在有個方便的小命令可以幫到我們。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ù)編寫一個腳本,生成能標(biāo)識運行時間的日志消息:
$ 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
擁有一個能識別自己的腳本對于追蹤腳本問題、系統(tǒng)審計和生成日志信息是非常有用的。
參數(shù)測試
在shell腳本中使用命令行參數(shù)時要當(dāng)心。如果運行腳本時沒有指定所需的參數(shù),則可能會出問題:
$ ./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ù),而實際上根本沒有的時候,腳本很可能會產(chǎn)生錯誤消息。這種編寫腳本的方法并不可取。在使用位置變量之前一定要檢查是否為空:
$ 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
上面這個例子使用了-n測試來檢查命令行參數(shù)$1中是否為空。
到此這篇關(guān)于shell處理用戶輸入傳遞參數(shù)的實現(xiàn)的文章就介紹到這了,更多相關(guān)shell 用戶輸入傳遞參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Shell腳本導(dǎo)入導(dǎo)出數(shù)據(jù)的項目示例
在工作中,很多場景都會涉及到數(shù)據(jù)的導(dǎo)入導(dǎo)出,本文就介紹一下使用Shell腳本導(dǎo)入導(dǎo)出數(shù)據(jù)的項目示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04檢查Linux系統(tǒng)中文件大小的方法總結(jié)
在Linux操作系統(tǒng)中,掌握如何高效檢查文件大小是每位開發(fā)者和系統(tǒng)管理員的必備技能,本文詳細(xì)介紹了四種檢查Linux文件大小的方法,感興趣的朋友可以參考下2024-03-03linux 驅(qū)動之Kconfig文件和Makefile文件實例
這篇文章主要介紹了linux 驅(qū)動之Kconfig文件和Makefile文件實例的相關(guān)資料,需要的朋友可以參考下2017-01-01Linux 中可重入函數(shù)與不可重入函數(shù)詳解
這篇文章主要介紹了Linux 中可重入函數(shù)與不可重入函數(shù)詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06fedora 23 lvm2格式 根目錄磁盤空間不足 擴容方法
下面小編就為大家?guī)硪黄猣edora 23 lvm2格式 根目錄磁盤空間不足 擴容方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02bash腳本如何監(jiān)控cup/內(nèi)存/磁盤使用率
本文主要介紹了bash腳本如何監(jiān)控cup/內(nèi)存/磁盤使用率,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10