Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Bash select

farenda 2015-10-02 0

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!
Categories Bash Tags bash
Previous: Java Thread example
Next: Java Thread priority

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored