Last active
August 29, 2015 14:08
Revisions
-
d13r revised this gist
Aug 19, 2014 . 2 changed files with 10 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,9 @@ # 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: http://www.linuxquestions.org/questions/linux-software-2/bash-scripting-pipe-input-to-script-vs.-1-570945/ 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 @@ -6,7 +12,11 @@ else # File input (myscript < file.txt) fi # ALTERNATIVE: # In Bash you can also use test -t to check for a terminal: if [ -t 0 ]; then # Terminal input (keyboard) - interactive else -
d13r revised this gist
Mar 3, 2012 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,14 @@ 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 -
d13r created this gist
Mar 3, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ 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](http://www.linuxquestions.org/questions/linux-software-2/bash-scripting-pipe-input-to-script-vs.-1-570945/) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ 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