Created
March 29, 2011 09:13
-
-
Save bortzmeyer/892058 to your computer and use it in GitHub Desktop.
Demonstrates how to test if there is data waiting on standard input (otherwise, we may wish to launch $EDITOR)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This script depends on the read builtin and its -t (timeout) | |
# option. It is not standard, we have to test it. | |
# http://www.cyberciti.biz/tips/how-do-i-find-out-what-shell-im-using.html | |
shell=$(ps hp $$|awk '{print $5}') | |
if [ $shell != "zsh" ] && [ $shell != "bash" ] && [ $shell != "ksh" ]; then | |
echo "Sorry, this script requires a shell with support of timeout in read. $shell is unknown to me." >&2 | |
exit 1 | |
fi | |
if ! read -t 0 FIRSTLINE; then | |
echo "Nothing on standard input" | |
else | |
echo "There was something on standard input:" | |
echo $FIRSTLINE | |
cat - | |
fi | |
# Thanks to Yann Droneaud for the tip. | |
# Here is the documentation in zshbuiltins(1): | |
# | |
# -t [ num ] | |
# Test if input is available before attempting to read. If num is present, it | |
# must begin with a digit and will be evaluated to give a number of seconds, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment