私はシェルプログラミングに初めて触れ、次のことが許されることを確認しました。
if [ $table = "Session" ]; then
continue; fi
次も同様です。
if [ $table = "Session" ]; then continue; fi
以下は構文エラーを生成しますが:
if [ $table = "Session" ]; then; continue; fi
then
キーワードが他のキーワードと異なる理由は何ですか?
ベストアンサー1
then
これはコマンドでもシェル組み込みでもありませんが、実際にはif
構文の一部であるためです。からman bash
:
if list; then list; [ elif list; then list; ] ... [ else list; ] fi
The if list is executed. If its exit status is zero, the then
list is executed. Otherwise, each elif list is executed in
turn, and if its exit status is zero, the corresponding then
list is executed and the command completes. Otherwise, the else
list is executed, if present. The exit status is the exit sta‐
tus of the last command executed, or zero if no condition tested
true.
だからこれ:
if [ $table = "Session" ]; then continue; fi
これは、両方とも独立して実行できるコマンドである[ $table = "Session" ]
ために機能します。対話型シェルに貼り付けるだけで構文エラーが発生しないことを確認できます。continue
list
if
martin@martin:~$ export table=test
martin@martin:~$ [ $table = "Session" ]
martin@martin:~$ continue
bash: continue: only meaningful in a `for', `while', or `until' loop
一方、then
これは実際に単独で実行できるコマンドではありません。
martin@martin:~$ then
bash: syntax error near unexpected token `then'
したがって、最初の2つの例では、if
マニュアルページの説明に従って使用しています。
if list; then list; fi
ただし、その後;
に1つを追加すると、構文エラーとして扱われます。もちろん、シェル構文は時々非常に混乱しているようです。特に初めて使用する場合にはさらにそうです。スペースが必要で周囲のスペースが必要であるという事実のため、非常に混乱していた記憶があります。しかし、これが実際にコマンドであるか組み込まれたシェルであることがわかったら理解できます。 :)then
bash
[
]
[