Skip to content

Instantly share code, notes, and snippets.

@antenore
Last active August 29, 2015 14:08
Show Gist options
  • Save antenore/b27dbf0696641b04036f to your computer and use it in GitHub Desktop.
Save antenore/b27dbf0696641b04036f to your computer and use it in GitHub Desktop.

How to detect whether input is from keyboard, a file, or another process.

Useful for writing a script that can read from standard input, or prompt the user for input if there is none.

Source: LinuxQuestions.org

if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
# Pipe input (echo abc | myscript)
elif file $( readlink /proc/$$/fd/0 ) | grep -q "character special"; then
# Standard input (keyboard)
else
# File input (myscript < file.txt)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment