InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Creating Simple Menus in bash
Bash

Creating Simple Menus in bash

InfinityCoder February 19, 2017

You have a simple SQL script that you would like to run against different databases to reset them for tests that you want to run.

You could supply the name of the database on the command line, but you want something more interactive.

How can you write a shell script to choose from a list of names?

Use the select statement to create simple character-based screen menus.

Here’s a simple example:

1
2
3
4
5
6
7
8
9
#!/usr/bin/env bash
# cookbook filename: dbinit.1
#
DBLIST=$(sh ./listdb | tail +2)
select DB in $DBLIST
do
   echo Initializing database: $DB
   mysql -uuser -p $DB <myinit.sql
done

Ignore for a moment how $DBLIST gets its values; just know that it is a list of words (like the output from ls would give).

The select statement will display those words, each preceded by a number, and the user will be prompted for input.

The user makes a choice by typing the number and the corresponding word is assigned to the variable specified after the keyword select (in this case DB).
Here’s what the running of this script might look like:

1
2
3
4
5
6
7
8
9
$ ./dbinit
1) testDB
2) simpleInventory
3) masterInventory
4) otherDB
#? 2
Initializing database: simpleInventory
#?
$

When the user types “2” the variable DB is assigned the word simpleInventory.

If you really want to get at the user’s literal choice, the variable $REPLY will hold it, in this case it would be “2”.
The select statement is really a loop. When you have entered a choice it will execute the body of the loop (between the do and the done) and then re-prompt you for the next value.
It doesn’t redisplay the list every time, only if you make no choice and just press the Enter key.

So whenever you want to see the list again, just press Enter.
It does not re-evaluate the code after the in, that is, you can’t alter the list once you’ve begun.

If you modified $DBLIST inside the loop, it wouldn’t change your list of choices.
The looping will stop when it reaches the end of the file, which for interactive use means when you type Ctrl-D.

(If you piped a series of choices into a select loop, it would end when the input ends.)
There isn’t any formatting control over the list. If you’re going to use select, you have to be satisfied with the way it displays your choices.

You can, however, alter the prompt on the select.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Formatting Dates for Display in bash
You need to format dates or time for output. Use …

Formatting Dates for Display in bash

Summing a List of Numbers in bash
You need to sum a list of numbers, including numbers …

Summing a List of Numbers in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap