├── numpad tree.png └── numpadInput.sh /numpad tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDGalea/bash-numpad-input/475e44fcd90e3980761f35b538a0660ce9f5a7f7/numpad tree.png -------------------------------------------------------------------------------- /numpadInput.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Bash Numpad Input script by Thomas Galea. 4 | # 5 | # This is very much intended as just an example. 6 | # Some functions are hard-coded inside this script rather than available via external scripts. 7 | # Some of those may not work for you. 8 | 9 | tput civis 10 | 11 | # Script expects a folder "numpadInput" in the same location as itself. 12 | cd numpadInput 13 | scriptRoot="$PWD" 14 | # We use a variable to contain the "beep" command so that it can be disabled by the user. 15 | beep="beep" 16 | 17 | # Main loop. 18 | while true;do 19 | clear 20 | # Print date and time at top of screen. 21 | echo -e " \e[30;47m "`date +%H:%M`" \e[0m \e[30;47m "`date "+%A, %d %B %Y"`" \e[0m \e[30;47m $HOSTNAME \e[0m " 22 | echo;echo 23 | 24 | # List options in current folder, assigning a number to each. 25 | # Colour dependant on whether item is a folder or file. 26 | x=0 27 | for item in *;do 28 | echo -ne " \e[30;47m $x \e[0m " 29 | # Check if folder or file. 30 | if [ -d "$item" ];then 31 | cutdown="${item##*_}" 32 | echo -e "\e[34m$cutdown\e[0m" 33 | unset cutdown 34 | #echo -e "\e[34m$item\e[0m" 35 | else 36 | cutdown="${item##*_}" 37 | cutdown="${cutdown%.*}" 38 | echo -e "\e[32m$cutdown\e[0m" 39 | unset cutdown 40 | #echo -e "\e[32m${item%.*}\e[0m" 41 | fi 42 | let x+=1 43 | echo 44 | done 45 | 46 | # Send cursor to bottom of page and print controls help. 47 | tput cup $(expr $(tput lines) - 3) 48 | echo -ne " \e[30;47m - \e[0m Switch to SRV0 " 49 | echo;echo 50 | echo -ne " \e[30;47m 0-9 \e[0m Menu Item " 51 | echo -ne " \e[30;47m / \e[0m Main Menu " 52 | echo -ne " \e[30;47m . \e[0m Back " 53 | echo -ne " \e[30;47m + \e[0m Toggle Beeps " 54 | echo -ne " \e[30;47m * \e[0m Restart Menu " 55 | 56 | # Set cursor colour to black so that the user input doens't appear on-screen. 57 | echo -ne "\e[30m" 58 | # Take input. 59 | inpIsNumber=0 60 | inp="" 61 | read -t10 -n1 inp 62 | # Reset cursor colour. 63 | echo -ne "\e[0m" 64 | # Check if input is number (I'm pretty sure there's a prettier way to do this but oh well). 65 | case $inp in 66 | 0) inpIsNumber=1;; 67 | 1) inpIsNumber=1;; 68 | 2) inpIsNumber=1;; 69 | 3) inpIsNumber=1;; 70 | 4) inpIsNumber=1;; 71 | 5) inpIsNumber=1;; 72 | 6) inpIsNumber=1;; 73 | 7) inpIsNumber=1;; 74 | 8) inpIsNumber=1;; 75 | 9) inpIsNumber=1;; 76 | esac 77 | if [ "$inpIsNumber" = "1" ];then 78 | # Input is number. Cycle through items until number reached. 79 | x=0 80 | target="" 81 | for currentItem in *;do 82 | if [ "$x" = "$inp" ];then 83 | target=$currentItem 84 | fi 85 | let x+=1 86 | done 87 | # Check if item exists, and if file or folder. 88 | if [ -d "$target" ];then 89 | $beep -f1000 -l50 & 90 | cd "$target" 91 | elif [ -x "$target" ];then 92 | $beep -f1000 -l50 -nf1200 -l50 & 93 | ./"$target" 94 | else 95 | $beep -f600 -l50 & 96 | fi 97 | else 98 | # Input not a number. Check if it's one of the in-script functions. 99 | # Return to root. 100 | if [ "$inp" = "/" ];then 101 | $beep -f1000 -l50 -nf750 -l50 & 102 | cd $scriptRoot 103 | # Parent folder. 104 | elif [ "$inp" = "." ];then 105 | $beep -f750 -l50 106 | if [ "$PWD" != "$scriptRoot" ];then 107 | cd .. 108 | fi 109 | # Toggle beeping. 110 | elif [ "$inp" = "+" ];then 111 | if [ "$beep" = "beep" ];then 112 | # Set command in '$beep' to 'true', which simply exits with an exit code of 0. 113 | beep="true" 114 | beep -f1000 -l50 -nf750 -l50 & 115 | else 116 | # Set command in '$beep' to 'beep'. 117 | beep="beep" 118 | beep -f750 -l50 -nf1000 -l50 & 119 | fi 120 | # Connect to SRV0. 121 | elif [ "$inp" = "-" ];then 122 | $beep -f750 -l50 -nf1000 -l50 -nf1200 -l50 123 | ssh srv0 124 | # Exit script. 125 | elif [ "$inp" = "*" ];then 126 | $beep -f1000 -l50 -nf750 -l50 -nf1000 -l50 127 | exit 0 128 | # Empty input - Either timeout, space or enter. 129 | elif [ "$inp" = "" ];then 130 | true 131 | else 132 | # No matches. 133 | $beep -f600 -l50 & 134 | fi 135 | fi 136 | done 137 | 138 | echo "Reached end of script. How?" 139 | read -n1 pause 140 | unset pause 141 | exit 1 --------------------------------------------------------------------------------