Menus in Korn Shell and invalid selections

Hey Guys.

I need to code a series of menus that have four options, selectable either by the number in the menu or the name, in succession. This part I have achieved however I am struggling to find a way that should the user try to enter an invalid selection, such as the number 5 or an incorrect name, there is an echo line saying that the entry is incorrect and the menu presented again - this will happen until a correct selection is made.

I have managed to get the echo line working: "echo "Chosen option is unknown, please try again!"\\n" .

As you can see, there are three menus at present.

One asks you to select from a list of friends whose names you previously input.

The next asks you to choose a celebrity from a precoded list.

The next asks you to choose a file from a precoded list.

This is my first time working with the korn shell and I first though of gotos (if incorrect, goto line whatever) but they unfortunatly dont work :frowning:

read VarName?"Please enter your name, then press Enter and Ctrl+D: "

echo "Hello, Welcome $VarName !" \\n

echo "Please enter your friends' names, seperated by spaces: " \\n

read VarFriend1 VarFriend2 VarFriend3

echo "Please choose a friend from the given menu below: "
select VarFriends in $VarFriend1 $VarFriend2 $VarFriend3 Exit

do
    if [[ -z "$VarFriends" ]]
        then
          echo "Chosen option is unknown, please try again!"\\n

    else
          echo "You choose $VarFriends"\\n
    
    fi
          
    if [[ $VarFriends = Exit ]]
        then exit
              
    else  
          echo "Welcome $VarFriends"\\n
    fi
  break
done

echo "Please choose a celebrity from the given menu below: "
select VarCeleb in RickRoss Eminem Obama Exit

do
    if [[ -z "$VarCeleb" ]]
        then
          echo "Chosen option is unknown, please try again!"\\n
         
    else
          echo "You choose $VarCeleb"\\n
    
    fi
          
    if [[ $VarCeleb = Exit ]]
        then exit
              
    else  
          echo "Welcome $VarCeleb"\\n
    fi
 break
done

echo "Please choose a File, A, B, or C which may correspond to the chosen Celebrity below: "
select VarFiles in FileA FileB FileC Exit

do
    if [[ -z "$VarFiles" ]]
        then
          echo "Chosen option is unknown, please try again!"\\n
         
    else
          echo "You choose $VarFiles"\\n
    
    grep $VarCeleb $VarFiles
    
    fi
          
    if [[ $VarFiles = Exit ]]
        then exit
              
    else  
          echo "This is $VarFiles"\\n
          cat $VarFiles
    fi
 
done

Any help would be greatly appreciated! I've been scratching my head all night about it, the answer is probably staring me in the face haha.

Thanks for all your help in advance,

Mudja

take a look at this 'paradigm':

#!/bin/ksh

PS3='Please select your name: '

names='John Fred Steve Bubba'

  select _name in ${names} quit
  do
    case ${_name} in
       quit) break ;;
       '') print -u2 "You must select one of the above [${names}]" ;;
       *)  _myName="${_name}"; break ;;
    esac
    REPLY=''
  done

  echo "Selected->[${_myName}]"

vgersh,

thanks so much for your quick response! I implemented the code and played around with it but couldnt find a way to use variables in the situation. Instead of John, Fred, Steve and bubba I need $VarFriend1, $VarFriend2, and $Varfriend3 - with these values set by the user with the previous command:

read VarFriend1 VarFriend2 VarFriend3

I seem to have trouble assigning the variable names these other variables...it just prints them as normal text.

So ideally, the code would go:

"Please enter your friends' names, seperated by spaces":

John Fred Steve (entered by user)

Then it would display these names like in the code you provided as values of $VarFriend1 $VarFriend2 and $VarFriend3.

Cheers for your help so far buddy :slight_smile: I've felt I've gotten a lot further so far already!

#!/bin/ksh

PS3='Please select your name: '
VarFriend1='John'
VarFriend2='Fred'
VarFriend3='Steve'
VarFriend4='Bubba'

names="${VarFriend1} ${VarFriend2} ${VarFriend3} ${VarFriend4}"

  select _name in ${names} quit
  do
    case ${_name} in
       quit) break ;;
       '') print -u2 "You must select one of the above [${names}]" ;;
       *)  _myName="${_name}"; break ;;
    esac
    REPLY=''
  done

Fantastic!! I'm so glad I came to this site!

I figured out a way to assign the variables, cleaned up my code a bit also and it all works. Just one more question if possible, and my internet research into the select command has so far turned up nothing. Instead of having to pick the number from the menu list, is it possible to use the name of the variable instead?

So instead of:

1) Tom
2) Dick
3) Harry
4) Quit

What would you like to choose: 3

As well as, or instead of, 3, you could write "Harry" and it would work. Obviously, the case function would throw an error if it wasn't 3 or Harry, for example you wrote Harryyy.

One more thing. I have created 3 files, FileA File B and File C. I want to set it so only FileA can be opened if Tom was selected previously, File B if Dick was selected previously, nd FileC if Harry was selected previously, else error. I figured the code might be something like:

case ${_name} 
    in  quit) break ;;
    in  Tom) cat FileA ;;
    in  Dick) cat FileB ;;
    in  Harry) cat FileC ;;
       '') print -u2 "You must select one of the above [${names}]" ;;
       *)  _myName="${_name}"; break ;;
    esac
    REPLY=''
  done

Cheers for all your help! I have never done programming before, but you have helped learn loads.

After testing the above code i found it did not work. I am still struggling to solve how I could make the one menu's options and case arguments dependent on the user response to a previous one