The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
reappearing menu list using select forever_49ers Shell Programming and Scripting 9 09-13-2006 01:05 PM
Drop Users trfrye UNIX for Dummies Questions & Answers 2 08-31-2005 12:39 PM
dynamic Select menu rawatds Shell Programming and Scripting 2 12-06-2004 02:54 AM
Dynamic select with multiple word menu items domivv Shell Programming and Scripting 5 07-22-2004 05:52 AM
select in ksh, any guidance for bash? Shakey21 UNIX for Dummies Questions & Answers 1 05-09-2002 09:40 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-29-2006
Registered User
 

Join Date: Nov 2005
Location: London
Posts: 14
Drop down menu in bash for timezone select

Is there any way to implement a drop down menu selection in bash?

This is on CDLinux which is a very minimal live CD and I am using it to install an image onto a hard drive. Part of that process is the timezone selection. There are just too many timezones to attempt to use the "select" command. It would just scroll off the screen and be gone.

CDLinux has lynx and I concidered using it and an HTML form but there is no way to post the output to any type of cgi script without a webserver running on the machine.

If not in bash, then are there any handy programs I could compile that would implement a drop down menu selection?
Reply With Quote
Forum Sponsor
  #2  
Old 04-29-2006
Registered User
 

Join Date: Nov 2005
Location: London
Posts: 14
Fixed!

OK, I wrote a script that does near-enough what I wanted to do...

PHP Code:
#!/bin/bash
#Read time zones into array
i=1; for item in `cat timezones.txt`; do tz[$i]="$item"let i=$i+1done
i
=1
echo "Locate timezone using up/down arrow keys then press enter to select."
while [ ]; do
  if [ 
$i -eq 0 ]; then i=1fi
  
if [ ! ${tz[$i]} ]; then let i=i-1fi
  
echo -en "\r                            "
  
echo -en "\r${tz[$i]}"
  
read -sn 1 twiddle
  
case "$twiddle" in
    
"B")
      
let i=i+1
      
;;
    
"A")
      
let i=i-1
      
;;
    
"")
      echo
      echo 
"OK to select ${tz[$i]} (y/N)?"
      
read -sn 1 confirm
      
if [ "$confirm" == "y" -"$confirm" == "Y" ]; then
        
break
      else
        echo 
"Locate timezone using up/down arrow keys then press enter to select."
      
fi
      
;;
   
esac
done
echo
echo 
"Final choice was ${tz[$i]}" 
The above script is fine for newer bash however CDLinux contains a really old version of bash that doesn't support the -en switches for the read command nor does it support arrays. After much faffing I managed to re-write the script to be backward compatible.

PHP Code:
#!/bin/bash
#Read time zones into array
function key_mode() {
  if [ $
-$"on" ]; then
    stty_state
=`stty -g`
    
stty rawstty -echo
  else
    
stty "$stty_state"
  
fi
}
i=1; for item in `cat timezones.txt`; do eval tz$i="$item"let i=$i+1done
i
=1
echo "Locate timezone using up/down arrow keys then press enter to select."
key_mode on
while [ ]; do
  eval 
selection=$tz$i
  
if [ $i -eq 0 ]; then
    i
=1
    
eval selection=$tz$i
  fi
  
if [ ${#selection} -lt 2 ]; then
    
let i=i-1
    
eval selection=$tz$i
  fi
  
echo -en "\r                            "
  
echo -en "\r$selection"
  
twiddle=`dd bs=1 count=1 2>/dev/null`
  
#read -sn 1 twiddle
  
case "$twiddle" in
    
`echo -e "\102"`)
      
let i=i+1
      
;;
    `
echo -e "\101"`)
      
let i=i-1
      
;;
    `
echo -e "\015"`)
      
key_mode off
      
echo
      echo 
"OK to select $selection (y/N)?"
      
key_mode on
      confirm
=`dd bs=1 count=1 2>/dev/null`
      if [ 
"$confirm" "y" -"$confirm" "Y" ]; then
        
break
      else
      
key_mode off
        
echo "Locate timezone using up/down arrow keys then press enter to select."
      
key_mode on
      fi
      
;;
   
esac
done
key_mode off
echo
echo 
"Final choice was $selection" 

Last edited by simonb; 05-01-2006 at 07:25 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0