Help us understand the problem. What is going on with this article?

個人で良く使っているシェルスクリプトのテンプレートの備忘

Set

※ -E 関数の中でもエラーを拾う。 -u 何も代入されていない変数を使おうとした場合エラーになる。

set -Eu

trap

※set -Eか-eが必要

# スクリプト実行時にエラーが発生したときの動作。
trap 'trap ERR; echo -e "\nAn error occurred."; exit 1' ERR

# ここだけset -E解除
set +E
...
# set -E復帰
set -E

VAR

※引数に何も入っていない場合エラー。

VAR=${1?"Invalid argument: VAR"}

logging

LOG_FILE="/tmp/$(basename $0 | sed 's/\.[^\.]*$//').log"
exec > >(tee -a ${LOG_FILE}) 2>&1

シェルスクリプトを実行しているディレクトリのフルパス取得

#シェルスクリプトを実行しているディレクトリのフルパス取得
SHELL_DIR=$(dirname $(readlink -f $0))

正規表現チェック

if [[ $VAR1 == *"文字1"* && $VAR2 == *"文字2"* ]]; then
    #何もしない
    :
else
    xxx
fi

ヒアドキュメントの変数代入

HOGEFUGA=$(
    cat << EOS
hoge
fuga
EOS
)

変数名と変数の変数値を表示

echo "${TARGET} : $(eval echo \"\$${TARGET}\")"
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away