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 14/04/2011, 01h17   #1
Futur Membre du Club
 
Homme
Technicien réseau
Inscription : avril 2011
Messages : 15
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Nouvelle-Calédonie

Informations professionnelles :
Activité : Technicien réseau
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : avril 2011
Messages : 15
Points : 17
Points : 17
Par défaut Utilisation de eval

Bonjour,
voici le script que j'étudie et qui fonctionne (script trouvé dans l'abs-guide, et qui illustre l'utilisation de eval)
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
**********************
#!/bin/bash
arr0=( 10 11 12 13 14 15 )
arr1=( 20 21 22 23 24 25 )
 
find_member ()
{
eval array_member=\${arr${array_number}[element_number]}
echo Element $array_member
}
 
array_number=1
element_number=5
find_member  # réponse 25
 
exit 0
**********************
Ma question est courte et précise:
je ne comprends pas pourquoi ce script ne fonctionne plus si on remplace la ligne
Code :
eval array_member=\${arr${array_number}[element_number]}
par la ligne
Code :
array_member=${arr${array_number}[element_number]}
Dans ce cas, il y a le message d'erreur suivant
Code :
${arr${array_number}[element_number]}: bad substitution
C'est certainement tout l'intérêt d'eval, mais je ne le comprends pas justement

Merci d'avance
syncope_nc est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 14/04/2011, 01h49   #2
Expert Confirmé Sénior
 
Avatar de N_BaH
 
Inscription : février 2008
Messages : 2 070
Détails du profil
Informations forums :
Inscription : février 2008
Messages : 2 070
Points : 4 153
Points : 4 153
Bonjour,

c'est parcequ'ici
Code :
array_member="${arr${array_number}[element_number]}"
le shell ne sait pas ce qu'il doit substituer (en premier), alors que là
Code :
eval array_member="\${arr${array_number}[element_number]}"
le shell remplace d'abord ce qui lui est accessible : ${array_number}, ensuite eval exécute l'assignation array_member="${arr1[element_number]}", soit array_member="25"
...
?
N_BaH est déconnecté   Envoyer un message privé Réponse avec citation 20
Vieux 14/04/2011, 04h59   #3
Futur Membre du Club
 
Homme
Technicien réseau
Inscription : avril 2011
Messages : 15
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Nouvelle-Calédonie

Informations professionnelles :
Activité : Technicien réseau
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : avril 2011
Messages : 15
Points : 17
Points : 17
Citation:
Envoyé par N_BaH Voir le message
Bonjour,

c'est parcequ'ici
Code :
array_member="${arr${array_number}[element_number]}"
le shell ne sait pas ce qu'il doit substituer (en premier), alors que là
Code :
eval array_member="\${arr${array_number}[element_number]}"
le shell remplace d'abord ce qui lui est accessible : ${array_number}, ensuite eval exécute l'assignation array_member="${arr1[element_number]}", soit array_member="25"
...
?
Merci de ta réponse N_BaH,
j'en déduis que toute initialisation dans le shell du type
variable= quelque chose comportant plus de une substitution
va échouer systématiquement ?
C'est ce que je comprends dans ta réponse
Effectivement le premier $ est protégé par le \: donc le shell n'a plus le choix
Ensuite avec eval ce $ est interprété et donne le résultat
Est cela qu'il fallait comprendre ?
Pourrais tu me répondre sur les 2 points que je sois sûr d'avoir bien compris !
Merci
syncope_nc est déconnecté   Envoyer un message privé Réponse avec citation 11
Vieux 14/04/2011, 13h17   #4
Expert Confirmé Sénior
 
Avatar de N_BaH
 
Inscription : février 2008
Messages : 2 070
Détails du profil
Informations forums :
Inscription : février 2008
Messages : 2 070
Points : 4 153
Points : 4 153
Bonjour,

a)
Citation:
toute initialisation dans le shell du type
variable= quelque chose comportant plus de une substitution
va échouer systématiquement ?
toutes celles où une variable compose le nom d'une autre variable entre accolades :
Code :
var=foo var1=bar foobar=baz; eval echo "\${foo$var1}"
sinon :
Code :
var=foo var1=bar; echo "$var$var1"
fonctionne directement, et
Code :
foo=baz; echo "${!var}"
nécessite une indirection (initiée par le !) pour obtenir baz

b)
Citation:
Est cela qu'il fallait comprendre ?
oui
N_BaH est déconnecté   Envoyer un message privé Réponse avec citation 30
Vieux 15/04/2011, 11h40   #5
Membre éprouvé
 
Avatar de ben.IT
 
Homme Benoît
Inscription : janvier 2009
Messages : 392
Détails du profil
Informations personnelles :
Nom : Homme Benoît
Âge : 24
Localisation : France, Puy de Dôme (Auvergne)

Informations forums :
Inscription : janvier 2009
Messages : 392
Points : 438
Points : 438
Voici à propos de l'indirection :
Citation:
3.5.3 Shell Parameter Expansion

The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

When braces are used, the matching ending brace is the first ‘}’ not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

The basic form of parameter expansion is ${parameter}. The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.

If the first character of parameter is an exclamation point, a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.

In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.

When not performing substring expansion, Bash tests for a parameter that is unset or null; omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both existence and that the value is not null; if the colon is omitted, the operator tests only for existence.
a+,
ben
__________________
Meet the free software gang
ben.IT est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



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


 
 
 
 
Partenaires

Hébergement Web