6/14/2011

対話メニュー形式のshell

対話式のshellは操作する人を拘束してしまうので悪とされているけれど、何かと需要があったので対話メニュー形式のshellを作ってみた。
#!/bin/sh

# Yes or No function
# return value Yes:0 No:1 Other:2
YN ()
{
        read -p $1 YESNO
        case ${YESNO} in
                "y" | "Y")
                        return 0
                ;;
                "n" | "N")
                        return 1
                ;;
                *)
                        echo "Input number out of range. Please press enter key."
                        read wait
                        return 2
                ;;
        esac
}

# Blank Function
# echo Blank-Line
BLANK()
{
        echo ""
        echo "/-- Check results --/"
        echo ""
}

# ANYKEY Function
# Pause operation
ANYKEY ()
{
        echo ""
        echo "Please press enter key."
        read wait
}

# Get executetime function
# Output format:YYYYMMDD_hhmmss
GETTIME ()
{
        SYSTEMTIME=`date '+%Y%m%d_%H%M%S'`
        echo ${SYSTEMTIME}
}

stty erase ^H
RTNCD=1
# It Executes until return value "0"
while [ ${RTNCD} = 1 ]; do
        # Output menu list
        clear
        echo "                          Menu"
        echo ""
        echo "  1.  Change Logfile Permission"
        echo "  2.  Copy messages file"
        echo "  99. Exit Menu"
        echo ""
        echo ""

        # Read from standard input
        read -p "Input Number -->" INPUT
        case ${INPUT} in
        1)
                echo "Change Logfile Permission"
                if YN "Do?(Y/N)>"; then
                        chmod 644 /var/log/messages
                        BLANK
                        ls -l /var/log/messages
                        ANYKEY
                        RTNCD=1
                else
                        echo "Boo"
                        RTNCD=1
                fi
        ;;
        2)
                echo "Copy messages file"
                if YN "Do?(Y/N)>"; then
                        cp -p /var/log/messages /var/log/messages.`GETTIME`
                        BLANK
                        ls -l /var/log/messages*
                        ANYKEY
                        RTNCD=1
                else
                        echo "Boo"
                        RTNCD=1
                fi
        ;;
        99)
                RTNCD=0
                echo "bye"
        ;;
        *)
                echo "Input number out of range."
                ANYKEY
        ;;
        esac
done

case文に色々追記していけばそこそこ使えるかも。

0 件のコメント:

コメントを投稿