1834395 Members
2194 Online
110066 Solutions
New Discussion

Menu scripts

 
SOLVED
Go to solution
someone_4
Honored Contributor

Menu scripts

I was wondering what is the best way to write a menu driven script?
I have written some using if statements and some using the case statement. Does it matter as far as performance or is it just personal preference ?

Richard
4 REPLIES 4
Patrick Wallek
Honored Contributor
Solution

Re: Menu scripts

I personally prefer the case statement for menu scripts. I think it's a litle easier and cleaner looking.

That's just me though.......
A. Clay Stephenson
Acclaimed Contributor

Re: Menu scripts

Hi Richard,

Performance in a menu is hardly an issue but clarity is. A long list of if's quickly gets messy but a case statement seldom does.

Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Menu scripts

Hi Richard:

There's another very good reason for using 'case' statements in many languages (shell programs among them).

'case' statements can greatly simplify readability of complex blocks of code. Entire blocks of code, including other 'if' satements can easily be added under each case head (label). This greatly enhances the ability to augment a script over time with new features without making the script unreadable or untenable to enhance or further maintain.

In shell scripting, file-matching patterns can be used, something that can't be done with an 'if' statement. For example,

case `ls *.log` in
syslog.log )
...
esac

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: Menu scripts

Another choice is the select statement in POSIX and ksh shells. It is a select..do..done type of statement as in:


PS3="/nChoose number:"
select ANS in new prev next edit exit quit
do
case $ANS in
new) print "New choice";;
prev) print "Go back";;
prev|next) print "edit things";;
exit|quit) break;;
*) print "Bad choice";;
esac
done

PS3 is the select prompt. Multiple columns are automatically formatted to fit within LINES and COLUMNS. select is repeated until the script hits a break, exit or return. TMOUT can be changed to perform a timeout.


Bill Hassell, sysadmin