3.45. 使用until编写选择菜单

#!/usr/bin/env bash
#usage:xxx
#scripts_name:xxx.sh
# author:xiaojian
#(1)按数字键1,列出当前目录的内容。
#(2)按数字键2,切换到指定的目录。
#(3)按数字键3,根据指定的文件名创建文件。
#(4)按数字键4,根据指定的文件名编辑文件。
#(5)按数字键5,根据指定的文件名删除文件。
#(6)按数字键6,退出程序
#当用户的输入非1、2、3、4、5、6时,重新显示菜单。

input=              #定义变量,初始值为空
until
    echo "---------------------------------------"
    echo "Please enter your choice:(1-6)"
    echo "(1) List you selected directory"
    echo "(2)Change to you selected directory"
    echo "(3)Create a new file"
    echo "(4)Edit you selected file"
    echo "(5)Remmove you selected file"
    echo "(6)Exit Menu"
    echo "--------------------------------------------------------"
    read -p "Please input you choice:" input              #读取用户输入,并保存到变量input中
    test $input = 6         # 如果input的值为5,退出until循环,否则继续执行
do
    case $input in
    1)
       ls
       ;;
    2)
        echo "Enter target directory"
        read dir
        cd $dir
       ;;
    3)
        echo "Enter a file name:"
        read file
        touch $file
       ;;
    4)
        echo "Enter a file name:"
        read file
        vi $file
       ;;
    5)
        echo "Enter a file name:"
        read file
        rm $file
       ;;
    esac

done