|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi folks,
i'm been scratching my head and wondering what's "wrong" (not wrong, rather not what i expected) with a simple command substitution with cat command. for example, i have a tab separated text file: $ cat foo.txt a b c aa bb cc and i have a shell script: $ cat foo.sh #!/bin/sh for bar in `cat foo.txt`; do echo "hello world" done the execution of this script prints SIX "hello world" rather than TWO as i expected. it is obvious the variable $bar is assigned the value of each single word (or one field in the convention of awk) during each loop and 6 words give 6 loops. while what i need is that a whole line would be assigned to $bar. how can i make shell to do what i just described? thank you very much, David |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
David Wang wrote:
> > for example, i have a tab separated text file: > > $ cat foo.txt > a b c > aa bb cc > > and i have a shell script: > > $ cat foo.sh > #!/bin/sh > > for bar in `cat foo.txt`; do > echo "hello world" > done > > the execution of this script prints SIX "hello world" rather than TWO > as i expected. it is obvious the variable $bar is assigned the value > of each single word (or one field in the convention of awk) during > each loop and 6 words give 6 loops. while what i need is that a whole > line would be assigned to $bar. > > how can i make shell to do what i just described? while read LINE; do echo "hello world: $LINE" done < foo.txt -- Best regards | "The only way to really learn scripting is to write Cyrus | scripts." -- Advanced Bash-Scripting Guide |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Thanks Cyrus, what you said works for me.
D. On Aug 30, 4:33 pm, Cyrus Kriticos <cyrus.kriti...@googlemail.com> wrote: > David Wang wrote: > > > for example, i have a tab separated text file: > > > $ cat foo.txt > > a b c > > aa bb cc > > > and i have a shell script: > > > $ cat foo.sh > > #!/bin/sh > > > for bar in `cat foo.txt`; do > > echo "hello world" > > done > > > the execution of this script prints SIX "hello world" rather than TWO > > as i expected. it is obvious the variable $bar is assigned the value > > of each single word (or one field in the convention of awk) during > > each loop and 6 words give 6 loops. while what i need is that a whole > > line would be assigned to $bar. > > > how can i make shell to do what i just described? > > while read LINE; do > echo "hello world: $LINE" > done < foo.txt > > -- > Best regards | "The only way to really learn scripting is to write > Cyrus | scripts." -- Advanced Bash-Scripting Guide |
|
![]() |
| Outils de la discussion | |
|
|