以下のスクリプトを使用して、スクリプトが1年の2日の開始時に実行されたときに、2日前に移動し、毎月1日と2日を確認して、2日前に移動します。
if [$month="01"] && [$day="01"];
then
date="$last_month/$yes_day/$last_year"
fulldate="$last_month/$yes_day/$last_year"
else
if [$month="01"] && [$day="02"];
then
date="$last_month/$yes_day/$last_year"
fulldate="$last_month/$yes_day/$last_year"
else
if [ $day = "01" ];
then
date="$last_month/$yes_day/$year"
fulldate="$year$last_month$yes_day"
else
if [ $day = "02" ];
then
date="$last_month/$yes_day/$year"
fulldate="$year$last_month$yes_day"
else
date="$month/$yes_day/$year"
fulldate="$year$month$yes_day"
fi
fi
fi
fi
しかし、私の悪い点は、以下のエラーメッセージを受け取っています
Etime_script.sh: line 19: [06=01]: command not found
Etime_script.sh: line 24: [06=01]: command not found
承認された回答:
[コード> メタ文字でも制御演算子でもありません(予約語でもありません。]でも同じです。 )したがって、その周りに空白が必要です。それ以外の場合、シェルはコマンド [01 =01]を「認識」します。 コマンド[の代わりに 個別のパラメータ01 、 = 、 01 、および] 。各演算子とオペランドは、[に対する個別の引数である必要があります コマンドなので、演算子の周囲にも空白が必要です。
if [ "$month" = "01" ]
[$ month ="01"] $ monthの任意の文字に一致するワイルドカードパターンです または"01 。何にも一致しない場合は、そのままにしておきます。
閉じ括弧の後にセミコロンがある場合、セミコロンは常に別のトークンの一部であるため、その前にスペースは必要ありません。
if [ "$month" = "01" ]; then
同じことがbash(およびkshとzsh)のダブルブラケット構文にも当てはまります。
複数の条件
条件を組み合わせるには2つの方法があります:
-
[内 -
別の
[&&と組み合わせたコマンド または||
角かっこでグループ化するのは、おそらく[内で簡単です。 。
if [ "$month" = "01" -a "$day" = "01" ] # -a for and, -o for or
if [ "$month" = "01" ] && [ "$day" = "01" ]
最初のものは信頼性が低いため、避ける必要があります(たとえば、 month ='!'を使用してみてください )。奇妙な変数の内容に関する問題は、最初に安全な文字列(存在する場合)を使用することで回避できます。または[[ / ]] [の代わりに / ] :
if [ "01" = "$month" -a "01" = "$day" ]