Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2782 connectés 

  FORUM HardWare.fr
  Linux et OS Alternatifs
  Codes et scripts

  [Bash] Affectation de données dans un tabeau via une boucle

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Bash] Affectation de données dans un tabeau via une boucle

n°845638
3xc4l18ur
question = ( to ) ? be : ! be;
Posté le 24-09-2006 à 18:16:45  profilanswer
 

Bonjour à tous et toutes
 
Voilà j'essai de stocker des lignes dans un tabeau. Ici c'est tout bêtement un grep sur les lignes d'un "ps".

Code :
  1. #!/bin/bash
  2. set -ex
  3. index=0
  4. ps -ef | grep delge | while read ligne; do
  5.         TAB[index]="$ligne"
  6.         echo "---->${#TAB[@]}<----"
  7.         let "index = $index + 1"
  8. done
  9. echo "total des ligne enregistrees : ${#TAB[@]}"
  10. echo
  11. index=0
  12. total=${#TAB[@]}
  13. while [ $index -lt $total ]
  14. do
  15.         echo ${TAB[index]}
  16.         let "index = $index + 1"
  17. done


 
Les echo "---->${#TAB[@]}<----" me disent bien que les lignes sont bien stockées.
Mais une fois sortie de la boucle le tableau est vite :( !!!
J'ai tenté avec les declare -a ou -ax rien n'y fait.
 
Auriez-vous une idée sur le pourquoi du comment de cet echec bashistique ?
 
Le chose plus ou moins drole c'est qu'en KSH le meme code fonctionne correctement.
 
Voici le resultat de l'execution en bash:

Code :
  1. ++ index=0
  2. ++ ps -ef
  3. ++ grep delge
  4. ++ read ligne
  5. ++ TAB[index]=cdelgehi 27284 26907  0 16:09:52 pts/93   0:03 bash
  6. ++ echo '---->1<----'
  7. ---->1<----
  8. ++ let 'index = 0 + 1'
  9. ++ read ligne
  10. ++ TAB[index]=cdelgehi 26907 26219  0 16:09:42 ?        0:07 /opt/local/openssh/sbin/sshd -R
  11. ++ echo '---->2<----'
  12. ---->2<----
  13. ++ let 'index = 1 + 1'
  14. ++ read ligne
  15. ++ TAB[index]=cdelgehi 27593     1  0 16:10:01 ?        0:00 /opt/local/openssh/bin/ssh-agent
  16. ++ echo '---->3<----'
  17. ---->3<----
  18. ++ let 'index = 2 + 1'
  19. ++ read ligne
  20. ++ TAB[index]=cdelgehi 29584 27284  0 18:11:48 pts/93   0:00 bash
  21. ++ echo '---->4<----'
  22. ---->4<----
  23. ++ let 'index = 3 + 1'
  24. ++ read ligne
  25. ++ TAB[index]=cdelgehi 29587 29584  0 18:11:48 pts/93   0:00 bash
  26. ++ echo '---->5<----'
  27. ---->5<----
  28. ++ let 'index = 4 + 1'
  29. ++ read ligne
  30. ++ echo 'total des ligne enregistrees : 0'
  31. total des ligne enregistrees : 0
  32. ++ echo
  33. ++ index=0
  34. ++ total=0
  35. ++ '[' 0 -lt 0 ']'


 
Voici le resultat de l'execution en ksh:

Code :
  1. + index=0
  2. + ps -ef
  3. + grep delge
  4. + read ligne
  5. + TAB[index]=cdelgehi 27284 26907  1 16:09:52 pts/93   0:03 bash
  6. + echo ---->1<----
  7. ---->1<----
  8. + let index = 0 + 1
  9. + read ligne
  10. + TAB[index]=cdelgehi  1503  1501  0 18:13:23 pts/93   0:00 /usr/bin/ksh ./plop.bash
  11. + echo ---->2<----
  12. ---->2<----
  13. + let index = 1 + 1
  14. + read ligne
  15. + TAB[index]=cdelgehi 26907 26219  0 16:09:42 ?        0:07 /opt/local/openssh/sbin/sshd -R
  16. + echo ---->3<----
  17. ---->3<----
  18. + let index = 2 + 1
  19. + read ligne
  20. + TAB[index]=cdelgehi 27593     1  0 16:10:01 ?        0:00 /opt/local/openssh/bin/ssh-agent
  21. + echo ---->4<----
  22. ---->4<----
  23. + let index = 3 + 1
  24. + read ligne
  25. + TAB[index]=cdelgehi  1501 27284  0 18:13:23 pts/93   0:00 /usr/bin/ksh ./plop.bash
  26. + echo ---->5<----
  27. ---->5<----
  28. + let index = 4 + 1
  29. + read ligne
  30. + echo total des ligne enregistrees : 5
  31. total des ligne enregistrees : 5
  32. + echo
  33. + index=0
  34. + total=5
  35. + [ 0 -lt 5 ]
  36. + echo cdelgehi 27284 26907 1 16:09:52 pts/93 0:03 bash
  37. cdelgehi 27284 26907 1 16:09:52 pts/93 0:03 bash
  38. + let index = 0 + 1
  39. + [ 1 -lt 5 ]
  40. + echo cdelgehi 1503 1501 0 18:13:23 pts/93 0:00 /usr/bin/ksh ./plop.bash
  41. cdelgehi 1503 1501 0 18:13:23 pts/93 0:00 /usr/bin/ksh ./plop.bash
  42. + let index = 1 + 1
  43. + [ 2 -lt 5 ]
  44. + echo cdelgehi 26907 26219 0 16:09:42 = 0:07 /opt/local/openssh/sbin/sshd -R
  45. cdelgehi 26907 26219 0 16:09:42 = 0:07 /opt/local/openssh/sbin/sshd -R
  46. + let index = 2 + 1
  47. + [ 3 -lt 5 ]
  48. + echo cdelgehi 27593 1 0 16:10:01 = 0:00 /opt/local/openssh/bin/ssh-agent
  49. cdelgehi 27593 1 0 16:10:01 = 0:00 /opt/local/openssh/bin/ssh-agent
  50. + let index = 3 + 1
  51. + [ 4 -lt 5 ]
  52. + echo cdelgehi 1501 27284 0 18:13:23 pts/93 0:00 /usr/bin/ksh ./plop.bash
  53. cdelgehi 1501 27284 0 18:13:23 pts/93 0:00 /usr/bin/ksh ./plop.bash
  54. + let index = 4 + 1
  55. + [ 5 -lt 5 ]

mood
Publicité
Posté le 24-09-2006 à 18:16:45  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Linux et OS Alternatifs
  Codes et scripts

  [Bash] Affectation de données dans un tabeau via une boucle

 

Sujets relatifs
Bash Unix et installation apache mysql sur Mac OS XRécupération de données effacées
[bash] Fichiers de configuration[bash] Traitement de chaine et manipulation de données
[MySQL] récupération de données[bash] permission non accordée
bash par défautsauvegarde donnees en secours urgent
executer un script bash depuis IE 
Plus de sujets relatifs à : [Bash] Affectation de données dans un tabeau via une boucle


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR