溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Shell腳本位置參數(shù)如何使用

發(fā)布時間:2023-03-23 13:41:44 來源:億速云 閱讀:138 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Shell腳本位置參數(shù)如何使用”,在日常操作中,相信很多人在Shell腳本位置參數(shù)如何使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Shell腳本位置參數(shù)如何使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

1.訪問命令行

Shell提供了一組名為位置參數(shù)的變了,其中包含了命令行上的各個單詞,這些變量按照0-9分別命名,

[sysadmin@ansible bin]$ cat posit-param.sh
#!/bin/bash
echo "
\$0 = $0
\$1 = $1
\$2 = $2
\$3 = $3
\$4 = $4
\$5 = $5
\$6 = $6
\$7 = $7
\$8 = $8
\$9 = $9
"
[sysadmin@ansible bin]$ posit-param.sh
$0 = /home/sysadmin/bin/posit-param.sh
$1 =
$2 =
$3 =
$4 =
$5 =
$6 =
$7 =
$8 =
$9 =

就算沒有提供參數(shù)值,$0始終出現(xiàn)在命令行中的第一項,表示執(zhí)行程序的路徑。如果提供了參數(shù)值,會看到下列執(zhí)行結(jié)果:

[sysadmin@ansible bin]$ posit-param.sh a b c d

$0 = /home/sysadmin/bin/posit-param.sh
$1 = a
$2 = b
$3 = c
$4 = d
$5 =
$6 =
$7 =
$8 =
$9 =

能通過參數(shù)擴展訪問的位置參數(shù)不止9個,要想指定第9個之后的參數(shù),將數(shù)字放入花括號中即可。即${10}、${211}等

2 確定參數(shù)個數(shù)

Shell還提供了變量$#,其中包含了命令行中的參數(shù)個數(shù)

[sysadmin@ansible bin]$ cat posit-param.sh
#!/bin/bash
echo "
Number of arguments: $#
\$0 = $0
\$1 = $1
\$2 = $2
\$3 = $3
\$4 = $4
\$5 = $5
\$6 = $6
\$7 = $7
\$8 = $8
\$9 = $9
"
[sysadmin@ansible bin]$ posit-param.sh a b c d

Number of arguments: 4
$0 = /home/sysadmin/bin/posit-param.sh
$1 = a
$2 = b
$3 = c
$4 = d
$5 =
$6 =
$7 =
$8 =
$9 =

3 shift-訪問多個參數(shù)

每執(zhí)行一次shift命令,就將所有的參數(shù)“左移一個位置”。實際上,通過shift命令,我們可以從始至終只和一個參數(shù)打交道(除了$0):

[sysadmin@ansible bin]$ cat posit-param2.sh
#!/bin/bash
count=1
while [[ $# -gt 0 ]]; do
        echo "Argument $count = $1"
        count=$((count + 1))
        shift
done
[sysadmin@ansible bin]$ posit-param2.sh a b c d
Argument 1 = a
Argument 2 = b
Argument 3 = c
Argument 4 = d

每次執(zhí)行shift,$2的值就會移入$1,然后$3的值移入$2,依次類推。與此同時,$#的值也會相應減一。

4 簡單應用

[sysadmin@ansible bin]$ cat file-info
#!/bin/bash

#file-info

PROGNAME="$(basename "$0")"

if [[ -e "$1" ]]; then
        echo -e "\nFile Type:"
        file "$1"
        echo -e "\nFile Status:"
        stat "$1"
else
        echo "$PROGNAME: usage: $PROGNAME file" >&2
        exit 1
fi

5 在Shell函數(shù)中使用位置參數(shù)

位置參數(shù)既可以向Shell腳本傳遞參數(shù),也可以向Shell函數(shù)傳遞參數(shù)。作為演示,我們將file_info腳本改寫成Shell函數(shù):

[sysadmin@ansible bin]$ cat file-info
#!/bin/bash

#file-info

file_info () {
        if [[ -e "$1" ]]; then
        echo -e "\nFile Type:"
            file "$1"
                echo -e "\nFile Status:"
                stat "$1"
        else
                echo "$FUNCNAME: usage: $FUNCNAME file" >&2
                return 1
        fi
}

file_info "$1"

6 批量處理位置參數(shù)

有時候批量處理所有位置參數(shù)更為實用,Shell為此提供了兩個特殊參數(shù)*和@,兩者均可擴展成完整的位置參數(shù)列表,但其區(qū)別有些微妙。

參數(shù)描述
$*擴展成從1開始的位置參數(shù)列表。如果它出現(xiàn)在雙引號內(nèi)部,則擴展成由雙引號引用的字符串,其中包含了所有的位置參數(shù),彼此之間以Shell變量IFS的第一個字符分割(默認是空格符)
$@擴展成從1開始的位置參數(shù)列表,如果它出現(xiàn)在雙引號內(nèi)部,則將每個位置參數(shù)擴展成獨立的單詞
[sysadmin@ansible bin]$ cat posit-params3
#!/bin/bash

# posit-params3

print_params () {
        echo "\$1 = $1"
        echo "\$2 = $2"
        echo "\$3 = $3"
        echo "\$4 = $4"
}

pass_params () {
        echo -e "\n" '$* :';print_params $*
        echo -e "\n" '"$*" :';print_params "$*"
        echo -e "\n" '$@ :';print_params $@
        echo -e "\n" '"$@" :';print_params "$@"
}

pass_params "word" "words with spaces"
[sysadmin@ansible bin]$ posit-params3

 $* :
$1 = word
$2 = words
$3 = with
$4 = spaces

 "$*" :
$1 = word words with spaces
$2 =
$3 =
$4 =

 $@ :
$1 = word
$2 = words
$3 = with
$4 = spaces

 "$@" :
$1 = word
$2 = words with spaces
$3 =
$4 =

到目前為止,“$@”適用于大部分情況,因為其保留了每個位置參數(shù)的整體性。為了保證安全性,應該堅持使用這種方法。

到此,關(guān)于“Shell腳本位置參數(shù)如何使用”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(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)容。

AI