Boucle FOR dans un script Bash
Résolu
pcsystemd
Messages postés
734
Statut
Membre
-
pcsystemd Messages postés 734 Statut Membre -
pcsystemd Messages postés 734 Statut Membre -
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
les requêtes qui sont construites ne sont pas correcte puisque la chaine n'est pas prise complétement :
hors je souhaite avoir :
Avez vous une idée pour m'aider?
Merci
L'accès au savoir est la première liberté que chaque homme devrait avoir.
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:
- Boucle FOR dans un script Bash
- Downloader for pc - Télécharger - Téléchargement & Transfert
- Script vidéo youtube - Guide
- Mas script - Accueil - Windows
- Bingo bash free - Télécharger - Divers Jeux
- Idm for mac - Télécharger - Téléchargement & Transfert
4 réponses
Salut,
Les quotes sont la clé du problème :
;-))
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
$
;-))
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 :
ce qui ne va pas.
Si je traiter le séparateur au niveau du fichier avec ISF?
Qu'en penses tu?
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?
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 :
Si tu tiens à conservé la variable, ceci marche aussi :
;-))
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}"
;-))
j'ai essayé avec while mais du coup j'ai plus rien :
J'ai pas du comprendre. Voila ce que j'ai dans mon script :
C'est bien ça?
Merci
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