Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'No "space bar" goodness' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
No "space bar" goodness
Authored by: dmw on Jul 13, '10 04:17:49PM
I've been using the following script for this. You can close the window by hitting any key, including space.

#!/bin/sh
# This script originates from:
# http://www.unix.com/shell-programming-scripting/59605-trap-key-press-script.html#post302182133
qlmanage -p "$@" &
main=$!

tput smso
echo "Press any key to return \c"
tput rmso
oldstty=`stty -g`
stty -icanon -echo min 1 time 0
dd bs=1 count=1 >/dev/null 2>&1
stty "$oldstty"
echo

kill $main


[ Reply to This | # ]
No "space bar" goodness
Authored by: Vynce on Jul 18, '10 06:51:20PM
That seems like an incredibly complex way of waiting for a keypress. The following script is much simpler and works just as well in my testing. To be honest, I didn't actually test your version of the script. Does it provide some other benefit that I'm just not seeing?
#!/bin/sh

qlmanage -p "$@" &>/dev/null &
QL_PID=$!

echo "Press any key to return"
read -s -n 1

kill $QL_PID


[ Reply to This | # ]
No "space bar" goodness
Authored by: dmw on Jul 19, '10 07:39:08AM
Does it provide some other benefit that I'm just not seeing?
In this venue, you're right, it is overly complicated. Its one benefit over using read -n 1 is that it's technically more portable across UNIXs. On OS X, sh is really bash, which provides the -n option. The real sh doesn't have this option.

But since this is a Mac forum, and the script itself is Mac specific (using qlmanage), your method is preferable. And to be honest, I think it may be unlikely you'd find a modern UNIX variant where sh isn't really bash, so I think I'm going to retire this snippet in favor of yours. Thanks!

[ Reply to This | # ]