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 > Setting a variable via STDIN
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Setting a variable via STDIN

Réponse
 
LinkBack Outils de la discussion
Vieux 22/08/2006, 15h16   #1 (permalink)
mr.bmonroe@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Setting a variable via STDIN

Hey all,

I have a script (using ksh88) that takes in STDIN and stores it within
a variable.

Is it more apropriate to use 'cat' to do this ala:

FOO=$(cat)

or is it acceptabe (prefered) to do it this way:

FOO=$(</dev/fd/0)

This is how I am doing it now but am I setting my self up for trouble
in the future?

Thanks
--Brett

  Réponse avec citation
Vieux 22/08/2006, 15h59   #2 (permalink)
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting a variable via STDIN

On 22 Aug 2006 07:16:08 -0700, mr.bmonroe@gmail.com
<mr.bmonroe@gmail.com> wrote:
> Hey all,
>
> I have a script (using ksh88) that takes in STDIN and stores it within
> a variable.
>
> Is it more apropriate to use 'cat' to do this ala:
>
> FOO=$(cat)
>
> or is it acceptabe (prefered) to do it this way:
>
> FOO=$(</dev/fd/0)
>
> This is how I am doing it now but am I setting my self up for trouble
> in the future?
>

The usual way is "read FOO", unless you want FOO to contain more than
one line. If that is the case, remind the user to press ctrl-D at the
end.


--
We must die because we have known them.
-- Ptah-hotep, 2000 B.C.
  Réponse avec citation
Vieux 22/08/2006, 16h27   #3 (permalink)
mr.bmonroe@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting a variable via STDIN

Bill Marcum wrote:
> On 22 Aug 2006 07:16:08 -0700, mr.bmonroe@gmail.com
> <mr.bmonroe@gmail.com> wrote:
> > Hey all,
> >
> > I have a script (using ksh88) that takes in STDIN and stores it within
> > a variable.
> >
> > Is it more apropriate to use 'cat' to do this ala:
> >
> > FOO=$(cat)
> >
> > or is it acceptabe (prefered) to do it this way:
> >
> > FOO=$(</dev/fd/0)
> >
> > This is how I am doing it now but am I setting my self up for trouble
> > in the future?
> >

> The usual way is "read FOO", unless you want FOO to contain more than
> one line. If that is the case, remind the user to press ctrl-D at the
> end.


Bill,

Two things (sorry I didn't mension them in my first email):

First, the script is not interactive, it is called by a 3rd party
program that spews a report to STDOUT.

Second, the output is multilined and can be anywhere from 10 to 50+
lines long. I want to map the text to a variable so as to avoid
parsing a temp file over and over (with the rest of the script). The
parsing is simple so I am using expr for the most part.

Thanks
--Brett

  Réponse avec citation
Vieux 22/08/2006, 17h11   #4 (permalink)
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting a variable via STDIN

On 22 Aug 2006 07:16:08 -0700, mr.bmonroe@gmail.com wrote:
> Hey all,
>
> I have a script (using ksh88) that takes in STDIN and stores it within
> a variable.
>
> Is it more apropriate to use 'cat' to do this ala:
>
> FOO=$(cat)
>
> or is it acceptabe (prefered) to do it this way:
>
> FOO=$(</dev/fd/0)
>
> This is how I am doing it now but am I setting my self up for trouble
> in the future?

[...]

FOO=$(cat)

is the correct way. $(<...) is shell dependant and /dev/fd/0 is
not available on all systems.

Please note that will both approaches, all the trailing newline
characters in stdin will be trimmed.

Use

FOO=$(cat; echo .); FOO=${FOO%.}

to work around it.

Another way to do it, less legible but may be faster for small
files:

FOO=
NL='
'
while IFS= read -r line; do
FOO=$FOO$line$NL
done
FOO=$FOO$line

In every case, remember that the NUL character can not be stores
in a shell variable, so you can't process binary files that way.

--
Stephane
  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.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10714 seconds with 12 queries