|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
panruochen@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. It is unclear what you want. func() { while read -r x do echo "'$x'" done } func <<- EOT Some text in a here document. EOT func < textfile > > How should I implement this function? I guess it may be related to I/O > redirection. What shall the function do? Janis |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 2008-03-24, panruochen@gmail.com <panruochen@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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 3ÔÂ24ÈÕ, ÉÏÎç9ʱ14·Ö, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote: > 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. > > It is unclear what you want. > > func() > { > while read -r x > do echo "'$x'" > done > > } > > func <<- EOT > Some text > in a here document. > EOT > > func < textfile > > > > > How should I implement this function? I guess it may be related to I/O > > redirection. > > What shall the function do? > > Janis I want my function, i.e CAT, act as `cat' can handle either cat 1.txt or cat <<EOF The red quick fox jumps over a lazy brown goat. EOF |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 2008-03-24, 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? If you write a function you can see for yourself: CAT () { echo "Number of arguments: $#" echo -n "Standard input: "; cat - } |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 *** |
|
![]() |
| Outils de la discussion | |
|
|