Boucle FOR dans un script Bash

Résolu
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

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.

4 réponses

  1. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
     
    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
  2. pcsystemd Messages postés 734 Statut Membre 23
     
    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
    1. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
       
      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
  3. pcsystemd Messages postés 734 Statut Membre 23
     
    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
    1. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
       
      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
  4. pcsystemd Messages postés 734 Statut Membre 23
     
    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