Differences between revisions 4 and 10 (spanning 6 versions)
Revision 4 as of 2008-04-30 13:32:04
Size: 1500
Editor: GreyCat
Comment: fancy counter thingy, because someone will want one
Revision 10 as of 2013-04-08 12:28:58
Size: 1892
Editor: geirha
Comment: printf instead of echo -n
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq65)]] <<Anchor(faq65)>>
Line 3: Line 3:
Use the following to wait until the user presses enter:
Line 4: Line 5:
Use the following to wait until the user presses enter:
Line 17: Line 17:
read -sn 1 -p "Press any key to continue..." read -rsn 1 -p "Press any key to continue..."
}}}

Sometimes you need to wait until the user presses any key to continue, but you are already using the "standard input" because (for example) you are using a pipe to feed your script. How do you tell {{{read}}} to read from the keyboard? Unix flexibility is helpful here, you can add "< /dev/tty"
{{{
# Bash
read -rsn 1 -p "Press any key to continue..." < /dev/tty
Line 23: Line 29:
echo "WARNING: You are about to do something stupid."
echo -n "Press a key within 5 seconds to cancel."
if ! read -sn 1 -t 5
printf 'WARNING: You are about to do something stupid.\n'
printf 'Press a key within 5 seconds to cancel.'
if ! read -rsn 1 -t 5
Line 42: Line 48:
  echo -n $1   printf %s "$1"
Line 45: Line 51:
    printf "\b%d" $i     printf '\b%d' "$i"
Line 50: Line 56:
echo 'Warning!!'
echo -n 'Five seconds to cancel: '
printf 'Warning!!\n'
printf 'Five seconds to cancel: '
Line 54: Line 60:
  echo; echo "boom"   printf '\nboom\n'
Line 56: Line 62:
  kill $pid; echo; echo "phew"   kill "$pid"; printf '\nphew\n'
Line 59: Line 65:

Is there a "PAUSE" command in bash like there is in MSDOS batch scripts? To prompt the user to press any key to continue?

Use the following to wait until the user presses enter:

# Bash
read -p "Press [enter] to continue..."

# Bourne
echo "Press [enter] to continue..."
read junk

Or use the following to wait until the user presses any key to continue:

# Bash
read -rsn 1 -p "Press any key to continue..."

Sometimes you need to wait until the user presses any key to continue, but you are already using the "standard input" because (for example) you are using a pipe to feed your script. How do you tell read to read from the keyboard? Unix flexibility is helpful here, you can add "< /dev/tty"

# Bash
read -rsn 1 -p "Press any key to continue..." < /dev/tty

If you want to put a timeout on that, use the -t option to read:

# Bash
printf 'WARNING: You are about to do something stupid.\n'
printf 'Press a key within 5 seconds to cancel.'
if ! read -rsn 1 -t 5
then something_stupid
fi

If you just want to pause for a while, regardless of the user's input, use sleep:

echo "The script is tired.  Please wait a minute."
sleep 60

If you want a fancy countdown on your timed read:

# Bash
# This function won't handle multi-digit counts.
countdown() {
  local i 
  printf %s "$1"
  sleep 1
  for ((i=$1-1; i>=1; i--)); do
    printf '\b%d' "$i"
    sleep 1
  done
}

printf 'Warning!!\n'
printf 'Five seconds to cancel: '
countdown 5 & pid=$!
if ! read -s -n 1 -t 5; then
  printf '\nboom\n'
else
  kill "$pid"; printf '\nphew\n'
fi

(If you test that code in an interactive shell, you'll get "chatter" from the job control system when the child process is created, and when it's killed. But in a script, there won't be any such noise.)

BashFAQ/065 (last edited 2013-04-08 12:28:58 by geirha)