Shell腳本傳參數(shù)方法總結(jié)
來(lái)源:易賢網(wǎng) 閱讀:711 次 日期:2015-05-11 11:21:18
溫馨提示:易賢網(wǎng)小編為您整理了“Shell腳本傳參數(shù)方法總結(jié)”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了Shell腳本傳參數(shù)方法總結(jié),本文著重講解傳遞固定個(gè)數(shù)參數(shù)的方法、可變個(gè)數(shù)參數(shù)的方法兩部份內(nèi)容,需要的朋友可以參考下

一、接收固定長(zhǎng)度的參數(shù)

代碼如下:

[root@svn shell_example]# cat params.sh

#!/bin/bash

#傳參測(cè)試腳本

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個(gè)參數(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ù)怎么辦呢?還要一個(gè)個(gè)加上來(lái)嗎?答案是否定的

以下用法應(yīng)該不陌生,就是直接執(zhí)行腳本本身,沒(méi)有附帶任何參數(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

# 這是個(gè)測(cè)試腳本傳參的測(cè)試?yán)?/p>

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

詳細(xì)的執(zhí)行過(guò)程如下

不傳參數(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個(gè)參數(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個(gè)參數(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

問(wèn)題來(lái)了,要是后期還要加參數(shù)怎么辦呢?或者我也不確定到底會(huì)傳幾個(gè)參數(shù).

解決方法如下,詳細(xì)執(zhí)行結(jié)果如下

代碼如下:

[root@svn shell_example]# cat manyparams.sh

#!/bin/bash

#這是個(gè)測(cè)試腳本傳N個(gè)參數(shù)的例子

echo "我的名字是 `basename $0` - 我是調(diào)用自 $0"

echo "我有 $# 參數(shù)"

count=1

while [ "$#" -ge "1" ];do

echo "參數(shù)序號(hào)為 $count 是 $1"

let count=count+1

shift

done

一個(gè)參數(shù)執(zhí)行

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

我的名字是 manyparams.sh - 我是調(diào)用自 manyparams.sh

我有 1 參數(shù)

參數(shù)序號(hào)為 1 是 one

5個(gè)參數(shù)執(zhí)行

代碼如下:

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

我的名字是 manyparams.sh - 我是調(diào)用自 manyparams.sh

我有 5 參數(shù)

參數(shù)序號(hào)為 1 是 one

參數(shù)序號(hào)為 2 是 two

參數(shù)序號(hào)為 3 是 three

參數(shù)序號(hào)為 4 是 four

參數(shù)序號(hào)為 5 是 five

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:Shell腳本傳參數(shù)方法總結(jié)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)