できごと
実装したshell scriptが、docker環境(alpine linux)で動作しなかった
原因
- alpine linuxのshellはash
- POSIX標準ではない記法をスクリプトに書いてしまったため、ashで動作しなかった
対策
shellcheck というshell用のlintツールがあるので、事前に確認しよう
使い方
homebrew でインストール
.shell-session
$ brew install shellcheck
以下のコードで動作確認してみる
test.sh
# !/bin/sh
if [[ ${HOGE} =~ fuga ]]; then
echo "aaa"
fi
.shell-session
$ shellcheck test.sh
In test.sh line 3:
if [[ ${HOGE} =~ fuga ]]; then
^-- SC2039: In POSIX sh, [[ ]] is undefined.
無事、[[ はPOSIX標準記法ではないと怒られました