Shell programming and simple menus - part 2

Unix Insider |  Operating Systems Add a new comment

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).

For the command: shellmenu mf.mnu the $# variable should
contain the value 1 for the 1 argument on the command line.

In "TEST THE COMMAND LINE" at line 75 through 79, the value of $# is
tested, and if it is not 1 then a usage message is displayed and the
script exits. The syntax for the test is

if [ ! $# = 1 ]
then
    usage
    exit
fi

Carefully copy the spacing used in the command there are spaces around
the left bracket and the right bracket. The "!" means NOT in this test.
The test reads "if the number of arguments is not 1 then execute a
usage() function and exit the script."

The usage() function is defined at lines 10 through 20 and
attempts to describe the correct usage for the shellmenu
script.

In "TEST THE MENU FILE", at line 87, a new variable, $MENU, is created
and is assigned the value of $1. Another test is done to determine if
the file exists and is readable. The test:

if [ -r $MENU ]

uses the name of the file in $MENU, mf.mnu, and then tests two
conditions. The -r tests that the file exists and that it is readable.
If so it returns "true" logically. We want the opposite test, so a NOT
(!) is added in:

if [ ! -r $MENU ]

Again notice spaces around the left and right brackets.

This tests if the file mf.mnu exists and is readable, provides
a message for the user, and displays the usage information and exits.

Finally the trick as to how the this menu system works appears in
"READ THE MENU FILE" at line 87. Note the period in the command:

. $MENU

    Add a comment

    Post a comment using one of these accounts
    Or join now
    At least 6 characters

    Note: Comment will appear soon after you have activated your account.
    Obscene/spam comments will be removed and accounts suspended.
    The information you submit is subject to our Privacy Policy and Terms of Service.

    ITworld LIVE

    Operating SystemsWhite Papers & Webcasts

    White Paper

    Microsoft Enterprise Agreement Program Overview

    Discover how flexible the Microsoft Enterprise Agreement Program is to help you build the right software solution agreement for your business. This paper highlights all the available options-from on-premise software and cloud service solutions, to payment options and enrollment programs, and more.

    White Paper

    Watson - A System Designed for Answers. The future of workload optimized systems design

    Watson is a workload optimized system designed for complex analytics, made possible by integrating massively parallel POWER7 processors and DeepQA technology. Read the white paper about Watson's workload optimized system design.

    See more White Papers | Webcasts

    Ask a question

    Ask a Question