2022-07-27 シェルスクリプトの set -eu / SREの理念と原則
シェルスクリプトの set -eu
シェルスクリプトのグッドプラクティスとして知られている -eu
のフラグ。
シェルスクリプトを書くときはset -euしておく - Qiita
#!/bin/bash -eu
# ここにシェルスクリプト書く
以下はmanより引用。
-e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command (see SHELL GRAMMAR above), exits with a non-zero status.
-u Treat unset variables and parameters other than the special parameters “@” and “*” as an error when performing parameter expansion. If expan- sion is attempted on an unset variable or parameter, the shell prints an error message, and, if not interactive, exits with a non-zero sta- tus.
ただ下記のケースでは、-e
は邪魔になることがあるので使う場合は注意。
-e は command1 || command2 みたいなことが出来なくなるの使うことないな。-uは付けといて良いが。
SREの理念と原則
SRE の話。基調講演の山口さんのお話がとても良かった。