Précédent   Forum des professionnels en informatique > Systèmes > Linux > Applications > Shell
Shell Vos questions sur l'utilisation des commandes shell
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 03/04/2007, 17h26   #1
Membre du Club
 
Inscription : juin 2006
Messages : 206
Détails du profil
Informations forums :
Inscription : juin 2006
Messages : 206
Points : 47
Points : 47
Par défaut initialiser un tableau

Bonsoir,

Je n'arrive pas à initialiser un tableau dans mon script shell et à le parcourir ?

Code :
1
2
3
4
5
6
 
tab={1,5,9,77,66,55,12};
for var in tab
do
echo $tab[var]
done
crazykangourou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/04/2007, 17h51   #2
Membre émérite
 
Avatar de FloMo
 
Inscription : juillet 2004
Messages : 714
Détails du profil
Informations forums :
Inscription : juillet 2004
Messages : 714
Points : 898
Points : 898
Code :
1
2
3
4
5
6
7
#!/bin/sh
 
tab="1 5 9 77 66 55 12"
 
for var in $tab; do
    echo $var
done
Dans un tableau en shell, le séparateur est l'espace.
FloMo est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 03/04/2007, 18h08   #3
Membre expérimenté
 
Avatar de BlaireauOne
 
Inscription : mars 2007
Messages : 469
Détails du profil
Informations personnelles :
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations forums :
Inscription : mars 2007
Messages : 469
Points : 576
Points : 576
Citation:
Envoyé par crazykangourou
Bonsoir,

Je n'arrive pas à initialiser un tableau dans mon script shell et à le parcourir ?

Code :
1
2
3
4
5
6
 
tab={1,5,9,77,66,55,12};
for var in tab
do
echo $tab[var]
done


Un vrai tableau auqel tu peux accéder via un indice :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
$ declare -a tab=(1 5 9 77 66 55 12)
$ echo ${tab[0]}
1
$ echo ${tab[1]}
5
$ind=5
$ echo ${tab[$ind]} #-- Utilisation de la variable "ind" comme indice
55
$ echo ${tab[*]}   #-- Affichage du tableau
1 5 9 77 66 55 12
$ echo ${#tab[*]} #-- Affichage du nombre d'elements dans le tableau
7
BlaireauOne est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 03/04/2007, 18h59   #4
Membre du Club
 
Inscription : juin 2006
Messages : 206
Détails du profil
Informations forums :
Inscription : juin 2006
Messages : 206
Points : 47
Points : 47
Merci !!!

Je précise que j'ai regarder dans la faq developpez, et sur google, mais rien d'explicite.

J'y étais presque.
crazykangourou est déconnecté   Envoyer un message privé Réponse avec citation 01
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 01h12.


 
 
 
 
Partenaires

Hébergement Web