Skip to content

Instantly share code, notes, and snippets.

@bytespider
Created May 29, 2012 17:44
Show Gist options
  • Save bytespider/2829697 to your computer and use it in GitHub Desktop.
Save bytespider/2829697 to your computer and use it in GitHub Desktop.
bash menu
#!/bin/sh
# Author: Rob Griffiths
# Based on work from http://www.unix.com/shell-programming-scripting/27099-drop-down-menu-bash-timezone-select.html
# Licence: MIT
# Copyright (c) 2012 Rob Griffiths
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
items=(one two three four five)
local i=0
local count=${#items[*]}
let count=count-1
echo "Please select from below using the arrow keys"
tput sc
while [ 0 ]; do
tput rc
if [ $i -lt 0 ]; then i=0; fi
if [ $i -eq ${#items[*]} ]; then let i=i-1; fi
for ((a=0; a<=$count; a++)); do
if [ $a -eq $i ]; then
tput rev
else
tput dim
fi
echo -en "\r "
echo -en "\r"
if [ $a -eq $i ]; then
echo -en " > "
else
echo -en " "
fi
echo -e "${items[a]}${RESET}"
done;
read -sn 1 twiddle
case "$twiddle" in
"B")
let i=i+1
;;
"A")
let i=i-1
;;
"")
echo
echo "OK to select ${items[$i]} (y/N)?"
read -sn 1 confirm
if [ "$confirm" == "y" -o "$confirm" == "Y" ]; then
break
else
tput cuu1
tput el
tput rc
fi
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment