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

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

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

向shell腳本傳遞數(shù)據(jù)的最基本方法是使用命令行參數(shù)。命令行參數(shù)允許運(yùn)行腳本時在命令行中添加數(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獲取在命令行中運(yùn)行的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.

但這里有一個潛在的問題。如果使用另一個命令來運(yùn)行shell腳本,則命令名會和腳本名混在一起,出現(xiàn)在位置變量$0中:

$ ./positional0.sh
This script name is ./positional0.sh.

這還不是唯一的問題。如果運(yùn)行腳本時使用的是絕對路徑,那么位置變量$0就會包含整個路徑:

$ $HOME/scripts/positional0.sh
This script name is /home/christine/scripts/positional0.sh.

如果你編寫的腳本中只打算使用腳本名,那就得做點(diǎn)兒額外工作,剝離腳本的運(yùn)行路徑。好在有個方便的小命令可以幫到我們。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)識運(yùn)行時間的日志消息:

$ 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)審計(jì)和生成日志信息是非常有用的。

參數(shù)測試

在shell腳本中使用命令行參數(shù)時要當(dāng)心。如果運(yùn)行腳本時沒有指定所需的參數(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ù),而實(shí)際上根本沒有的時候,腳本很可能會產(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ù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)shell 用戶輸入傳遞參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

和田县| 自治县| 常州市| 遂宁市| 太和县| 龙井市| 宁远县| 漳平市| 长葛市| 万全县| 铜川市| 邵东县| 额济纳旗| 钟山县| 宁乡县| 太湖县| 天门市| 赞皇县| 灵宝市| 柳江县| 浠水县| 石林| 连江县| 水城县| 乌兰县| 紫云| 方山县| 兴宁市| 时尚| 鱼台县| 荃湾区| 尚义县| 牡丹江市| 东山县| 隆化县| 青川县| 佛教| 青岛市| 若羌县| 女性| 沅陵县|