アフィリエイト広告を利用しています

広告

この広告は30日以上更新がないブログに表示されております。
新規記事の投稿を行うことで、非表示にすることが可能です。
posted by fanblog

2017年02月12日

シェルスクリプトです。 コマンドファイルを読み込んで順次コマンド実行するのですが コマンドファイル内に期待する実行結果などを記載し 自動で実行確認もしてくれるのを考えてました。(まだ色々問題はありそうですが)

#!/sbin/sh

#================================================================
# ファイル名:cmd_seqexec.sh
# 概要 :コマンドファイルの順次実行
# パラメータ:
# P1:コマンドファイル名
# P2:開始行数
# コマンドファイル形式:
# コマンド名,チェック(無/自動/対話),条件(一致/不一致),期待結果
#================================================================

#1ならdebugプリント出力
debug=0

#1なら全てのコマンド実行結果(標準出力)を出力する
#1以外なら対話確認の時だけ出力する
cmd_result_print=0

#初期化
gyou=0
cmd=""
cmd_start=0

#---------------------------------------
#デバックプリント関数
#---------------------------------------
debug_echo()
{
if [ $debug -eq 1 ]
then
echo "(debug)$debug_msg"
fi
}

#---------------------------------------
#エラープリント関数
#---------------------------------------
err_echo()
{
echo "コマンドファイルの順次実行が異常終了しました。"
echo "$err_msg"
if [ $cmd_start -eq 1 ]
then
echo "行数:$gyou コマンド名:$cmd"
fi
exit 0
}

#---------------------------------------
#コマンド実行関数
#---------------------------------------
func_cmd_exec()
{
#Noと現時刻を表示
echo "=================================================================="
echo ">>>行数:$gyou"
echo ">>>実行コマンド:$cmd"

#コマンド実行を行い、標準出力された内容を実行結果に設定する
cmd_result=$($cmd)

###cmd_result=$($cmd 2>&1) #標準出力、標準エラーの両方を変数に設定

if [ $cmd_result_print -eq 1 ]
then
echo ">>>実行結果:$cmd_result"
elif [ $chk = "対話" ]
then
echo ">>>実行結果:$cmd_result"
fi
}

#---------------------------------------
#対話確認関数
#---------------------------------------
func_taiwa()
{
while true
do
echo ">>>対話確認を行います。実行結果のok/ngを選択して下さい。:"
read answer
case $answer in
ok)
break
;;
ng)
err_msg="対話にてNGが選択されました。"; err_echo
;;
*)
;;
esac
done
}

#---------------------------------------
#メイン処理ここから
#---------------------------------------

#---------------------------------------
#パラメータチェック
#---------------------------------------

#入力されたかチェック
if [ $# -eq 0 ]
then
err_msg="第一パラメータでコマンドファイルが指定されていません。"; err_echo
fi

#ファイルが存在するかチェック
if [ ! -e $1 ]
then
err_msg="第一パラメータで指定されたファイルがありません。"; err_echo
fi

file=$1

if [ $# -eq 1 ]
then
err_msg="第2パラメータで開始行数が指定されていません。"; err_echo
fi

#数字かチェック

#P2に+1をし、その戻り値をRETに保存
expr $2 + 1 > /dev/null 2>&1
ret=$?

#戻り値を使って正常か異常かを判断
if [ ! $ret -lt 2 ]
then
err_msg="第2パラメータが数字ではありません。"; err_echo
fi

start_gyou=$2

#---------------------------------------
#コマンドファイルの順次実行
#---------------------------------------

gyou=1
cmd_start=1

while : #無限ループ
do
debug_msg="$gyou行目を処理します。"; debug_echo

if [ $gyou -lt $start_gyou ]
then
debug_msg="開始行数ではないのでパス"; debug_echo
gyou=$((gyou + 1))
continue
fi

#1行取得
line=$(sed -n ${gyou}P $file)

debug_msg="line=$line"; debug_echo

cmd=`echo "$line" | cut -d ',' -f 1`
chk=`echo "$line" | cut -d ',' -f 2`
jyouken=`echo "$line" | cut -d ',' -f 3`
expected_val=`echo "$line" | cut -d ',' -f 4`

debug_msg="1カラム目(コマンド):$cmd"; debug_echo
debug_msg="2カラム目(チャック):$chk"; debug_echo
debug_msg="3カラム目(条件  ):$jyouken"; debug_echo
debug_msg="4カラム目(期待結果):$expected_val"; debug_echo

if [ -z "$cmd" ]
then
debug_msg="コマンド未入力のためパス"; debug_echo
gyou=$((gyou + 1))
continue
fi

if [ $(echo "$cmd" | cut -c1) = "#" ]
then
debug_msg="コマンドの先頭文字が#のためスキップします。"; debug_echo
gyou=$((gyou + 1))
continue
fi

if [ "$cmd" = "end" ]
then
debug_msg="コマンドがendなので終了します。"; debug_echo
break
fi

case $chk in
"自動")
case $jyouken in
"一致")
if [ -z "$expected_val" ]
then
err_msg="コマンドファイルの期待結果が未入力です。"; err_echo
fi

#コマンド実行
func_cmd_exec

#期待結果と比較
if [ $cmd_result = $expected_val ]
then
echo ">>>自動チェック(一致):OK"
else
err_msg="自動チェックにてNGとなりました。"; err_echo
fi
;;
"含む")
if [ -z "$expected_val" ]
then
err_msg="コマンドファイルの期待結果が未入力です。"; err_echo
fi

#コマンド実行
func_cmd_exec

#期待結果と比較
if [ "$(echo "$cmd_result" | grep "$expected_val")" ]
then
echo ">>>自動チェック(含む):OK"
else
err_msg="自動チェックにてNGとなりました。"; err_echo
fi
;;
*)
err_msg="コマンドファイルの条件で一致/含む以外が指定されました。"; err_echo
;;
esac
;;

"対話")
#コマンド実行
func_cmd_exec

#対話確認関数
func_taiwa
;;

"無")
#コマンド実行
func_cmd_exec
;;
*)
err_msg="コマンドファイルのチェックで自動/対話/無以外が指定されました。"; err_echo
;;
esac

gyou=$((gyou + 1))

sleep 1
done

echo "コマンドファイルの順次実行が正常終了しました。"

#正常終了
exit 1
ファン
検索
<< 2017年02月 >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28        
最新記事
写真ギャラリー
最新コメント
タグクラウド
カテゴリーアーカイブ
月別アーカイブ
プロフィール
日別アーカイブ
×

この広告は30日以上新しい記事の更新がないブログに表示されております。