I want to get the content of stdin in a variable only if something was pipped to the script, the code I'm using is:
input=$(cat)
echo "$@ $input"
Doing echo 'something' | ./script
works as expected, but not pipping anything just makes the script waiting indefinitely for input. How do I ignore empty stdin?
Testing if something was passed to stdin like this before trying to set a variable seems to solve the problem:
if ! [ -t 0 ]; then
input=$(cat)
fi