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
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 | |
# Terminal input (keyboard) | |
else | |
# File input (myscript < file.txt) | |
fi | |
# In Bash you can also use test -t to check for a terminal: | |
if [ -t 0 ]; then | |
# Terminal input (keyboard) - interactive | |
else | |
# File or pipe input - non-interactive | |
fi |