最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

shell處理用戶輸入傳遞參數(shù)的實(shí)現(xiàn)

 更新時(shí)間:2024年11月01日 09:43:03   作者:pineapple rong  
本文主要介紹了shell處理用戶輸入傳遞參數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

向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腳本名。這在編寫包含多種功能或生成日志消息的工具時(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.

如果你編寫的腳本中只打算使用腳本名,那就得做點(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ù)編寫一個(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ò)誤消息。這種編寫腳本的方法并不可取。在使用位置變量之前一定要檢查是否為空:

$ 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)文章

最新評(píng)論

河东区| 宁波市| 宣武区| 临武县| 青神县| 湖南省| 四川省| 环江| 息烽县| 岳西县| 安西县| 奉贤区| 江城| 丹江口市| 乐亭县| 分宜县| 龙门县| 格尔木市| 台前县| 嘉定区| 成武县| 曲靖市| 沁源县| 阳城县| 万全县| 肇州县| 扎兰屯市| 青州市| 土默特右旗| 天台县| 连江县| 华宁县| 东阿县| 安西县| 贡觉县| 资溪县| 庄河市| 广西| 江门市| 双城市| 邹城市|