Shell programming and simple menus - part 1

Unix Insider |  Operating Systems 1 comment

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.

The following lines define a function
pagedir and then execute it. The function executes
an ls -l command and then pipes
the results through more to create a paging display.

#!/bin/sh
# Define a paged directory listing function
pagedir () { ls -l|more ; }
# and then execute it
pagedir

Repeating a command or set of commands can be done by enclosing the
commands within a while-do-done command.

In the following example the shell logic loops until the user enters
something other than "y."

#!/bin/sh
answer=y
while [ $answer = y ]
do
    echo "Hello"
    echo "go again?"
    read answer
done

Shell scripts also allow for a case-select type construction, case-esac that executes one of several choices based on a variable containing one of
a list of values. In the example below the choices are a), b), c), and
finally, *), which stands for anything else.

#!/bin/sh
# How to use case-esac
echo "Please enter a letter a through c"
read answer
case $answer in
    a)    echo You chose a;;
    b)    echo You chose b;;
    c)    echo You chose c;;
    *)    echo You did not chose a, b or c;;
esac

A case-esac choice can also be set up to allow the user to
enter the upper or lower case versions. In the example below the choices
are a|A), b|B), c|C), or *) which stand for a or A (a|A), b or B (b|B),
c or C (c|C), or anything else.

1 comment

    Anonymous 1 year ago
    Great, more people should see the shell as a viableprogramming language.

      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