Operating System - Linux
1755496 Members
3239 Online
108834 Solutions
New Discussion юеВ

Writing a Menu-Driven Script

 
OMGPLSHELP
New Member

Writing a Menu-Driven Script

Hello all,

Can someone tell me how to get a menu driven script to execute a simple command? For example:


echo "\n >>> Enter for the current time"
read choice
case ${choice} in



If the user selects
, how would you write this in the script to execute the `time' command?

Thanks all!
3 REPLIES 3
Patrick Wallek
Honored Contributor

Re: Writing a Menu-Driven Script

Have a look at this page:

http://www.esscc.uq.edu.au/~ksteube/Bshell/#page_25

Also, 'man sh-posix' (the POSIX shell man page) has syntax examples.
James R. Ferguson
Acclaimed Contributor

Re: Writing a Menu-Driven Script

Hi

...
read choice
eval ${choice}
...

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Writing a Menu-Driven Script

JRF has the command being entered by the user. Is this what you want? (The user could enter "rm -rf /". )-:

In my reply to your same question in:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=993074

http://h30499.www3.hp.com/t5/Languages-and-Scripting/Sample-Shell-Script-with-Menu/m-p/3712343

I had:
I was going to say you use the select builtin. And if you look at Pete's example, it does that.

> echo "+ Enter for the current time"
> read choice
> case ${choice} in

One could do that. For my select example:
echo "Select the compare mode:"
select DT in "-r" "-b" "-a"; do
    break
done
if [ "$DT" = "" ]; then
    DT="-r"
fi

This prints:
Select the compare mode:
1) -r
2) -b
3) -a
#?

The user types in a number, 1 to 3.
The logic above says if an invalid number is typed, set it to the "-r" choice.

> If the user selects, how would you write this in the script to execute the time command?

For your case:
case ${choice} in
"") date ;;
... # other choices
*) echo "invalid choice: ${choice}" ;;
esac