Re: Unix script question (basic)

From: Michael Tosch (eedmit_at_NO.eed.SPAM.ericsson.PLS.se)
Date: 03/05/04


Date: 5 Mar 2004 19:16:45 GMT

In article <20040305134412.10232.00000833@mb-m03.aol.com>, piestiany7@aol.com (Piestiany7) writes:
> Some time ago on these boards someone suggested a good
> way of finding out how many columns there were in a file
>
> read x < $1
> set -- $x
> columns=$#
>
> I have two questions
>
> 1) what effect is set -- $x having (ie in enabling subsequently for $# to have
> meaning)

set sets positional paramenters. The original parameters (arguments passed when
the script was called) are overwritten.
The handling of positional parameters in brief:
$1 is first parameter, $2 is second, ...., and $# is number of paremeters set.

In case $x would start with a -, the set would expect an option. A -- means there
are no further options (analog to rm -- -i which removes the file '-i').

This and more you find in
man sh

>
> 2) if we were to put this into a loop to read every single row in the file and
> output the number of columns, can we use a construct like
> <command> | while read row
>
> if so what can we have for 'command'? cat looks at a field at a time
>

Yes.
cat does nothing on the file contents.
So

cat file | while read row; do; ...; done

works as good as the smarter

while read row; do; ...; done < file

-- 
Michael Tosch
IT Specialist
HP Managed Services Germany
Phone +49 2407 575 313
Mail: michael.tosch@hp.com


Relevant Pages

  • Unix script question (basic)
    ... if we were to put this into a loop to read every single row in the file and ... if so what can we have for 'command'? ... cat looks at a field at a time ...
    (comp.unix.questions)
  • Re: Better exponentials
    ... > Better in sense that they will agree with previous measurements I've ... > made on a single row. ... > exponential model to this data, I get value of approximately 0.2 for ... The best material model of a cat is another, or preferably the same, cat. ...
    (comp.soft-sys.matlab)