2016-05-06 5 views
1

Iが出力された、ことを実行した後

...
echo please enter your name 
read name 
if [ -z "$name" ] 
then 
     echo please enter your name 
fi 
if [ -n "$name" ] 
then 
     echo Thank you so much 
fi 
echo $0 
echo $1 
echo $2 
echo $3 

を私はシンプルなbashスクリプトを書いて、それの終わりに、私は$ 0、$ 1のような位置引数をテストしてみました:

please enter your name 
j 
Thank you so much 
/bin/reza.sh 

なぜ$ 0だけが出力され、それ以外は何もありませんでしたか?

./bin/reza.sh first second third 
please enter your name 
monk 
Thank you so much 
/bin/reza.sh 
first 
second 
third 

以下のような

+3

スクリプト実行時に '$ 1'を渡していませんでした... – Jahid

+3

$ 1はあなたが一度も渡したことのない最初のコマンドライン引数です。 – monk

答えて

0

スクリプトに入力する引数は、$ 1、$ 2、$ 3などの順番です。このtestscript

#!/bin/bash 
echo $0 #Gives you the command/script name itself 
echo $1 #Gives you the first argument 
echo $2 #Gives you the second argument 
echo $3 #Gives you the third argument 
echo [email protected] #Gives you all arguments 
echo $# #Gives you the total number of arguments excluding the script name 

ので

$./testscript a b c 

の結果は、引数が割り当てられていない場合は、その値がnullまたは何も意志である

./testscript 
a 
b 
c 
a b c 
3 

です印刷される。

$ printf "%sThere is nothing before this.\n" $1 

はあなたを与える:

There is nothing before this. 

注:は終わりに自動的にbashで改行を追加します、引数をテストするために、エコーをechoを使用しないでください。

+0

それは素晴らしい答えでした。どうもありがとうございます。 –

+0

@mimrmoghimi:あなたは大歓迎です – sjsam

3

実行それはまた、$ 0がスクリプト自体のファイル名です。

+0

ポジティブな議論がどのように機能するかは非常に混乱していました。あなたのお手伝いをありがとう。 –

+0

あなたを助けてくれてうれしいです。 – monk

関連する問題