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 > command substitution with cat
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

command substitution with cat

Réponse
 
LinkBack Outils de la discussion
Vieux 30/08/2007, 21h26   #1
David Wang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut command substitution with cat

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

  Réponse avec citation
Vieux 30/08/2007, 21h33   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: command substitution with cat

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
  Réponse avec citation
Vieux 31/08/2007, 17h45   #3
David Wang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: command substitution with cat

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



  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 02h36.


Édité par : vBulletin® version 3.7.3
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,10284 seconds with 11 queries