bash script read pipe or argument

bash, linux, pipe, stdin

Solution

What about checking the argument first?

if (($#)) ; then
     process "$1"
else
     cat | process
fi

Or, just take advantage from the same behaviour of `cat`:

cat "$@" | process

Problem

I want my script to read a string either from stdin , if it's piped, or from an argument. So first i want to check if some text is piped and if not it should use an argument as input. My code looks something like this: ``` value=$(cat) # read from stdin if [ "$pipe" != "" ]; then #check if pipe is not empty #Do something with pipe string else #Do something with argument string fi ``` The problem is when it's not piped, then the script will halt and wait for "ctrl d" and i dont want that. Any suggestions on how to solve this? Thanks in advance. /Tomas

Original source

Related problems