Shell programming and simple menus - part 2

October 17, 2001, 10:24 AM —  Unix Insider — 

In last
month's column we started a simple menu system, as useful utility
that illustrated several shell programming concepts. Now let's take
that a step further. In the next example, the menu system is broken
into two parts: the shell script that executes the menu, and a separate
"data" file that contains the menu prompts and commands to be
executed.

Listing 2 looks very similar to the first menu in last month's Listing
1 (see sidebar) except that it is
missing all of the menu prompts and commands to execute! Where are
they?

Listing 3 is an example of a menu file to be processed by
shellmenu. This file contains the missing information. You
have to study both listings to see how these work together. Both
listings include line numbers to simplify the explanations.

If you compare last months Listing 1 to Listing 2 and Listing 3 from
this month, you will see that these two listings are almost identical,
but the prompts and function definitions have been split out of Listing
2 and placed in Listing 3.

The key points to look at in Listing 2 are "TEST THE COMMAND LINE" at
lines 68 through 80, "TEST THE MENU FILE" at lines 81 through 100 and
"READ THE MENU FILE" at lines 132 through 138.

Be sure that shellmenu has been given execute privileges
with the command:

chmod a+x shellmenu

To start this menu using the file named mf.mnu shown in
Listing 3 as the menu, type the shellmenu command followed
by the name of the file to use as the menu.

shellmenu mf.mnu

This causes the value mf.mnu to be assigned to a variable,
$1, inside the shell script. The variables $0, $1, $2, $3 and so on
exist in all shell scripts and contain any values that were listed on
the command line that was used to start the script. $0 contains the
name of the shell script itself, shellmenu. $1 contains
the first word on the command line after shellmenu, in
this case mf.mnu. The $2 variable would contain the next
word or argument on the command line after mf.mnu if there
were any. The shell also creates a variable named $#. This variable
contains the number of arguments that were on the command line after
the command (in this case, shellmenu).

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