Skip to content

Instantly share code, notes, and snippets.

@antenore
Last active August 29, 2015 14:08

Revisions

  1. @d13r d13r revised this gist Aug 19, 2014. 2 changed files with 10 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    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/)
    10 changes: 10 additions & 0 deletions example.sh → detect-input-type.sh
    Original 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
  2. @d13r d13r revised this gist Mar 3, 2012. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion example.sh
    Original 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
    # Standard input (keyboard)
    # 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
  3. @d13r d13r created this gist Mar 3, 2012.
    5 changes: 5 additions & 0 deletions README.md
    Original 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/)
    7 changes: 7 additions & 0 deletions example.sh
    Original 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