LoginSignup
5
6

More than 5 years have passed since last update.

シェルスクリプトでデバッグマクロっぽいものを実現する

Last updated at Posted at 2012-04-13

コメントにデバッグ用のコードを書いておき、必要なときに文字列置換でコメントアウトする感じ。

# foo.sh

# デバッグプリント関数
#DEBUG function warn { echo "debug: $*" }
# または赤文字のデバッグプリント関数 (Zsh 限定)
#DEBUG function warn { print -P "%F{red}debug: $*%f" }

# デバッグモードで実行する部分
#DEBUG warn "HELLO"
#DEBUG warn "WORLD"

# デバッグモードでは実行しない部分
#DEBUG : <<'EOT'
echo "hello"
echo "world"
#DEBUG EOT
# 普通に実行
$ sh foo.sh
hello
world

# デバッグモードで実行
$ sed 's/#DEBUG //' foo.sh | sh -s
debug: HELLO
debug: WORLD

# デバッグモードで source (Zsh 限定)
$ source =(sed 's/#DEBUG //' foo.sh)
debug: HELLO
debug: WORLD
5
6
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
5
6