#1 Le 03/11/2014, à 20:25
- ermann
Problème de remplissage de tableaux sous ubuntu 14.04
Bonjour à tous!
S'il vous plait j'ai un problème avec la manipulation des tableaux sous mon shell. Ma version d'ubuntu est la 14.04.
Je remplis le tableau dans la boucle et hors de la boucle le tableau parait vide.
J'ai aussi essayé d'afficher le tableau dans la boucle et tout semblait correct, mais une fois hors de la boucle cela paraissait toujours vide et je ne voit pas ou est le problème.
Voici mon code et plus bas vous avez le resultat.
#!/bin/bash
index=0
ls -l | while read -r ; do
echo "Merci Reply = $REPLY"
words[index]="$REPLY"
index=$(expr $index + 1)
done
echo "words = ${words[@]}"
for i in $(seq 0 $(expr $index - 1)); do
echo ${words[$i]}
done
Voici le résultat: ce qui est absurde.
Merci Reply = total 112
Merci Reply = -rwxrwxr-x 1 erman erman 656 nov. 3 15:45 conversion.sh
Merci Reply = -rw-r--r-- 1 erman erman 50421 nov. 3 03:31 ctcSim.vcf
Merci Reply = -rw-r--r-- 1 erman erman 20619 nov. 3 03:32 SimContacts0000.vcf
Merci Reply = -rw-rw-r-- 1 erman erman 84 nov. 3 11:23 SimContacts.vcf
Merci Reply = -rw-rw-r-- 1 erman erman 20619 nov. 3 11:13 SimContacts.vcf~
Merci Reply = -rwxrwxr-x 1 erman erman 233 nov. 3 19:08 tableau.sh
words =
Dernière modification par ermann (Le 03/11/2014, à 20:28)
Hors ligne
#2 Le 03/11/2014, à 20:34
- Watael
Re : Problème de remplissage de tableaux sous ubuntu 14.04
salut,
Je remplis le tableau dans la boucle et hors de la boucle le tableau parait vide.
le pipe initie un sous-interpréteur en dehors duquel les variables qui y sont créées n'existent pas.
si tu utilises `ls' pour simplement obtenir la liste des fichiers d'un répertoire, tu dois plutôt te servir des globs de bash par exemple :
repertoire=~
listeFichiers=( $repertoire/* )
c'est tout.
Connected \o/
Welcome to sHell. · eval is evil.
Hors ligne
#3 Le 03/11/2014, à 20:47
- pingouinux
Re : Problème de remplissage de tableaux sous ubuntu 14.04
Bonsoir,
La solution de Watael est la plus simple, mais voici une façon de faire la boucle pour récupérer le tableau à l'extérieur :
while read -r ; do
.................
done < <(ls -l)
Hors ligne
#4 Le 04/11/2014, à 12:36
- ermann
Re : Problème de remplissage de tableaux sous ubuntu 14.04
Merci pour l'enseignement Watael.
En fait j'ai assayé la solution de pingouinux et tout fonctionne bien, Merci encore.
voici la version qui fonctionne:
#!/bin/bash
index=0
while read -r ; do
echo "Merci Reply = $REPLY"
words[index]="$REPLY"
index=$(expr $index + 1)
done < <(ls -l)
echo "word = ${words[@]}"
for i in $(seq 0 $(expr $index - 1)); do
echo ${words[$i]}
done
Ce qui suit est le resultat.
Merci Reply = total 184
Merci Reply = -rwxrwxr-x 1 erman erman 1667 nov. 4 08:51 conversion.sh
Merci Reply = -rw-r--r-- 1 erman erman 50477 nov. 4 06:28 ctcSim.vcf
Merci Reply = -rw-r--r-- 1 erman erman 38980 nov. 4 06:25 ctcSim.vcf~
Merci Reply = -rw-rw-r-- 1 erman erman 14482 nov. 4 09:00 MySimContact.vcf
Merci Reply = -rw-rw-r-- 1 erman erman 15405 nov. 4 08:56 MySimContact.vcf~
Merci Reply = -rw-r--r-- 1 erman erman 20619 nov. 3 03:32 SimContacts0000.vcf
Merci Reply = -rw-rw-r-- 1 erman erman 84 nov. 3 11:23 SimContacts.vcf
Merci Reply = -rw-rw-r-- 1 erman erman 20619 nov. 3 11:13 SimContacts.vcf~
Merci Reply = -rwxrwxr-x 1 erman erman 234 nov. 4 11:20 tableau.sh
word = total 184 -rwxrwxr-x 1 erman erman 1667 nov. 4 08:51 conversion.sh -rw-r--r-- 1 erman erman 50477 nov. 4 06:28 ctcSim.vcf -rw-r--r-- 1 erman erman 38980 nov. 4 06:25 ctcSim.vcf~ -rw-rw-r-- 1 erman erman 14482 nov. 4 09:00 MySimContact.vcf -rw-rw-r-- 1 erman erman 15405 nov. 4 08:56 MySimContact.vcf~ -rw-r--r-- 1 erman erman 20619 nov. 3 03:32 SimContacts0000.vcf -rw-rw-r-- 1 erman erman 84 nov. 3 11:23 SimContacts.vcf -rw-rw-r-- 1 erman erman 20619 nov. 3 11:13 SimContacts.vcf~ -rwxrwxr-x 1 erman erman 234 nov. 4 11:20 tableau.sh
total 184
-rwxrwxr-x 1 erman erman 1667 nov. 4 08:51 conversion.sh
-rw-r--r-- 1 erman erman 50477 nov. 4 06:28 ctcSim.vcf
-rw-r--r-- 1 erman erman 38980 nov. 4 06:25 ctcSim.vcf~
-rw-rw-r-- 1 erman erman 14482 nov. 4 09:00 MySimContact.vcf
-rw-rw-r-- 1 erman erman 15405 nov. 4 08:56 MySimContact.vcf~
-rw-r--r-- 1 erman erman 20619 nov. 3 03:32 SimContacts0000.vcf
-rw-rw-r-- 1 erman erman 84 nov. 3 11:23 SimContacts.vcf
-rw-rw-r-- 1 erman erman 20619 nov. 3 11:13 SimContacts.vcf~
-rwxrwxr-x 1 erman erman 234 nov. 4 11:20 tableau.sh
Hors ligne
#5 Le 04/11/2014, à 13:00
- pingouinux
Re : Problème de remplissage de tableaux sous ubuntu 14.04
Tu peux même simplifier un peu
#!/bin/bash
index=0
while read -r ; do
echo "Merci Reply = $REPLY"
words[((index++))]="$REPLY"
done < <(ls -l)
echo "words = ${words[@]}"
for i in "${words[@]}"; do
echo "$i"
done
Hors ligne