Size: 808
Comment: Improved examples and some more explanation.
|
Size: 1869
Comment: "Pause" reading input from the keyboard
|
Deletions are marked like this. | Additions are marked like this. |
Line 6: | Line 6: |
read -sp "Press [enter] to continue..." | # Bash read -p "Press [enter] to continue..." # Bourne echo "Press [enter] to continue..." read junk |
Line 11: | Line 16: |
# Bash | |
Line 12: | Line 18: |
}}} 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 -sn 1 -p "Press any key to continue..." < /dev/tty |
|
Line 16: | Line 28: |
# Bash | |
Line 17: | Line 30: |
if ! read -sn 1 -t 5 -p "Will resume in 5 seconds. Press any key to abort..." | echo -n "Press a key within 5 seconds to cancel." if ! read -sn 1 -t 5 |
Line 22: | Line 36: |
For those of you who just want to pause for a while, regardless of the user's input, use `sleep`: | If you just want to pause for a while, regardless of the user's input, use `sleep`: |
Line 27: | Line 41: |
If you want a fancy countdown on your timed `read`: {{{ # Bash # This function won't handle multi-digit counts. countdown() { local i echo -n $1 sleep 1 for ((i=$1-1; i>=1; i--)); do printf "\b%d" $i sleep 1 done } echo 'Warning!!' echo -n 'Five seconds to cancel: ' countdown 5 & pid=$! if ! read -s -n 1 -t 5; then echo; echo "boom" else kill $pid; echo; echo "phew" 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.) |
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 -sn 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 -sn 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 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 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 echo -n $1 sleep 1 for ((i=$1-1; i>=1; i--)); do printf "\b%d" $i sleep 1 done } echo 'Warning!!' echo -n 'Five seconds to cancel: ' countdown 5 & pid=$! if ! read -s -n 1 -t 5; then echo; echo "boom" else kill $pid; echo; echo "phew" 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.)