开发者

Bash: Only allow Pipe (|) or Redirect(<) to pass data, and show usage otherwise

I have a script which takes in several arguments.

Now, I have modified the script to except multiple file names and operate on them.

I also want this script to execute when I am receiving input via a pipe (|) or a redirected input (<). But, I do not want the script to wait for input on terminal when none of the above three inputs are provided, and rather show usage instructions.

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.

Regards

Nikhil Gupta


if test -t 0; then
    echo Ignoring terminal input.
else
    process -
fi

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


Please be aware that there are two different "test" commands: the built in bash command, and the "test" program which is often installed as /usr/bin/test, part of the coreutils package. The two provide the same functions.

[[ -t 0 ]]

is equivalent to

/usr/bin/test -t 0

You may run either of the above on a "bash" command line, with the same results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