|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|