See this answer below for a zsh solution
Let's say I have the following in my ~/.bash_aliases
to ask for confirmation before suspending the system:
function suspend()
{ #
echo "Please confirm/cancel system suspension:"
select confirmation in "confirm" "cancel"; do
case ${confirmation} in
confirm )
echo "System suspending..."
systemctl suspend
break;;
cancel )
echo "Canceled suspension."
break;;
esac
done
}
I would like systemctl suspend
to be still executed if no answer is given by the user. For example, after 10 seconds without user input, the content of the "confirm" case would be executed.
I tried the following, with a backgrounded sleep
in a subshell:
function suspend()
{ #
flag_cancel=0
echo "Please confirm/cancel system suspension:"
(
sleep 10 &&
if [ $flag_cancel -eq 0 ]; then
echo "System suspending..."
systemctl suspend
fi &
)
select confirmation in "confirm" "cancel"; do
case ${confirmation} in
confirm )
echo "System suspending..."
systemctl suspend
break;;
cancel )
flag_cancel=1
echo "Canceled suspension."
break;;
esac
done
}
but a change of the value of flag_cancel
is not taken into account, so the command is always executed after the sleep
.
How to achieve what I want?