I am writing an interactive shell script that needs to run on as many systems as possible. Is there an alternative way to implement the following that is compatible with a standard POSIX system?
#! /bin/bash
echo -n "Do you wish to continue? (Y/n) 5 seconds to respond... "
read -n 1 -t 5 answer # accepts a single character, 5 second timeout.
if [ "$answer" = "n" ] || [ "$answer" = "N" ] ; then
echo -e "\nExiting..."
exit
fi
echo -e "\nContinuing with script..."
# More code
The timeout on read
is most important to me (read -t 5
). The one character limit for read is desirable but not essential (read -n 1
).
Ideally, the script would work on POSIX systems and also within bash without having to enable a special POSIX compatibility mode.