您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Shell腳本傳參數(shù)的方法介紹”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
一、接收固定長度的參數(shù)
代碼如下:
[root@svn shell_example]# cat params.sh
#!/bin/bash
#傳參測試腳本
echo "My name is `basename $0` -I was called as $0"
echo "My first parameter is : $1"
echo "My second parameter is : $2"
空參數(shù)執(zhí)行
代碼如下:
[root@svn shell_example]# sh params.sh
My name is params.sh -I was called as params.sh
My first parameter is :
My second parameter is :
傳遞2個參數(shù)執(zhí)行
代碼如下:
[root@svn shell_example]# sh params.sh one two
My name is params.sh -I was called as params.sh
My first parameter is : one
My second parameter is : two
二、那如果還有參數(shù)怎么辦呢?還要一個個加上來嗎?答案是否定的
以下用法應(yīng)該不陌生,就是直接執(zhí)行腳本本身,沒有附帶任何參數(shù),那么腳本講拋出幫助信息.即怎么使用此腳本.見紅字部分
代碼如下:
[root@svn shell_example]# sh params_v2.sh
My name is params_v2.sh -I was called as params_v2.sh
I was called with 0 parameters.
Usage: params_v2.sh first second
You provided 0 parameters,but 2 are required.
代碼如下
代碼如下:
[root@svn shell_example]# cat params_v2.sh
#!/bin/bash
# 這是個測試腳本傳參的測試例子
echo "My name is `basename $0` -I was called as $0"
echo "I was called with $# parameters."
if [ "$#" -eq "2" ];then
echo "My first parameter is $1"
echo "My second parameter is $2"
else
echo "Usage: `basename $0` first second"
echo "You provided $# parameters,but 2 are required."
fi
詳細的執(zhí)行過程如下
不傳參數(shù)執(zhí)行
代碼如下:
[root@svn shell_example]# sh params_v2.sh
My name is params_v2.sh -I was called as params_v2.sh
I was called with 0 parameters.
Usage: params_v2.sh first second
You provided 0 parameters,but 2 are required.
傳遞3個參數(shù)執(zhí)行
代碼如下:
[root@svn shell_example]# sh params_v2.sh one two three
My name is params_v2.sh -I was called as params_v2.sh
I was called with 3 parameters.
Usage: params_v2.sh first second
You provided 3 parameters,but 2 are required.
傳遞2個參數(shù)執(zhí)行
代碼如下:
[root@svn shell_example]# sh params_v2.sh one two
My name is params_v2.sh -I was called as params_v2.sh
I was called with 2 parameters.
My first parameter is one
My second parameter is two
問題來了,要是后期還要加參數(shù)怎么辦呢?或者我也不確定到底會傳幾個參數(shù).
解決方法如下,詳細執(zhí)行結(jié)果如下
代碼如下:
[root@svn shell_example]# cat manyparams.sh
#!/bin/bash
#這是個測試腳本傳N個參數(shù)的例子
echo "我的名字是 `basename $0` - 我是調(diào)用自 $0"
echo "我有 $# 參數(shù)"
count=1
while [ "$#" -ge "1" ];do
echo "參數(shù)序號為 $count 是 $1"
let count=count+1
shift
done
一個參數(shù)執(zhí)行
[root@svn shell_example]# sh manyparams.sh one
我的名字是 manyparams.sh - 我是調(diào)用自 manyparams.sh
我有 1 參數(shù)
參數(shù)序號為 1 是 one
5個參數(shù)執(zhí)行
代碼如下:
[root@svn shell_example]# sh manyparams.sh one two three four five
我的名字是 manyparams.sh - 我是調(diào)用自 manyparams.sh
我有 5 參數(shù)
參數(shù)序號為 1 是 one
參數(shù)序號為 2 是 two
參數(shù)序號為 3 是 three
參數(shù)序號為 4 是 four
參數(shù)序號為 5 是 five
“Shell腳本傳參數(shù)的方法介紹”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。