Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read Command in Bourne?

2 views
Skip to first unread message

gilgantic

unread,
Mar 6, 2003, 3:03:28 AM3/6/03
to
Is it possible to display a default value when using the "read" command? For
instance in the code below is it possible to modify this so that the variable
"answer" will display the word "fine", by default. Then the user can edit as
needed.

echo "How are you?"
read answer

loop

unread,
Mar 6, 2003, 3:20:57 AM3/6/03
to
do you mean if the user enter "null" value for `read'?
if it is , it should work

read
: ${REPLY:=fine}
you may change it by echo ${REPLY:=fine} to display it
if $REPLY is null, the word `fine' is assign to $REPLY

forgive my poor english
gilgantic ??:


--
If pro is the opposite of con, what is the opposite of progress?

Adage from fortune

Stephane CHAZELAS

unread,
Mar 6, 2003, 3:51:01 AM3/6/03
to

Not with "read". You'll need to get or implement a "line
editor". zsh has the vared buitlin command:

answer=fine
vared -p "How are you? " answer

--
Stéphane

Hanspeter 'Happl' Oberlin

unread,
Mar 6, 2003, 9:18:34 AM3/6/03
to
In article <d6052717.03030...@posting.google.com>,
gilg...@yahoo.com (gilgantic) wrote:

Try something like the following:

DEFAULTINPUT=default
echo "INPUT = [$DEFAULTINPUT] ? \c"
read USERINPUT
case "$USERINPUT" in
"") INPUT="$DEFAULTINPUT" ;;
*) INPUT="$USERINPUT" ;;
esac ;;

Greetings from Switzerland -- Gruesse aus der Schweiz
Happl

Ben

unread,
Mar 6, 2003, 9:50:31 AM3/6/03
to

It is kind of possible but you have to mess about with display
properties so it may not work on everyones terminal. You need to do
something like this (tested on Linux)..
(Also assumes that you know where you are putting your question on the
display)

#!/bin/ksh

alias CUP="tput cup" # row col
ans=fine

clear
msg="How are you?"
echo "$msg $ans"

