0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

背景

他の方の技術ブログや記事を読んでいて &&|| ってコマンドラインでも使えるんだなと知りました。

説明

&& : AND制御演算子。

command1 && command2 の場合、command1が成功したらcommand2を実行する。

|| : OR制御演算子。
command1が異常終了した時に、それに対処する形でcommand2を実行出来る。
事後のエラー処理などを行なう時に使える。

実行例

今のファイルを用意

sh command01.sh
echo "Test 01";
sh command02.sh
echo "Test 02";
sh command03.sh
echo "Test 03;

※command03.shはechoの " の閉じを敢えて消してエラーを出す狙い用

実行

$ sh command01.sh && sh command02.sh
Test 01
Test 02
$ sh command03.sh && sh command02.sh
command03.sh: line 1: unexpected EOF while looking for matching `"'
command03.sh: line 2: syntax error: unexpected end of file
$ sh command03.sh || sh command02.sh
command03.sh: line 1: unexpected EOF while looking for matching `"'
command03.sh: line 2: syntax error: unexpected end of file
Test 02
$ sh command02.sh || sh command03.sh
Test 02

まとめ

上記のように、エラーが起きるcommand03.shの指定の仕方で挙動がわかりました。
なるほど〜

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?