Search code examples
bashshellunixls

bash ls menu option loop back to first menu


Hello I am writing a shell script menu and I want to give the users the options to change the way they can list directories but nothing happens when the user types in a directory it just loops back to the menu.

can any of you experienced programmers point out what I am doing wrong if so thanks in advance. here is what i have so far:

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
;;

Solution

  • I think you just need a read statement at the end of the loop - otherwise the screen is cleared and the output the user has selected is therefore lost.

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

    This is only a suggestion: you could store the response in a variable and use that to exit the loop.

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