# reposition cursor to start of default answer
CUP 0 $((${#msg}+1))
read ans2

# handle case where user overwrote some of the default, but not all
ans2="$ans2$(expr substr $ans $((${#ans2} + 1)) ${#ans})"

echo "Answer was: $ans2"

regards,
Ben


Ben

unread,
Mar 6, 2003, 10:04:11 AM3/6/03
to
Ben wrote:
> # handle case where user overwrote some of the default, but not all
> ans2="$ans2$(expr substr $ans $((${#ans2} + 1)) ${#ans})"

If you are using bash you can do:
ans2=$ans2${ans:${#ans2}:${#ans} }

but you also would have to do a:
shopt -s expand_aliases
to handle the alias (though you don't have to use aliases)

regards,
Ben

Stephane CHAZELAS

unread,
Mar 6, 2003, 11:13:11 AM3/6/03
to
On Thu, 06 Mar 2003 09:50:31 -0500, Ben <tota...@hotmail.com> wrote:
[...]

> msg="How are you?"
> echo "$msg $ans"
>
> # reposition cursor to start of default answer
> CUP 0 $((${#msg}+1))
> read ans2
>
> # handle case where user overwrote some of the default, but not all
> ans2="$ans2$(expr substr $ans $((${#ans2} + 1)) ${#ans})"
>
> echo "Answer was: $ans2"
[...]

I posted this once in fr.comp.os.unix, to be used for example as

LE "How are you ? [.....]\b\b\b\b\b\b" 5 . fine
echo "Answer was: $REPLY"

LE() {
# shell Line Editor. Extremely slow and stupid code. However it
# should work on ansi/vt100/linux derived terminals on POSIX
# systems.
# Understands some emacs key bindings: CTRL-(A,B,D,E,F,H,K,L)
# plus the CTRL-W and CTRL-U normal killword and kill.
# no Meta-X key, but handling of <Left>, <Right>, <Home>, <End>
# <Suppr>.
#
# Args:
# [1]: prompt (\x sequences recognized, defaults to "")
# [2]: max input length (unlimited if < 0, (default))
# [3]: fill character when erasing (defaults to space)
# [4]: initial value.
# Returns:
# 0: OK
# 1: od(d) error or CTRL-C hit

LE_prompt=$1
LE_max=${2--1}
LE_fill=${3-" "}

LE_backward() {
LE_s=$1
while [ "x$LE_s" != x ]; do
printf \\b$2
LE_s=${LE_s%?}
done
}

LE_restore='stty "$LE_tty"
LC_COLLATE='${LC_COLLATE-"; unset LC_COLLATE"}
LE_ret=1 LE_tty=$(stty -g) LE_px=$4 LE_sx= LC_COLLATE=C

stty -icanon -echo -isig min 100 time 1 -istrip
printf '%b%s' "$LE_prompt" "$LE_px"

while set -- $(dd bs=100 count=1 2> /dev/null | od -vAn -to1); do
while [ $# -gt 0 ]; do
LE_k=$1
shift
if [ "$LE_k" = 033 ]; then
case "$1$2$3" in
133103*|117103*) shift 2; LE_k=006;;
133104*|117104*) shift 2; LE_k=002;;
133110*|117110*) shift 2; LE_k=001;;
133120*|117120*) shift 2; LE_k=004;;
133106*|117106*) shift 2; LE_k=005;;
133061176) shift 3; LE_k=001;;
133064176) shift 3; LE_k=005;;
133063176) shift 3; LE_k=004;;
133*|117*)
shift
while [ 0$1 -ge 060 -a 0$1 -le 071 -o 0$1 -eq 073 ]; do
shift
done;;
esac
fi

case $LE_k in
001) # beginning of line
LE_backward "$LE_px"
LE_sx=$LE_px$LE_sx
LE_px=;;
002) # backward
if [ "x$LE_px" = x ]; then
printf \\a
else
printf \\b
LE_tmp=${LE_px%?}
LE_sx=${LE_px#"$LE_tmp"}$LE_sx
LE_px=$LE_tmp
fi;;
003) # CTRL-C
break 2;;
004) # del char
if [ "x$LE_sx" = x ]; then
printf \\a
else
LE_sx=${LE_sx#?}
printf '%s\b' "$LE_sx$LE_fill"
LE_backward "$LE_sx"
fi;;
012|015) # NL or CR
LE_ret=0
break 2;;
005) # end of line
printf %s "$LE_sx"
LE_px=$LE_px$LE_sx
LE_sx=;;
006) # forward
if [ "x$LE_sx" = x ]; then
printf \\a
else
LE_tmp=${LE_sx#?}
LE_px=$LE_px${LE_sx%"$LE_tmp"}
printf %s "${LE_sx%"$LE_tmp"}"
LE_sx=$LE_tmp
fi;;
010|177) # backspace or del
if [ "x$LE_px" = x ]; then
printf \\a
else
printf '\b%s\b' "$LE_sx$LE_fill"
LE_backward "$LE_sx"
LE_px=${LE_px%?}
fi;;
013) # kill to end of line
LE_tmp=
while [ "x$LE_sx" != x ]; do
LE_tmp=$LE_tmp$LE_fill
LE_sx=${LE_sx#?}
done
printf %s "$LE_tmp"
LE_backward "$LE_tmp";;
014) # redraw
printf '\r%b%s' "$LE_prompt" "$LE_px$LE_sx"
LE_backward "$LE_sx";;
025) # kill line
printf '\r%b' "$LE_prompt"
LE_px=
LE_sx=;;
027) # kill word
if [ "x$LE_px" = x ]; then
printf \\a
else
LE_tmp=${LE_px% *}
[ "x$LE_px" = "x$LE_tmp" ] && LE_tmp=
LE_backward "${LE_px#"$LE_tmp"}" "${LE_fill}\\b"
LE_px=$LE_tmp
fi;;
[02][4-7]?|[13]??)
if [ $LE_max -ge 0 ] && LE_tmp=$LE_px$LE_sx \
&& [ ${#LE_tmp} -eq $LE_max ]; then
printf \\a
else
LE_px=$LE_px$(printf '%b' "\\0$LE_k")
printf '%b%s' "\\0$LE_k" "$LE_sx"
LE_backward "$LE_sx"
fi;;
*)
printf \\a;;
esac
done
done
eval "$LE_restore"
REPLY=$LE_px$LE_sx
echo
return $LE_ret
}

--
Stéphane

0 new messages