Afficher un message
Vieux 25/03/2008, 03h13   #7
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to create a function or script which can accept either a file name or HERE Document?

In article
<440773ca-ae43-4c10-ae93-0e1d26f3e507@s19g2000prg.googlegroups.com>,
PRC <panruochen@gmail.com> wrote:

> On 3ÔÂ24ÈÕ, ÉÏÎç9ʱ22·Ö, Bill Marcum <marcumb...@bellsouth.net> wrote:
> > On 2008-03-24, panruoc...@gmail.com <panruoc...@gmail.com> wrote:
> >
> >
> >
> > > I want to create a function or script, i.e CAT, which can accept
> > > either a file name or HERE Document, as `cat' or most unix command
> > > does.

> >
> > > How should I implement this function? I guess it may be related to I/O
> > > redirection.

> >
> > Test whether there are any arguments [ $# -ne 0 ], or any non-option
> > arguments if your script takes options. If a filename is given, you can
> > redirect input to that file, and perhaps set a variable with the filename.

>
> what is passed to CAT if
> CAT <<EOF
> The red quick fox jumps over a lazy brown goat.
> EOF
> is excuted?


No arguments are passed. The here-document becomes the standard input
for the program. Most Unix file-processing utilities will read from
stdin if they don't get any filename arguments.

If your OS supports /dev/stdin, you can use this to simplify your script:

if [ $# -eq 0 ]
then set /dev/stdin
fi

Then the rest of your script can simply iterate over the filename
arguments.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
  Réponse avec citation
 
Page generated in 0,05130 seconds with 9 queries