Boucle FOR dans un script Bash

Résolu/Fermé
pcsystemd Messages postés 691 Date d'inscription dimanche 27 novembre 2005 Statut Membre Dernière intervention 15 janvier 2024 - Modifié par pcsystemd le 18/10/2011 à 10:45
pcsystemd Messages postés 691 Date d'inscription dimanche 27 novembre 2005 Statut Membre Dernière intervention 15 janvier 2024 - 18 oct. 2011 à 11:56
Bonjour,

j'ai un problème dans mon script bash que je vous expose.

j'ai le fichier monfichier.txt contenant ceci :

client : un site global
client : un site global particulier
client : un site privé
client : un site privé particulier


lorsque je souhaite l'utiliser dans ma boucle for comme suite

LIST='cat monfichier.txt' 

for c in ${LIST}; do 
          req="select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='${c}' group by a.id order by a.id;" 
done


les requêtes qui sont construites ne sont pas correcte puisque la chaine n'est pas prise complétement :

select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='\''client'\'' group by a.id order by a.id; 
select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga=''\'':'\'' group by a.id order by a.id; 
select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga=''\''un'\'' group by a.id order by a.id; 
select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga=''\''site'\'' group by a.id order by a.id; 
select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga=''\''global'\'' group by a.id order by a.id;


hors je souhaite avoir :

select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='client : un site global' group by a.id order by a.id; 
select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='client : un site global particulier' group by a.id order by a.id; 
select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='client : un site privé' group by a.id order by a.id; 
select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='client : un site privé particulier' group by a.id order by a.id;


Avez vous une idée pour m'aider?

Merci


L'accès au savoir est la première liberté que chaque homme devrait avoir.
A voir également:

4 réponses

zipe31 Messages postés 36402 Date d'inscription dimanche 7 novembre 2010 Statut Contributeur Dernière intervention 27 janvier 2021 6 407
18 oct. 2011 à 10:52
Salut,

Les quotes sont la clé du problème :

$ cat plop 
client : un site global
client : un site global particulier
client : un site privé
client : un site privé particulier

$ A=$(cat plop)

$ echo $A     # Sans quotes
client : un site global client : un site global particulier client : un site privé client : un site privé particulier

$ echo "$A"     # Avec quotes
client : un site global
client : un site global particulier
client : un site privé
client : un site privé particulier

$ for i in ${A};do echo "${i}";done     # Sans quotes
client
:
un
site
global
client
:
un
site
global
particulier
client
:
un
site
privé
client
:
un
site
privé
particulier

$ for i in "${A}";do echo "${i}";done     # Avec quotes
client : un site global
client : un site global particulier
client : un site privé
client : un site privé particulier

$

;-))
0
pcsystemd Messages postés 691 Date d'inscription dimanche 27 novembre 2005 Statut Membre Dernière intervention 15 janvier 2024 22
18 oct. 2011 à 11:08
Bonjour zipe31,

merci pour ta réponse rapide(toujours là ;-))

il y a de l'amélioration avec ta solution sauf que maintenant j'ai ceci :

select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='\''client : un site global
client : un site global particulier
client : un site privé
client : un site privé particulier'\''  group by a.id order by a.id;


ce qui ne va pas.

Si je traiter le séparateur au niveau du fichier avec ISF?

Qu'en penses tu?
0
zipe31 Messages postés 36402 Date d'inscription dimanche 7 novembre 2010 Statut Contributeur Dernière intervention 27 janvier 2021 6 407
18 oct. 2011 à 11:14
Effectivement j'avais négligé le problème causé par la boucle "for".

Le plus simple est d'utiliser une boucle "while" et le fichier directement plutôt qu'une variable, comme suit :

$ while read line; do echo "ligne = $line";done < plop 
ligne = client : un site global
ligne = client : un site global particulier
ligne = client : un site privé
ligne = client : un site privé particulier

Si tu tiens à conservé la variable, ceci marche aussi :
done <<<"${A}"

;-))
0
pcsystemd Messages postés 691 Date d'inscription dimanche 27 novembre 2005 Statut Membre Dernière intervention 15 janvier 2024 22
18 oct. 2011 à 11:52
j'ai essayé avec while mais du coup j'ai plus rien :

select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='\'''\''  group by a.id order by a.id;


J'ai pas du comprendre. Voila ce que j'ai dans mon script :

while read line; do echo "ligne = $line";done < monfichier.lst
req="select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='${line}' group by a.id order by a.id;" 


C'est bien ça?
Merci
0
zipe31 Messages postés 36402 Date d'inscription dimanche 7 novembre 2010 Statut Contributeur Dernière intervention 27 janvier 2021 6 407
18 oct. 2011 à 11:55
while read line; do 
req="select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='${line}' group by a.id order by a.id;" 
done <  monfichier.lst
0
pcsystemd Messages postés 691 Date d'inscription dimanche 27 novembre 2005 Statut Membre Dernière intervention 15 janvier 2024 22
18 oct. 2011 à 11:56
Oubli je suis fatigué. Je dis n'importe quoi.

Merci

while read line; do 
req="select a.id,count(c.id) from client c, local a where c.idfer=a.id and orga='${line}' group by a.id order by a.id;" 
done < monfichier.lst
0