On 6/4/14, Marcus MERIGHI <mcmer-open...@tor.at> wrote:
> Hello,
>
> In my attempts to write a simple script that lets the user select
> options with a single key stroke I found no other way than to use bash
> and its built-in read command with -n 1.
>
> I am looking for a way to do this in ksh(1). Any ideas? Please...

maybe something like this:

--- 8< ---
#!/bin/sh

# TODO handle signals!
stty -icanon

# ask question
echo "1. Sing a song."
echo "2. Read a book."
echo "3. Go to sleep."
echo -n "Make a choice: "
# read answer
ans=`dd count=1 2>/dev/null`

# TODO validation

# print answer
echo ""
echo "ans=$ans"

# reset
stty icanon
--- >8 ---

--patrick


> Some snippets from the bash(1) man page:
>
> read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p
>         prompt] [-t timeout] [-u fd] [name ...]
>         One line is read from the standard input, or from the file [...]
>         -n nchars
>                 read returns after reading nchars characters rather than
>                 waiting for a complete line of input, but honor a
>                 delimiter if fewer than nchars characters are read
>                 before the delimiter.
>
> Thanks in advance, Marcus

Reply via email to