shell -- shift用法

來源:互聯網
上載者:User

標籤:

shift是Unix中非常有用的命令。可以使命令參數左移,從而使指令碼程式中命令參數位置不變的情況下依次遍曆所有參數。
如shift 3表示原來的$4現在變成$1,原來的$5現在變成$2等等,原來的$1、$2、$3丟棄,$0不移動。不帶參數的shift命令相當於shift 1。

樣本:shift_test.sh

#!/bin/shif [ $# -eq 0 ]then    echo "Usage: $0 arg1 arg2..."    exit 1fiuntil [ $# -eq 0 ]do    echo "$*"    echo "first arg $1; Total $#"    shiftdone

執行結果:$ > ./shift_test.sh 1 2 3 4 51 2 3 4 5first arg 1; Total 52 3 4 5first arg 2; Total 43 4 5first arg 3; Total 34 5first arg 4; Total 25first arg 5; Total 1

Shift 命令還有另外一個重要用途, Bsh 定義了9個位置變數,從 $1 到 $9,這並不意味著使用者在命令列只能使用9個參數,藉助 shift 命令可以訪問多於9個的參數。
Shift 命令一次移動參數的個數由其所帶的參數指定。例如當 shell 程式處理完前九個命令列參數後,可以使用 shift 9 命令把 $10 移到 $1。

shell -- shift用法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.