2013-11-27 42 views
1

香港專業教育學院遇到的代碼

if [ $# -eq 1 ]; then 
    echo "usage: Phar ~/flashmem ~/archive" 
    exit 
fi 

我從來沒有遇到[$#當量1]。之前,我似乎無法找到一個意義

+1

不幸的是,[bash manual](http://www.gnu.org/software /bash/manual/html_node/index.html)的編寫使得很難找到諸如'$#'之類的東西,因爲它沒有顯示'$'。所謂的「特殊參數」,比如'$ *','$ @','$#'等等[參見3.4.2特殊參數](http://www.gnu.org/software/bash /manual/html_node/Special-Parameters.html)。 –

+0

這也不利於Google比人(1)bash更加積極地去掉符號。 – kojiro

回答

4

的 「$#」 返回作爲參數傳遞的參數個數

#!/bin/bash 
echo $# 

現在

./testess.sh test1 test2 test3 

這回3

./testess.sh test1 test2 test3 test4 test5 

這回5

所以在你的代碼中如果「$#」等於第一個數字(只傳遞一個參數),執行echo命令

1

Expands to the number of positional parameters in decimal.

(從man bash複製,Special Parameters下)。

+0

這意味着什麼呢?對不起,我對bash很陌生 – WhereAreYouSyntax

相關問題