prefilling keyboard buffer

2007-06-16 Thread greno

How can the keyboard buffer be prefilled with an answer for a read command?
I want to prompt the user with a question using the read command and then
suggest an answer that they could backspace over and change if necessary.  
example:
[code] read -p "Please enter quantity: " qty [/code]
Please enter quantity: 27   # this is what I want the user to see and they
could backspace over the 27 and enter another number if necessary.
I've tried echoing characters out to /dev/tty but you cannot backspace over
these and they are not present in the answer variable from the user.  And
yes I know you can use selects and other methods but that is not what I
want.  I want this method to work.

-- 
View this message in context: 
http://www.nabble.com/prefilling-keyboard-buffer-tf3931493.html#a11150900
Sent from the Gnu - Bash mailing list archive at Nabble.com.



___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: prefilling keyboard buffer

2007-06-16 Thread Bob Proulx
greno wrote:
> How can the keyboard buffer be prefilled with an answer for a read command?
> I want to prompt the user with a question using the read command and then
> suggest an answer that they could backspace over and change if necessary.  

In order to do this a helper application is usually needed such as
'dialog' or 'whiptail' or some such.  The helper application
implements full terminal editing in order to achive that behavior.
Those work fine but create a dependency that personally would avoid if
I were writing a script to be used on multiple systems.  If I know
that I will always have dialog available (such as on Debian systems)
then I use it.  But on HP-UX or AIX for counter examples then
requiring dialog to be present is an extra dependency that I would
rather avoid.

I recommend a simpler method.  I recommend placing the default value
in the prompt.  If the user simply enters without entering any data
then use the default value.  If the user enters anything then use
whatever the user entered.  This is in some ways better because the
default value is always present even if the user selects a non-default
value.

I will include an example to illustrate the technique.

Bob

#!/bin/sh

defaultvalue=42
printf "Enter a value [$defaultvalue]: "
read value
if [ -z "$value" ]; then
  value=$defaultvalue
fi

echo "The value is \"$value\""

exit 0


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: prefilling keyboard buffer

2007-06-16 Thread Chris F.A. Johnson
greno wrote:
> How can the keyboard buffer be prefilled with an answer for a read command?
> I want to prompt the user with a question using the read command and then
> suggest an answer that they could backspace over and change if necessary.  

default="Something or other"
history -s "$default"
read -ep "Press up arrow to edit \"$default\": " XX

-- 
   Chris F.A. Johnson  
   ===
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
.


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash