Bash: allow only Pipe (|) or redirection (<) to be transmitted and show otherwise use

I have a script that takes several arguments.
Now I modified the script to exclude multiple file names and work with them.
I also want this script to be executed when I get input through a pipe (|) or redirected input (<). But I do not want the script to wait for input on the terminal when none of the above three inputs is provided, but rather shows instructions for use.

I am using the following function:

 # PIPED CONTENT if [ "$#" == "0" ]; then READINPUT="1" if [ "x$TEXTINPUT" == x"" ]; then READINPUT=1 TMPFL=`tempfile -m 777` while read data; do echo "${data}" >> $TMPFL done TEXTINPUT="`cat $TMPFL`" rm $TMPFL fi # if [ "x$TEXTINPUT" == x"" ]; then # if [ "$#" == "0" ]; then usage; fi # fi fi 

Any help is appreciated.

Yours faithfully
Nikhil Gupta

+4
source share
2 answers
 if test -t 0; then echo Ignoring terminal input. else process - fi 

The -t test takes a file descriptor as a parameter (0 is stdin) and returns true if it is a terminal.

+6
source

Keep in mind that there are two test commands: the bash built-in command and the test program, which is often installed as / usr / bin / test, part of the coreutils package. These two functions provide the same functions.

 [[ -t 0 ]] 

equivalently

 /usr/bin/test -t 0 

You can run any of the above bash command line commands with the same results.

+1
source

Source: https://habr.com/ru/post/1345821/


All Articles