I have a function that takes input from the command line, and, optionally, from stdin. However, it will stop for input when no input stream is specified.
#!/bin/bash
somefunc() {
# output strings on command line.
while (($#)); do
echo "$1"
shift
done
# take input from stdin, if it exists.
declare line
while read -sr line; do
echo "$line"
done
}
somefunc "$@"
This works:
$ somefunc "arg1" "arg2" "..." <"/some/file"
This does not:
$ somefunc "arg1" "arg2" "..."
I can get around this problem by using a timeout on the read command (-t 0.0001 for instance), but this seems a bit kludgy and fragile.
Is there a better way of achieving this without using a timeout?