Why menu won't allow 2 character input?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why menu won't allow 2 character input?
# 1  
Old 07-10-2014
Why menu won't allow 2 character input?

I’m trying to write a script for users to easily check folder size.
The idea is to have a menu that starts at the top directory and then drills down to lower directories.

Selections 1-9 work fine.
The issue I’m having is any 2 digit menu selections (10+) doesn’t work and it returns the error message ‘Invalid Selection’

Can someone explain why I’m unable to enter a 2 character menu option?

* For the code example I removed options 3-9.
I know I could use alpha characters to have more than 9 options but I’m trying to understand the error.

Thank you for your time and knowledge.


Code:
#!/bin/bash
#================================================
trap "rm ./f 2> /dev/null; 0 t" 0 1 3
loop=y
while test $loop = "y"
do
  clear
  tput cup 3 12; echo "Select Path for Top 25 Folder Sizes"
  tput cup 4 12; echo "=================================="
  tput cup 6 9; echo " 1 = /path/to/dir1"
  tput cup 7 9; echo " 2 = /path/to/dir1/subdir1"
  tput cup 8 9; echo "10 = /path/to/dir1/subdir2"
  tput cup 9 9; echo "11 = /path/to/dir1/subdir3"
  tput cup 10 9; echo " Q = Quit"
  tput cup 12 9; echo "Selection:"
  tput cup 12 19;
  read choice || continue
  case $choice in
    [1]) path= /path/to/dir1" ;;
    [2]) path="/path/to/dir1/subdir1" ;;
    [10]) path="/path/to/dir1/subdir2" ;;
    [11]) path="/path/to/dir1/subdir3" ;;

    [Qq]) clear ; exit ;;

    *)tput cup 14 9; echo "Invalid Selection"; read choice ;;
  esac
  echo $path

  cd $path && ls | xargs -I {} du -s {} | sort -nr | head -25 | awk '{sum=$1;
  hum[1024**4]="T"; hum[1024**3]="G";hum[1024**2]="M";hum[1024]="K";
  for (x=1024**4; x>=1024; x/=1024){if (sum>=x) { printf "%.1f%s\t\t",sum/x,hum[x];print $2;break}}}' | less
done


Last edited by Scott; 07-10-2014 at 02:58 PM.. Reason: Added code tags; removed FONT tags
# 2  
Old 07-10-2014
With a few corrections just the loop only the square brackets are not used when using dedicated letters or pseudo-numbers...
The second 'read' is not really necessary.
Code:
#!/bin/bash
loop=y
while test $loop = "y"
do
clear
tput cup 3 12; echo "Select Path for Top 25 Folder Sizes."
tput cup 4 12; echo "===================================="
tput cup 6 9; echo " 1 = /path/to/dir1"
tput cup 7 9; echo " 2 = /path/to/dir1/subdir1"
tput cup 8 9; echo "10 = /path/to/dir1/subdir2"
tput cup 9 9; echo "11 = /path/to/dir1/subdir3"
tput cup 10 9; echo " Q = Quit"
tput cup 12 9; echo "Selection:"
tput cup 12 19;
read choice || continue
case $choice in
	1) path="/path/to/dir1" ;;
	2) path="/path/to/dir1/subdir1" ;;
	10) path="/path/to/dir1/subdir2" ;;
	11) path="/path/to/dir1/subdir3" ;;
	[Qq]) clear ; exit ;;
	*)tput cup 14 9; echo "Invalid Selection..." ; path="" ;;
esac
echo $path
sleep 3
done


Last edited by wisecracker; 07-10-2014 at 02:53 PM..
# 3  
Old 07-10-2014
Thank you for helping me better understand what was occurring.
I made the changes and it works perfectly.
# 4  
Old 07-10-2014
No problem but you can simplify your loop and eliminate the variable loop=y ...

If your terminal accepts terminal escape sequences you can do away with tput.

I didn't change these as it would have confused the issue but I am glad to have helped...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script won't run because hardware won't produce display

Can anyone offer any advice on how to modify the script below to work on a new system we have, that has no graphics capability? We admin the system through a serial RAS device. I've tried running the below script through the RAS and through an ssh -X session. It failed with something like "GTK... (3 Replies)
Discussion started by: yelirt5
3 Replies

2. Shell Programming and Scripting

Help with 'select' for menu input

A lot of my scripting makes use of the 'select' command to create menu driven input. A typical example of how I use it is as: somevar='' PS3='Select one: ' while ]; do select somevar in $(sqlplus -s $dbuser/$dbpw@mydb <<EOF set echo off feedback off verify off... (7 Replies)
Discussion started by: edstevens
7 Replies

3. Shell Programming and Scripting

Building a dynamic UNIX menu with input files

Hi! I am looking to build dynamic menu (named: lookup.sh) that reads a pipe delimited file for input. for example, contents of input.txt could be: user1|srv1 user3|srv1 user4|srv1 user2|srv2 I want the menu look like: 1) get password for user1 on srv1 2) get password for user3 on... (7 Replies)
Discussion started by: cpolikowsky
7 Replies

4. Shell Programming and Scripting

Execute function as soon as input is provided in menu drive shell script

Hi All, I have a menu driven scripts. As you know while running the script we have to input the option such as 1,2, and 3 to execute function accordingly. but after selecting the input we have to press Enter. My requirement is to execute function as soon as we press the option. Is there... (5 Replies)
Discussion started by: kiran_j
5 Replies

5. Shell Programming and Scripting

Update Statement User menu input screen

Hi Guys, Any good reference for me to perform user database update statement on table which has quite number of fields could be updated depend on user specified column name and the value to assign. All the approaches are welcome and appreciated. Thanks. (1 Reply)
Discussion started by: ckwan123
1 Replies

6. Shell Programming and Scripting

Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed. eg. 1 + 1 = or 1 + 1 + 2 + 1 = Both of these should be possible. #!/bin/bash read -p "what's the first number? " n1... (3 Replies)
Discussion started by: redshine6
3 Replies

7. Shell Programming and Scripting

Plink batch input menu prompt

I am automating voicemail backups and can use plink to login, but I want to pass input to the menu, not the linux shell: This will log me in..... c:\Batch>plink -ssh 10.12.99.64 -l root -pw 9999 I'd like to input an "S" with a "-m" remote command file for this, etc... MAIN MENU... (2 Replies)
Discussion started by: JimCasagrande
2 Replies

8. UNIX for Dummies Questions & Answers

Input A Menu Selection In A Variable

hey all, me again....having a problem with my code, where I essentially am trying to show a menu, have the user select an option (from 1 to 5), then display which selection they picked... #!/bin/bash # A LIST OF THE ERROR MESSAGES AND THE PROPER SYNTAX: error_0="Correct amount of... (1 Reply)
Discussion started by: SoVi3t
1 Replies

9. Red Hat

Server won't boot up ... no GRUB menu

I dont know what the heck happen but one of our DBAs was working on a cluster for oracle. Rebooted the box and viola, the OS won't load. After the POST message, I get a blinking cursor on the upper left hand corner. Doesn't even say "GRUB" or anything, it just blinks. I tried going into rescue... (1 Reply)
Discussion started by: sdotsen
1 Replies

10. AIX

won't mount /usr...won't boot fully

Hello: NOOB here. I attempted to use smit mkcd. Failed on first attempt, not enough space. 2nd attempt tried to place iso on /usr, not enough space there. Cleanup ran for about 5 minutes after aborting. Now AIX won't boot. LCD display on 7029-6E3 says: 0517 MOUNT /USR. Attempted to boot from CD... (11 Replies)
Discussion started by: bbird
11 Replies
Login or Register to Ask a Question