2010年9月14日 星期二

選單程式(Case)

<1>mycase.sh
#判斷輸入的檔案為哪一類的壓縮檔
#!/bin/bash

read -p "filename: " x
case "${x##*.}"  in
        gz)
                echo this is gz file.
                ;;
        bz2)
                echo this is bz2 file.
                ;;
        1)
                clear
                ;;
        *)
                echo "Archive format not recognized."
                exit 1
                ;;
esac
exit 0

<2>menu.sh
#選單程式,1.使用vim編輯檔案 2.離開
#!/bin/bash
clear

function Menu {
echo "<1> Edit file"
echo "<2> Quit"

echo ""
echo -ne "choice: "
read userchoice
clear

case $userchoice in
        "1")
#利用check來決定迴圈執行與否。預設0,檔案存在變1,不存在還是給0重來
                check=0
                while [ "$check" == "0" ]
                do
                        read -p "please input filename or input q exit: " filename
                        [ $filename == "q" -o $filename == "Q" ] && return
                        if [ ! -f "$filename" ]; then
                                echo "請輸入正確檔案位置"
                                check=0
                        else
                                check=1
                        fi
                done
                vim $filename

                ;;
        "2")
                echo -n "Do you really want to quit (y/n)?"
                read -e ans
                [ "$ans" == "y" -o "$ans" == "Y" ] && exit 0
                echo ""
                ;;
        *)
                echo "Unknowm $userchioce"
                ;;
esac
}

while [ 1 ]
do
        clear
        Menu

        echo ""
        echo -ne "Return to Main Menu ..."
        read
done

沒有留言:

張貼留言