Java Programming Tutorials

Java programming tutorials with many code examples!

Bash select

Problem:

How to use Bash select function to write interactive scripts? In this post we’ll show how to use select to interact with user and guide her through filesystem. Read on.

Solution:

The syntax:

select name [ in list ] ; do commands ; done

In the simplest case select presents given list of values as an enumerated choice. Then reads user input, which should be a number from presented list. Read input is stored in variable REPLY and matching value in variable name. Then it executes commands. Everything is in a loop until Ctlr+D (EOF) is found as an input.

Here’s a simple Bash script that uses select to display user’s choice:

#!/bin/bash
select x in foo bar baz; do
    echo "Your choice: ${x}"
done

Let’s run it:

$> ./simple-select.sh
1) foo
2) bar
3) baz
#? 2
Your choice: bar
#? 3
Your choice: baz
#? 1
Your choice: foo
#?
$>

If in list is not provided then select presents positional parameters, which can be script parameters or function parameters. Example using Bash select to present script parameters:

#!/bin/bash

select param; do
    echo "Your choice: ${param}"
done

The script in action:

$> ./select-param.sh
$> ./select-param.sh foo bar baz
1) foo
2) bar
3) baz
#? 3
Your choice: baz
#?

Yet another example. This time we’ll use Bash select to implement a simple directory browser. The script presents content of current directory and reads user’s action. If selected item is a file – displays it, directory – opens it. For other things we just display error message.

#!/bin/bash

REPLY=.
while [ -n "$REPLY" ] ; do
    select d in $(ls -a); do
        if [ -d "${d}" ]; then
            cd "${d}"
            echo "pwd: $(pwd)"
        elif [ -f "${d}" ]; then
            echo "cat ${d}:"
            cat "${d}"
        else
            echo "Don't know what to do with: ${REPLY}"
        fi
        break
    done
done

We have to break each select loop and use external while-do, because select doesn’t evaluate given list of items on each loop and we want to do that, because we may have to display a new directory content.

And here’s the file manager script in action:

$> ./select.sh
1) .                  4) com                 7) fun.sh             10) select.sh
2) ..                 5) compile.sh          8) getopts.sh         11) until-loop.sh
3) case.sh            6) echo.sh             9) here-documents.sh  12) while-loop.sh
#? 4
pwd: /farenda/bash/com
1) .
2) ..
3) farenda
#? 2
pwd: /farenda/bash
1) .                  4) com                 7) fun.sh             10) select.sh
2) ..                 5) compile.sh          8) getopts.sh         11) until-loop.sh
3) case.sh            6) echo.sh             9) here-documents.sh  12) while-loop.sh
#? 3
cat case.sh:
#!/bin/bash

function usage() {
    echo "$0 demonstrates Bash case command usage"
    exit 1
}

while [ ! -z "${1}" ]; do
    case "$1" in
        -o|--option)
            OPTIONS="$2"; shift 2;;
        -d|--directory)
            DIRECTORY="$2"; shift 2;;
        -h|--help)
            usage;;
        *)
            echo "Unknown param: '${1}'"
            usage;;
    esac
done

echo "Options: ${OPTIONS}"
echo "Directory: ${DIRECTORY}"
1) .                  4) com                 7) fun.sh             10) select.sh
2) ..                 5) compile.sh          8) getopts.sh         11) until-loop.sh
3) case.sh            6) echo.sh             9) here-documents.sh  12) while-loop.sh
#?
$>

IMHO it’s very useful for writing interactive scripts or wizards that should guide users through available actions.

If you know interesting use-case for Bash select function please share in the comments. Thanks! :-)

Share with the World!