${var?} のような bash 変数パラメータ展開における疑問符の意味は何ですか? 質問する

${var?} のような bash 変数パラメータ展開における疑問符の意味は何ですか? 質問する

次のように使用される bash 変数の意味は何ですか?

 ${Server?}

ベストアンサー1

これは、(man ページから)とほぼ同じように動作しますbash

${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

この特定のバリアントは、変数が存在すること(定義されており、null ではないこと)を確認します。存在する場合は、それを使用します。存在しない場合は、 で指定されたエラー メッセージword( がない場合は適切なメッセージword)を出力し、スクリプトを終了します。

これとコロンなしバージョンとの実際の違いは、bash引用したセクションの上の man ページで確認できます。

部分文字列の拡張を実行しない場合は、以下に記載されている形式を使用して、bash設定されていないまたは null のパラメータをテストします。コロンを省略すると、設定されていないパラメータのみがテストされます。

つまり、上記のセクションは次のように変更できます (基本的に「null」ビットを削除します)。

${parameter?word}
Display Error if Unset. If parameter is unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

その違いは次のように示されます。

pax> unset xyzzy ; export plugh=

pax> echo ${xyzzy:?no}
bash: xyzzy: no

pax> echo ${plugh:?no}
bash: plugh: no

pax> echo ${xyzzy?no}
bash: xyzzy: no

pax> echo ${plugh?no}

pax> _

両方の設定が解除されている間、そしてnull 変数は ではエラーになり:?、設定されていない変数のみ ではエラーになります?

おすすめ記事