[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bbc script



> On Thu, 19 Aug 2021, Greg Wooledge wrote:
> > Also relevant: https://mywiki.wooledge.org/BashFAQ/115

On Thu, Aug 19, 2021 at 07:40:23PM -0400, Jude DaShiell wrote:
> I had a read of your wiki  article and think I almost understand it.  To
> the left of the parens there's a single letter in that calculator example.
> That I understand.  If you have a) add as part of the case statement is
> add in that partial case statement so when that selection is made add
> appears on the screen?  If so, I can improve what I've done.

The echo commands write the output to the terminal.  They control what
the user sees as the menu.

   echo "== Make your selection:"
   echo "a) add"
   echo "s) subtract"
   echo "m) multiply"
   echo "q) quit"

There's nothing complicated here.  That's the whole point.  You decide
what the user should see, then you write that to the terminal, using
echo, or printf, or cat and a here-doc, or whatever you like.

The case statement simply compares what the user typed to a bunch of
predefined strings.  You choose what those strings are.

      case $ch in
        a) add; break;;
        s) subtract; break;;
        m) multiply; break;;
        q) exit 0;;
        *) echo "Unrecognized command.  Please try again.";;
      esac

Again, it's not complicated.  If the user types "a", then you run the
"add" command.  If the user types "s", you run the "subtract" command.
And so on.

If you wanted to allow the user to type either "a" or "ad" or "add" to
enter the addition menu, you could change the case statement to look
like this:

      case $ch in
        a | ad | add) add; break;;

And so on.  It's your menu.  You define how it should work.

In this example, "add" is a function.  It's defined below the "main" menu
function, but it could be defined pretty much anywhere.

add() {

That's the start of the definition of "add".  I didn't include the
definitions of "subtract" and "multiply" because they're similar, and
you should be able to figure them out.  Of course, in your program,
you probably aren't going to prompt the user for numbers and then add
them.  You're going to do something entirely different.  So, again,
there's no point in writing a bunch of useless functions in this example.

The main points I'm trying to convey here are:

1) You don't need "select" to make a menu.  You can just use echo and read.

2) In fact, "select" locks you in to a specific form of menu with very
   little flexibility.  If you use echo and read, you can make your menu
   work however you like.  You can do a *better* menu.  (Hell, you could
   get all fancy and use whiptail or dialog if you want.)

3) If you have nested menus, it's useful for each menu to be in its own
   function.  The parent menu should call the appropriate function to
   transfer control to the next (child) menu.  The child menu's function
   should simply "return" (or fall off the end of the function) to transfer
   control back to the parent.  Don't explicitly *call* the parent again.

And please, don't overthink it.  This is really basic stuff.


Reply to: