Code: Select all
while true; do
read -p "" -n 1 key
case $key in
[xX]) y="y"; break ;;
esac
done
Code: Select all
t=$((SECONDS+5))
while [ $t -lt 5 ]; do
Any ideas?
Thanks
Code: Select all
while true; do
read -p "" -n 1 key
case $key in
[xX]) y="y"; break ;;
esac
done
Code: Select all
t=$((SECONDS+5))
while [ $t -lt 5 ]; do
Code: Select all
SECOND=0
while [ $SECOND -lt 5 ]; do
sleep 1s
SECOND=$((SECOND+1))
done
echo "SECOND is now $SECOND"
Code: Select all
read -p "" -n 1 -t 5 key
Code: Select all
-t timeout
Cause read to time out and return failure if a complete line of input (or a specified number of char‐
acters) is not read within timeout seconds. timeout may be a decimal number with a fractional por‐
tion following the decimal point. This option is only effective if read is reading input from a ter‐
minal, pipe, or other special file; it has no effect when reading from regular files. If read times
out, read saves any partial input read into the specified variable name. If timeout is 0, read
returns immediately, without trying to read any data. The exit status is 0 if input is available on
the specified file descriptor, non-zero otherwise. The exit status is greater than 128 if the time‐
out is exceeded.
Code: Select all
#!/bin/bash
#set -xu
declare -- sleepPID=0
declare -- subprocessPID=0
function my_loop() {
#lsof -p $$
local -- key=''
local -- running=true
printf -- '%s\n' "${FUNCNAME}"
# Handle ctrl-c. Not inherited
trap 'running=false' INT
while ${running}; do
# FD are inherited
read -u 3 -rp "==> " key
case ${key} in
[xX]) y="y"; break ;;
esac
done
}
# Handle ctrl-c
function cleanup() {
while kill -0 ${subprocessPID}; do
kill ${subprocessPID}
sleep 1
done
while kill -0 ${sleepPID}; do
kill ${sleepPID}
sleep 1
done
trap - INT
}
trap cleanup INT
exec 3</dev/tty
my_loop &
subprocessPID=$!
sleep 10s &
sleepPID=$!
printf -- 'Sleep pid is [%s]\n' ${sleepPID}
printf -- 'Subprocess pid is [%s]\n' ${subprocessPID}
wait ${sleepPID}
kill ${subprocessPID}
trap - INT
Code: Select all
read -p "" -n 1 -t 5 key