Sponsored Content
Top Forums Shell Programming and Scripting Menus in Korn Shell and invalid selections Post 302263901 by Mudja on Tuesday 2nd of December 2008 03:54:42 PM
Old 12-02-2008
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 Smilie

Code:
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
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to convert from korn shell to normal shell with this code?

well i have this code here..and it works fine in kornshell.. #!/bin/ksh home=c:/..../ input=$1 sed '1,3d' $input > $1.out line="" cat $1.out | while read a do line="$line $a" done echo $line > $1 rm $1.out however...now i want it just in normal sh mode..how to convert this?... (21 Replies)
Discussion started by: forevercalz
21 Replies

2. Shell Programming and Scripting

Shell Script Menus - Rejecting invalid input (KSH)

Greetings all, I'm currently writing a shell script menu which is dynamically populated from an array. Have a question to ask about the filtering of invalid input. I'm using KSH. A brief description of my algorithm is as follows: 1) Read in input from user and store in a variable. (a valid... (2 Replies)
Discussion started by: rockysfr
2 Replies

3. Shell Programming and Scripting

menu selections

I am trying to find a way to allow users to select multiple options in a shell menu. I am using case and it gives menu options 1-9, how can I set this up so that it give the user the ability to choose more then one option, ie 1,2 or 3,4,5, etc... (4 Replies)
Discussion started by: lwif
4 Replies

4. UNIX for Advanced & Expert Users

Shell menus And Oracle

Dear All, Kindly suggest on how should i proceed with the following requirement I need to develop an interactive shell script menu which would enable the user to inquire the value of a column based on a key value . The output can be more records.. Also is it possible to do the following 1)... (2 Replies)
Discussion started by: ksm
2 Replies

5. Shell Programming and Scripting

01.30 Invalid shell error

Hi, I am getting the error 01.30 Invalid shell error I am running the bash shell script in the korn login shell. I have mentioned the #!/bin/bash statement in the my script but not sure why it is giving this error to me.. (4 Replies)
Discussion started by: mr_harish80
4 Replies

6. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

7. Solaris

Detect Invalid Data by C shell

Dear all, I'd be so grateful if I could get great feedback again for my problems. We usually spool some text files from our system in csv format. Unfortunately, some data contains ',' (comma) and it's rare case but when it comes to spool, that row included comma should be invalid data, due... (2 Replies)
Discussion started by: elph
2 Replies

8. UNIX for Dummies Questions & Answers

Invalid option errors running shell script

The script below fails with the following error messages: gzip: invalid option -- 'w' Try `gzip --help' for more information. mysqldump: Got errno 32 on write cp: invalid option -- 'w' Try `cp --help' for more information. rm: invalid option -- 'w' Try `rm --help' for more information. ... (1 Reply)
Discussion started by: ANNACTION
1 Replies

9. Shell Programming and Scripting

Shell Scripting Problem - Invalid Back Reference

Here is the question... Create a new script, sub2, taking three parameters... 1.) the string to be replaced 2.) the string with which to replace it 3.) the name of the file in which to make the substitution ...that treats the string to be replaced as plain text instead of as a regular... (1 Reply)
Discussion started by: johnhisenburg
1 Replies
sh(1)							      General Commands Manual							     sh(1)

NAME
sh - Shell, the standard command language interpreter DESCRIPTION
[Tru64 UNIX] Tru64 UNIX provides two command interpreters with the name sh. The XCU5.0 and POSIX.2 compliant command interpreter sh is available in the file /usr/bin/posix/sh and is described in the sh(1p) reference page. The Bourne shell, historically known as sh, is available in the file /usr/bin/sh and is described in the sh(1b) reference page. [Tru64 UNIX] Your initial, or login, shell is determined by your entry in the file /etc/passwd. This file can be changed only by your sys- tem administrator. You must use whatever procedures are in place at your location to have this entry changed. [Tru64 UNIX] If available on your system, you may use the passwd -s or the chsh commands to change your login shell. Note This option is not available if your site manages passwords through the Network Information Service (NIS) facility. Check with your system administrator. [Tru64 UNIX] Subsequent shells spawned from the initial shell depend on the value in the environment variable BIN_SH. If this variable is set to xpg4, the POSIX shell is started. If this variable is set to svr4, an SVR4 compliant version of the shell is started. If this vari- able is unset, the Bourne shell is started. If this variable is set to any other value, an error is reported and the results are unpre- dictable. See the EXAMPLES section for information on setting this variable. NOTES
[Tru64 UNIX] With Tru64 UNIX Version 4.0 the Korn shell, /usr/bin/ksh is the same as the POSIX shell /usr/bin/posix/sh. RESTRICTIONS
[Tru64 UNIX] The file /etc/shells must include entries for both the POSIX shell /usr/bin/posix/sh and the Bourne shell, /usr/bin/sh. If this file is incorrect, see your system administrator. EXAMPLES
Using the Bourne, Korn, or POSIX shell, to set the variable BIN_SH to use the POSIX/ XCU5.0compliant shell, enter: BIN_SH=xpg4 export BIN_SH Using the Bourne, Korn, or POSIX shell, to set the variable BIN_SH to use the SVR4 compliant shell, enter: BIN_SH=svr4 export BIN_SH Using the Bourne, Korn, or POSIX shell, to unset the variable BIN_SH, enter: unset BIN_SH Using the C/ shell, to set the variable BIN_SH to use the POSIX/XCU5.0 compliant shell, enter: setenv BIN_SH xpg4 Using the C/ shell, to set the variable BIN_SH to use the SVR4 compliant shell, enter: setenv BIN_SH svr4 Using the C/ shell, to unset the variable BIN_SH, enter: unsetenv BIN_SH FILES
User profile. Contains user information, including the login shell name. Contains the names of available and permitted shells. SEE ALSO
Commands: csh(1), ksh(1), Bourne shell sh(1b), POSIX shell sh(1p), passwd(1) Files: passwd(4), shells(4) Standards: standards(5) sh(1)
All times are GMT -4. The time now is 10:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy