PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How to create a function or script which can accept either a filename or HERE Document?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to create a function or script which can accept either a filename or HERE Document?

Réponse
 
LinkBack Outils de la discussion
Vieux 24/03/2008, 02h06   #1
panruochen@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to create a function or script which can accept either a filename or HERE Document?

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.
  Réponse avec citation
Vieux 24/03/2008, 02h14   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to create a function or script which can accept either afile name or HERE Document?

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
  Réponse avec citation
Vieux 24/03/2008, 02h22   #3
Bill Marcum
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?

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.
  Réponse avec citation
Vieux 24/03/2008, 04h01   #4
PRC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to create a function or script which can accept either a filename or HERE Document?

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
  Réponse avec citation
Vieux 24/03/2008, 04h19   #5
PRC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to create a function or script which can accept either a filename or HERE Document?

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?
  Réponse avec citation
Vieux 24/03/2008, 04h34   #6
Bill Marcum
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?

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 -
}

  Réponse avec citation
Vieux 25/03/2008, 02h13   #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
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14581 seconds with 15 queries