Afficher un message
Vieux 22/08/2006, 17h11   #4
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
 
Page generated in 0,04457 seconds with 9 queries