bourne shell user interaction
bourne shell user interaction
am 15.02.2006 18:04:17 von bobfob
Hi,
I'm restricted to using the bourne shell under aix and can't install
additional utilities.
I have a script which loops but I'd like it to see if the user wants to
interrupt the loop before looping - without a ctrl-c.
something like
press F within the next 5 seconds to terminate this script
if no key is pressed, it loops.
I think read and expect can't timeout so I couldn't see how they could
be useful.
any ideas?
Thanks,
Nick.
Re: bourne shell user interaction
am 15.02.2006 18:29:10 von Stephane CHAZELAS
On 15 Feb 2006 09:04:17 -0800, bobfob wrote:
> Hi,
>
> I'm restricted to using the bourne shell under aix and can't install
> additional utilities.
That must be a very old AIX. AIX doesn't have the Bourne shell
anymore. Maybe you mean a Unix shell or POSIX shell as /bin/sh.
> I have a script which loops but I'd like it to see if the user wants to
> interrupt the loop before looping - without a ctrl-c.
>
> something like
>
> press F within the next 5 seconds to terminate this script
>
> if no key is pressed, it loops.
>
> I think read and expect can't timeout so I couldn't see how they could
> be useful.
[...]
expect times out. You can setup the terminal so that a read on
it can timeout:
stty -icanon time 50 min 0; read
read will return with a non-zero exit status if one doesn't type
anything for 5 seconds.
if expect -c '
set timeout 5
expect_user {
"\n" {exit 0}
timeout {exit 1}
}'
then echo something entered
else timeout reached
fi
--
Stephane
Re: bourne shell user interaction
am 15.02.2006 19:31:34 von Sven Mascheck
Stephane Chazelas wrote:
>> I'm restricted to using the bourne shell under aix
> That must be a very old AIX. AIX doesn't have the Bourne shell
> anymore. Maybe you mean a Unix shell or POSIX shell as /bin/sh.
(Isn't "Unix" shell as confusing as "Bourne" shell?)
BTW: AIX provides the (SVR3) Bourne shell until today, as /bin/bsh,
(but i doubt it was meant.)
Re: bourne shell user interaction
am 15.02.2006 20:28:13 von Stephane CHAZELAS
2006-02-15, 19:31(+01), Sven Mascheck:
> Stephane Chazelas wrote:
>
>>> I'm restricted to using the bourne shell under aix
>
>> That must be a very old AIX. AIX doesn't have the Bourne shell
>> anymore. Maybe you mean a Unix shell or POSIX shell as /bin/sh.
>
> (Isn't "Unix" shell as confusing as "Bourne" shell?)
Yes, probably. Unix conformant shell may be better.
> BTW: AIX provides the (SVR3) Bourne shell until today, as /bin/bsh,
> (but i doubt it was meant.)
Yes. Do you know when was the last time AIX had a Bourne shell
as /bin/sh?
--
Stéphane
Re: bourne shell user interaction
am 15.02.2006 22:11:12 von Sven Mascheck
Stephane CHAZELAS wrote:
>>> That must be a very old AIX.
> Do you know when was the last time AIX had a Bourne shell as /bin/sh?
It was swapped with the transition from AIX 3 to 4 (mid of '94)
(Yet, i interestingly know a still productive AIX 3.2.5.)
Re: bourne shell user interaction
am 15.02.2006 23:41:13 von bobfob
Thanks for the answer Stephane and I'm glad it sparked some nostalgia!
(you're right I was mistaken re. the shell version, it's csh)
Nick.
Sven Mascheck wrote:
> Stephane CHAZELAS wrote:
>
> >>> That must be a very old AIX.
>
> > Do you know when was the last time AIX had a Bourne shell as /bin/sh?
>
> It was swapped with the transition from AIX 3 to 4 (mid of '94)
> (Yet, i interestingly know a still productive AIX 3.2.5.)
Re: bourne shell user interaction
am 16.02.2006 08:10:42 von conrads
In article <1140023057.828392.86950@o13g2000cwo.googlegroups.com>,
bobfob wrote:
>
>
>Hi,
>
>I'm restricted to using the bourne shell under aix and can't install
>additional utilities.
>
>I have a script which loops but I'd like it to see if the user wants to
>interrupt the loop before looping - without a ctrl-c.
>
>something like
>
>press F within the next 5 seconds to terminate this script
>
>if no key is pressed, it loops.
>
>I think read and expect can't timeout so I couldn't see how they could
>be useful.
>
>any ideas?
You could call the following script from your main script, like so:
char=$(readkey $timeout)
If the exit code is 0, the user pressed a key within the timeout period.
If the exit code is 1, no key was pressed.
#!/bin/sh
#
# readkey
#
# read a single key from the keyboard, with optional timeout
#
# echoes the character read to stdout
#
# returns zero on successful read or 1 on read timeout
#
timeout=0 # default timeout (none)
if [ $# -eq 1 ]
then
timeout=$1
elif [ $# -ne 0 ]
then
echo "Error: too many arguments to readkey"
exit 1
fi
# get tty name
input=$(who am i | awk '{print $2}')
if [ $timeout -eq 0 ] # not using a timeout, dd outputs to stdout
then
stty raw
c=`dd if=/dev/$input bs=1 count=1 2>/dev/null`
stty cooked
echo -n $c
exit 0
else # timeout in effect
stty raw
# run dd as a background job so we can do the timeout countdown
# save dd output to a temp file
dd if=/dev/${input} of=/tmp/readkey.$$ bs=1 count=1 2>/dev/null &
dd_pid=$!
while [ $timeout -gt 0 ]
do
if ps -p ${dd_pid} >/dev/null 2>&1 # dd still running?
then
sleep 1
timeout=$((timeout - 1))
else # dd finished
stty cooked
cat /tmp/readkey.$$ 2>/dev/null
rm -f /tmp/readkey.$$
exit 0
fi
done
# we land here if the timeout elapsed
if ps -p ${dd_pid} >/dev/null 2>&1 # dd still running?
then
stty cooked
exit 1
else # dd finished
stty cooked
cat /tmp/readkey.$$ 2>/dev/null
rm -f /tmp/readkey.$$
exit 0
fi
fi
--
Conrad J. Sabatier -- "In Unix veritas"