【问题标题】:bash ls menu option loop back to first menubash ls 菜单选项循环回到第一个菜单
【发布时间】:2014-04-16 21:41:05
【问题描述】:

您好,我正在编写一个 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
;;

【问题讨论】:

    标签: bash shell unix ls


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      您可能需要考虑使用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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-30
        • 1970-01-01
        • 1970-01-01
        • 2012-06-29
        • 2022-01-01
        • 2014-03-08
        相关资源
        最近更新 更多