Why is my selection not working?


New Member
Dec 11, 2019
Reaction score
So I'm working on editing this code and I thought I had all the errors for it fixed but when I run the code it works, like the code shows up but when i type in the letter to make a selection it doesn't actually execute the right part of the code it doesn't do anything and I'm not sure how to fix it.

while true
    #Clear the screen before displaying the menu again
    echo "       SYSA 3204 - Main Menu  "
    echo "---------------------------------"
    echo "U. User Analysis"
    echo "D. Disk Usage "
    echo "E. Exit from the menu"

    #read the option
    read -p "Enter Your Selection (U/D/E)  : " option

    case $option in
            # validate lastname to contain lowercase characters  - 5 to 8 characters
            while true
                read -p "Enter the directory name : " dname
                echo $dname | grep -qE '^[A-Z]*$'
                if ([ ?? -eq 0 ]); then
                    if ([ -z "$dname" ]); then
                        echo "Directory Name cannot be empty"
                        length=$(echo $dname || wc -c)
                        if [ $length -ge 6 ] || [ $length -le 9 ]; then
                            echo "Directory Name must be between 5 to 8 characters"
                    echo "$dname is invalid name - you must enter lowercase characters between 5 to 8"

            if [ ! -d $dname ]; then
                mkdir $dname

            if ([ ! -f $dname/users.txt ]); then
                     touch $dname\users.txt
            last | grep -e ^dm | grep "Dec 6" | awk '{print $1" "$5" "$4" "$2}'  >>$dname/users.txt
            no_users=$(cat $dname/users.txt | wc -c)
            if ([ $no_users -gt 5 ]); then
              'BEGIN {FS=":" ; \
              printf "%-15s %-10s %-20s %-20s \n----------------------------------------------------------\n"
              "Login Name", "Terminal", "IP Address", "Time Spent}
              {printf "%-15s %-10s %-20s %-20s\n" $1, $3, $2,$4}
              END {print "----------------------------------------------------------"}'
              echo "Number of Users : no_users"
               echo "Number of Users less than 5"

             df | grep  ^/ | awk '{print $3" "$2" "$1" "$5}' || tee data.txt
            nos= cat data.txt | wc -l
            echo -e "/nThe number of file systems = $nos on $(date +"%b-%Y-%a")\n"
            echo "The highest File System is : "
            (cat data.txt | sort -nk3 -r | tail -1)

        [Ee]) echo "Now I can relax- no more tests except Project 2 to finish"

         echo "Your Selection is wrong. Please enter A, D or E only"
    read -p "Press Enter To Continue..." dummy

This is my full code and the selections I'm having problems with are [Dd]) and [Ee])

G'day @slotegraafd and welcome to linux.org :)

I am moving this to Command Line, where it will draw attention from those with coding knowledge.

Good luck

Chris Turner
The code is being called for the options D and E, but you are just looping straight back to display the menu. So you aren't actually seeing the results.

So, for example - when the user presses E you get the "Now I can relax..." message, plus the "Your selection is wrong" error message.
Then the loop re-iterates and you get the menu re-displayed.

Surely when the user presses E, you'd expect the script to exit. So after outputting the farewell message, perhaps put in a call to exit. That fixes one problem.

Also, your error message is in the wrong place. At the moment, the error message is displayed after each of the commands has completed and before reiterating through the loop and re-displaying your menu. Regardless of whether or not the user actually entered something invalid.
So in order to catch invalid inputs - you will need to add a default case in your case statements that will display the error message.

Finally - your "Press enter to continue" prompt is in the wrong place. You've put it outside the main loop.

In order to give the user the opportunity to see the final output from the command they selected and before the screen clears and the menu is re-drawn, you will need to move the prompt inside the main loop, immediately BEFORE the "done" statement that closes the loop.

So the last few lines of your script should look something like this:
        [Ee]) echo "Now I can relax- no more tests except Project 2 to finish"
        *) echo "Your Selection is wrong. Please enter A, D or E only"
        read -p "Press Enter To Continue..." dummy

Now all of those problems are fixed.

However I can't be 100% sure of the rest of the code used for the U and D options, because I don't have your test-data. The script just throws errors for me using options U and D. So I will leave you to check that!
Last edited:
As an addendum - I spotted those errors and problems in the code straight away without even having to run the script. But I'm an experienced programmer - I'm used to finding these kinds of things.

But I can fully appreciate how it can be difficult to spot problems like this if you are new to programming. Especially when it appeared as if your script was not calling parts of the code.

With the buggy version of your code -
For the U option - you know that the code is being called because it prompts you to enter a directory name.

But when you pressed options E or D there were no additional user inputs.
So if you were lucky - you may have spotted a flash or something when the code that was executed displayed output - just before the screen cleared and the menu was re-displayed. But if you didn't spot that - it would appear to you that your script had done nothing.

So your confusion is completely understandable.

If you ever experience a situation like this again - you should know that you can run your script in debug mode like this:
bash -x ./yourscript.sh

That will run your script and give simultaneous output about which lines of code were called and shows the state of variables used in each line too. That will allow you to trace the execution of your script and narrow in on the causes of bugs in your code.
Another option is the set x

set x
... the rest of my script goes here.

Members online

Latest posts