Case switching

Hello Folks

I am writing this simple program but I am stuck at this point. Here is the snippet from my script where I have issues.


3)     echo "Current Directory:   $(pwd)                Menu 3"

      echo -e "Enter a file name\n"
      read fname
      if [ -f $name ]
      then

              if [ -x $name ]
              then
                cat $name
              else
                echo "You do not have access to file $name"
              fi
      else
                 echo "File $name doesn't exist"
        
                echo -e "Press Enter to return to the Main Menu\c"
                read dummy
        fi
;;

4)      clear
        
        echo "Current Directory:   $(pwd)                Menu 4"

        echo  -e "Enter a directory name\n"
        read dname
        if [ -d $dname ]
        then
                cd $dname
                if [ $? -eq 0 ]
                then
                        continue
                else
                        echo "Directory $dname not accessible"
        
                fi
        else
                echo "Subdirectory $dname does not exist."
        
                echo -e "Press Enter to return to the Main Menu\c"
                read dummy
        fi

This is a section of the script where it takes entry either 3 or 4. If 3 is selected it prompts for the user to enter filename and if the user is hitting just enter and not any filename, it should return to main menu. But that's not happening, its just looping around and waiting for an input. But if 4 is selected and an enter is pressed with out directory name it goes to that cd $dname part and moves to main menu. Since $dname is empty as the user has not provided any value it goes to home directory of the user.

My requirement is that the script should go back to main menu if the user hits just "enter" with out any value. How can I achieve that?

Heya

You are reading fname , and compare name later.
Also highly recomend, use quotes around variables when comparing (if, case, ...)
Otherwise an empty variable results in a failing script, rather than a failed comparision.

hth

1 Like

that was typo "fname and name" happened when I pasted it here and edited. But when I put the variables in quotes on if condition, it started working.

Just seeing more places of chances to break the script.
Put quotes around all variables that may contain file or folder names, as both of which may contain spaces.
The absolute only place no quotes are required, is if a variable is set to numbers, as the do not contain spaces at all - doesnt apply to this code sample though.

Saying these need quotes too:

  • cat file
  • -[fdx] file

Have a nice day

But is there any way we can just exit to main menu if the value entered is a null?

Got the answer comparing the vaue with null and doing continue.

There are for and while loops.

For a menu, a while loop is highly recomended.
So you set a variable to be checked (boolean), and while its true, you loop the menu.

The actual actions become functions, which are called upon selection within the while loop.
When the function ends (return), it returns back to the menu, unless you quite the script (exit).

hth