Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

8
2

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 5 years have passed since last update.

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

Last updated at Posted at 2019-09-11

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}\")"
8
2
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
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?