Skip to content

Instantly share code, notes, and snippets.

@Juul
Created May 5, 2016 13:37
Show Gist options
  • Save Juul/d8408750ef1dd20f9ae60d2d905dbf47 to your computer and use it in GitHub Desktop.
Save Juul/d8408750ef1dd20f9ae60d2d905dbf47 to your computer and use it in GitHub Desktop.
An ash shell script (busybox) for a user prompt with timeout
#!/bin/ash
# This script waits for three seconds for the user to press any key
# that generates a character (other than enter) and otherwise
# proceeds normally
#
# This is an ash shell script for use in e.g. a busybox initrd
# to allow letting the user boot into a rescue environment (e.g. the initrd itself)
#
# This script requires: dd, stty, grep, cut, kill and sleep
kill_dd() {
ddpid=`ps | grep dd | grep -v grep | cut -d ' ' -f 1`
if [ ! -z "$ddpid" ]; then
kill -2 $ddpid 2> /dev/null
fi
}
read_char() {
stty -icanon -echo
(sleep "2" && kill_dd) & pid=$!
trap "kill $pid 2> /dev/null" INT
eval "$1=\$(dd bs=1 count=1 2> /dev/null)"
kill_dd
kill $pid 2> /dev/null
stty icanon echo
}
echo -n "Type 'r' within the next three seconds for rescue mode..."
read_char choice
if [ ! -z "$choice" ]; then
echo ""
echo "Booting into rescue mode!"
else
echo ""
echo "Booting normally..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment