LoginSignup
0
0

More than 5 years have passed since last update.

シェルスクリプトメモ 条件分岐編

Posted at

書き方

if [ 条件 ]; then
elif [ 条件 ]; then
else
fi

注意

  • [ ] それぞれと条件の間には、必ずスペースがいる
  • ; を忘れないこと
    • ; を書かないで改行を挟んでから then を書いても良い

条件分岐に使えるオプション

そもそも [] これは test コマンドの糖衣構文らしい。
test コマンドで使えそうなオプション一覧。

オプション 説明
-e 対象が存在するなら新
-d 対象がディレクトリなら新
-f 対象がファイルなら新

使用例

# $HOME/.zsh/environment が存在していたら読み込む
if [ -e "$HOME/.zsh/environment" ]; then
  source "$HOME/.zsh/environment"
fi
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0