Shell programming and simple menus - part 1

October 15, 2001, 01:12 PM —  Unix Insider — 

Any programmer or system administrator can
benefit from limiting end-user
access to the things that they actually need. For the timid, Unix offers
little help on how to start, and for the adventurous Unix offers too
many simple commands that can cause havoc.

A menu system is an almost mandatory add-on to any Unix system in the real
world. In saying "real world" Unix system, I mean one that doesn't involve
sitting in a dark room hunched over a glowing terminal trying to determine
the next highest prime number.

Commercial menu systems involve more than a simple prompt that dispatches
to some program based on a user selection. But often a simple approach will
get the job done. While I was creating EZMENU for Unix, one customer
required just such a simple solution.

The menu had to:

  1. Provide one or more prompts.
  2. Allow the user to select a prompt.
  3. Execute one or more commands associated with the selection.

The menu also had to provide a method for the user to exit the menu.

The design of the menu is simple, and it provides an excellent vehicle to
learn some shell programming tricks. The logic for a menu can be described
in the following simple steps:

  1. Display a menu.
  2. Get the user's menu pick.
  3. Check that the pick is valid and if not, display an error message and
    return to step 1.

  4. If the user's menu pick is to exit, then exit.
  5. Otherwise, execute the command(s) associated with the menu pick.
  6. Loop back to step 1.

The menu prompts can be displayed directly using the echo command as in:

echo "Please enter 1 to select Accounts Receivable"

or indirectly using a variable as in:

amenu="Please enter 1 to select Accounts Receivable"
echo $amenu

A menu pick can be collected from the user by using the read command which
will read input from the keyboard as in:

echo "Please enter your choice"
read answer
 

Defining functions

Another important feature of the shell (sh or ksh) is the ability to define
a function. A shell function is a collection of one or more shell commands
that are given a function name. A function can be executed by issuing the
name of the function in the shell script as if it were a Unix command.
However, a function must be defined before it can be executed. To define a
function, give it a name followed by an open and close parentheses,
followed by opening and closing curly braces. Within the curly braces place
the commands to be executed.

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
peer-to-peer

jfruh
Apple syncing patent can't come soon enough

pasmith
New Twitter features borrow from 3rd party clients

Esther Schindler
Open Source Changes the Software Acquisition Process

mikelgan
How to set up continuous podcast play on the new iTunes

David Strom
Five important Windows 7 mobility features

sjvn
Guard your Wi-Fi for your own sake                        

Sandra Henry-Stocker
Grepping on Whole Words

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace