[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Reading input piped to a bash script



On Mon, Oct 18, 2004 at 07:51:49PM +0200, Olle Eriksson wrote:

> I want to be able to receive input both through a pipe, and through 
> parameters to the script. For example:
> 
>   echo "blabla" | myscript.sh
> and
>   myscript.sh "blabla"

Assuming "blabla" is a file name:

	if [ -z "$1" -o ! -f "$1" ]
	then
	    # No (valid) file specified, so we'll take stdin.
	else
	    # File specified, so we'll read it instead of stdin.
	    exec < "$1"
	fi

	while read this that the other # ...
	do
	    ...

If it's just text, then check for its existence and process it, after
which you can read or ignore stdin at your whim.


> The script should be able to check whether or not any input is coming 
> through the pipe, and if not use $1.

What I've listed above does the opposite, but presumably you can accomplish
your goals with that behaviour.

-- 
Mason Loring Bliss    ((  "In the drowsy dark cave of the mind dreams
mason@blisses.org      ))  build  their nest  with fragments  dropped
http://blisses.org/   ((   from day's caravan." - Rabindranath Tagore

Attachment: pgp9VkIulTKiA.pgp
Description: PGP signature


Reply to: