bash ls菜单选项循环回到第一个菜单

时间:2014-04-16 21:41:05

标签: bash shell unix ls

您好我正在编写一个shell脚本菜单,我想给用户提供更改列表目录方式的选项,但是当用户键入目录时它没有任何反应,它只是循环回菜单。

你们中任何有经验的程序员都可以提前指出我做错了吗? 这是我到目前为止所做的:

while true
        do
        clear
        echo "Listing options "
        echo "========================================="
        echo "1. List Hidden Files "
        echo "2. List the name of the current directory "
        echo "3. show files that have a following / or * "
        echo "4. show group ownership of files in the directory "
        echo "5. Print the inode id for each file in the directory "
        echo "6. Long listing of details about the files and the directory "
        echo "7. List all sub directories encountered while listing "
        echo "8. Sort by Time instead of name "

                read dirChoice
                case "$dirChoice" in

                1)echo -n "Please Enter the directory you wish to list: "
                read dList
                ls -a ~/$dList;
                break;;
                2)echo -n "Please Enter the directory you wish to list: "
                read dList
                ls -d ~/$dList;
                ;;
                3)echo -n "Please Enter the directory you wish to list: "
                read dList
                ls -f ~/$dList;
                ;;
esac
done
;;

2 个答案:

答案 0 :(得分:1)

我认为你只需要在循环结束时使用read语句 - 否则屏幕会被清除,因此用户选择的输出就会丢失。

esac
read -p "press any key to continue " 
done

这只是一个建议:您可以将响应存储在变量中并使用它来退出循环。

 read -p "press x to quit - any other key to contine " answer
 if [ "$answer = "x" ];then
     break
 fi

答案 1 :(得分:1)

您可能需要考虑使用select命令:

choices=( "List Hidden Files"
          "List the name of the current directory"
          "Show files that have a following / or *"
          "Show group ownership of files in the directory"
          "Print the inode id for each file in the directory"
          "Long listing of details about the files and the directory"
          "List all sub directories encountered while listing"
          "Sort by Time instead of name")

select dirChoice in "${choices[@]}"; do
    case "$dirChoice" in

            1) read -p "Please Enter the directory you wish to list: " dList
               ls -a ~/$dList;
               break;;

             # etc. 
    esac
done