Skip to content

cdrubin/bash_ui

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bash_ui

Some helpful functions for making interactive bash scripts on Linux terminal respond to cursor keys for data entry.

(and now not dependent on tput from ncurses)

DOWNLOAD

select, multi-select and select value


Example 1 (select):

Allow a user to select a path

source bash_ui.sh

CHOICES=`find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'`
choose_one

echo $CHOSEN

Example 2 (select multiple):

Ask user to select from a set of options

source bash_ui.sh

CHOICES=`find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'`
choose_multiple

echo $CHOSEN

Example 3 (select):

source bash_ui.sh

read -r -d '' CHOICES <<EOT 
1. Option 1=1 
2. Yes, option 2=2 
3. Believe it or not, option 3=3 
EOT 

choose_one

if [ "$CHOSEN" == "3" ]; then 
  echo "Somehow it really was option 3 that was selected!" 
fi 

Example 4 (select value):

source bash_ui.sh

read -r -d '' CHOICES <<EOT 
one=1 
two=2
three=3
all=1\n2\n3
EOT 

choose_one

if [ ${CHOSEN} == "3" ]; then 
  echo "'three' which corresponds to value 3 was selected!" 
fi 

shui

(welcome to shui! DOWNLOAD)

A helper script that does not need to be sourced, works in bash < 4 (hello macOS) and with Linux and Mac terminals


Example 5 (select)

#!/bin/bash

result=$(find /home -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | ./shui 2>&1 1>/dev/tty)
echo "Chosen home dir: $result"

Example 6 (select multiple)

#!/bin/bash

result=$(find /home -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | ./shui multiple 2>&1 1>/dev/tty)
echo "Chosen home directories:"
echo "$result"

About

respond to arrow keys for selections in bash scripts

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages