Re: Reading input piped to a bash script
On Mon, 18 Oct 2004 19:51:49 +0200, Olle Eriksson wrote:
> Can anyone explain how I can read input to a bash
> script through a pipe?
> I want to be able to receive input both through a
> and through
> parameters to the script. For example:
> echo "blabla" | myscript.sh
> myscript.sh "blabla"
> The script should be able to check whether or not any
> input is coming
> through the pipe, and if not use $1. I have tried with
> cat and read but
> they always asks the user interactively if nothing is
> piped to the
You should use exec like the following simple example:
exec 6<&0 # Link file descriptor #6 with stdin.
read a1 # read if somethign comes from a pipe
echo $a1 # print the output of the pipe.
# you can make here the validation that you need if not
then read $1
exec 0<&6 6<&- # restore stdin.
I hope this help
Sergio Basurto J.
If I have seen further it is by standing on the
shoulders of giants. (Isaac Newton)