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
LKSH(1) 						    BSD General Commands Manual 						   LKSH(1)

NAME
lksh -- Legacy Korn shell built on mksh SYNOPSIS
lksh [-+abCefhiklmnprUuvXx] [-+o opt] [-c string | -s | file [args ...]] DESCRIPTION
lksh is a command interpreter intended exclusively for running legacy shell scripts. It is built on mksh; refer to its manual page for details on the scripting language. It is recommended to port scripts to mksh instead of relying on legacy or idiotic POSIX-mandated behav- iour, since the MirBSD Korn Shell scripting language is much more consistent. LEGACY MODE
lksh has the following differences from mksh: o lksh is not suitable for use as /bin/sh. o There is no explicit support for interactive use, nor any command line editing or history code. Hence, lksh is not suitable as a user's login shell, either; use mksh instead. o The KSH_VERSION string identifies lksh as ``LEGACY KSH'' instead of ``MIRBSD KSH''. o lksh only offers the traditional ten file descriptors to scripts. o lksh uses POSIX arithmetics, which has quite a few implications: The data type for arithmetics is the host ISO C long data type. Signed integer wraparound is Undefined Behaviour. The sign of the result of a modulo operation with at least one negative operand is unspeci- fied. Shift operations on negative numbers are unspecified. Division of the largest negative number by -1 is Undefined Behaviour. The compiler is permitted to delete all data and crash the system if Undefined Behaviour occurs. o The rotation arithmetic operators are not available. o The shift arithmetic operators take all bits of the second operand into account; if they exceed permitted precision, the result is unspecified. o The GNU bash extension &> to redirect stdout and stderr in one go is not parsed. o The mksh command line option -T is not available. o Unless set -o posix is active, lksh always uses traditional mode for constructs like: $ set -- $(getopt ab:c "$@") $ echo $? POSIX mandates this to show 0, but traditional mode passes through the errorlevel from the getopt(1) command. o lksh, unlike AT&T UNIX ksh, does not keep file descriptors > 2 private. SEE ALSO
mksh(1) https://www.mirbsd.org/mksh.htm https://www.mirbsd.org/ksh-chan.htm CAVEATS
lksh tries to make a cross between a legacy bourne/posix compatibl-ish shell and a legacy pdksh-alike but ``legacy'' is not exactly speci- fied. The set built-in command does not have all options one would expect from a full-blown mksh or pdksh. Talk to the MirOS development team using the mailing list at <miros-mksh@mirbsd.org> or the #!/bin/mksh (or #ksh) IRC channel at irc.freenode.net (Port 6697 SSL, 6667 unencrypted) if you need any further quirks or assistance, and consider migrating your legacy scripts to work with mksh instead of requiring lksh. MirBSD May 2, 2013 MirBSD
All times are GMT -4. The time now is 11:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy