専用の簡易シェルをつくって遊ぶ

といってもシェルスクリプトですが。

小ネタ的に、プロセスが起動し続けるような連続運転テスト用に専用の簡易シェルをつくって遊びました。

#!/bin/sh

SCRIPT_DIR="."
BTB_PROCESS=$SCRIPT_DIR"/test.sh"

echo "h/help/? for help"
while true
do
    printf "test>"
    read line
    pid=`ps -ef | grep ${BTB_PROCESS} | grep -v grep | awk '{print $2}'`
    if [ "${line}" = "exit" ]; then
        break
    elif [ "${line}" = "h" ]||[ "${line}" = "help" ]||[ "${line}" = "?" ]; then
        echo "h/help/?  : read help"
        echo "start     : start test script background"
        echo "stop      : stop test script running"
        echo "status    : check test is running or not"
    elif [ "${line}" = "start" ]; then
        if [ "$pid" = "" ]; then
            nohup ${BTB_PROCESS} &
            echo "Started running test.."
        else
            echo "Already running."
        fi
    elif [ "${line}" = "stop" ]; then
        if [ "$pid" = "" ]; then
            echo "Already stopped."
        else
            kill -9 ${pid}
            echo "Stopped running test.."
        fi
    elif [ "${line}" = "status" ]; then
        [ "${pid}" != "" ] && echo "Running." || echo "Stopped"
    else
        sleep 0
    fi
done

コマンドラインで実行してみるとこんな感じになります。

-bash-3.00$ ./myshell
h/help/? for help
test>h
h/help/?  : read help
start     : start test script background
stop      : stop test script running
status    : check test is running or not
test>status
Stopped
test>start
Started running test..
test>nohup: appending output to `nohup.out'

test>status
Running.
test>exit
-bash-3.00$

簡単にシェルっぽいものがつくることができて面白いです。シェルスクリプトベースだと、さすがにやれる事に限界がありますが。

Term::Shellを使ってPerlで書くなどすれば、何か便利なものがつくれるかもという気がしてきました。*1

*1:まさにTerm::Shellを継承して書かれているtwittershellのような