
Bashのスクリプトをデバッグする方法です。
Bashのスクリプトをデバックする方法は?
「bash -x」コマンドを使用して、スクリプトを実行します。
サンプルのスクリプトを用意する!
以下のサンプルのスクリプトを要します。
$ cat sample_bash_03.sh
#!/bin/bash
#
# Usage : sample_bash_03.sh $1
# Version : 1.0
#
SHELL_NAME=`basename $0`
STA_MSG=Start!
END_MSG=End!
# START
/usr/bin/logger -ip local0.crit -t ${SHELL_NAME} ${STA_MSG}
# Main
if [[ $# -ge 2 ]]; then
MSG="Too many Arguments : $#"
elif [[ -z "$1" ]]; then
MSG="Default"
else
MSG=$1
fi
echo "$MSG"
# END
/usr/bin/logger -ip local0.crit -t ${SHELL_NAME} ${END_MSG}
exit 0
スクリプトをトレースする!
「bash -x」コマンドを使用して、スクリプトをトレースします。実行された行と、変数の値を知ることができます。
$ bash -x sample_bash_03.sh ++ basename sample_bash_03.sh + SHELL_NAME=sample_bash_03.sh + STA_MSG='Start!' + END_MSG='End!' + /usr/bin/logger -ip local0.crit -t sample_bash_03.sh 'Start!' + [[ 0 -ge 2 ]] + [[ -z '' ]] + MSG=Default + echo Default Default + /usr/bin/logger -ip local0.crit -t sample_bash_03.sh 'End!' + exit 0
おわりに
Bashで作成したスクリプトの動作を解析する場合は、「bash -x」コマンドでスクリプトを実行して、スクリプトの動作をトレースします。
関連記事
関連書籍(Amazon)
