Shell指令碼傳參數方法總結_linux shell

來源:互聯網
上載者:User

一、接收固定長度的參數

複製代碼 代碼如下:

[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"

空參數執行
複製代碼 代碼如下:

[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個參數執行
複製代碼 代碼如下:

[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

二、那如果還有參數怎麼辦呢?還要一個個加上來嗎?答案是否定的

以下用法應該不陌生,就是直接執行指令碼本身,沒有附帶任何參數,那麼指令碼講拋出協助資訊.即怎麼使用此指令碼.見紅字部分

複製代碼 代碼如下:

[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

詳細的執行過程如下
不傳參數執行

複製代碼 代碼如下:

[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個參數執行
複製代碼 代碼如下:

[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個參數執行
複製代碼 代碼如下:

[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

問題來了,要是後期還要加參數怎麼辦呢?或者我也不確定到底會傳幾個參數.
解決方案如下,詳細執行結果如下

複製代碼 代碼如下:

[root@svn shell_example]# cat manyparams.sh
#!/bin/bash
#這是個測試指令碼傳N個參數的例子

echo "我的名字是 `basename $0` - 我是調用自 $0"
echo "我有 $# 參數"

count=1
while [ "$#" -ge "1" ];do
    echo "參數序號為 $count 是 $1"
    let count=count+1
    shift
done

一個參數執行

[root@svn shell_example]# sh manyparams.sh one

我的名字是 manyparams.sh - 我是調用自 manyparams.sh
我有 1 參數
參數序號為 1 是 one

5個參數執行

複製代碼 代碼如下:

[root@svn shell_example]# sh manyparams.sh one two three four five

我的名字是 manyparams.sh - 我是調用自 manyparams.sh
我有 5 參數
參數序號為 1 是 one
參數序號為 2 是 two
參數序號為 3 是 three
參數序號為 4 是 four
參數序號為 5 是 five

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.