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 16/03/2011, 21h38   #1
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
Par défaut [bash] accéder à la valeur d'une variable dont le nom est construit par un script : eval ou $[var]

Salut à tous,
je souhaite accéder à la valeur d'une variable créée dans un script à partir d'une variable $prefixe et d'une variable $suffixe.

pour cela , j'utilise la commande eval afin de faire subir une double évaluation à la ligne contenant le nom de ma variable d'intérêt. Or je viens de voir dans un script bash une autre syntaxe qui semble pouvoir permettre de faire ça : $[var] .
voici le script :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
  1 #!/bin/bash
  2 
  3 #nom de la variable dont la valeur m interesse
  4 prefixe_suffixe='9000'
  5 var1="prefixe"
  6 var2="suffixe"
  7 
  8 #creation du nom de la variable d interet
  9 var_full="${var1}_${var2}"
 10 echo $var_fulldd
 11 
 12 #syntaxe 1 : double evaluation avec eval
 13 eval "echo \$${var_full}"
 14 
 15 
 16 #syntaxe 2 : ??
 17 echo $[$var_full]

et voici le résultat de son exécution :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
[benoit@benoit src]$ bash -x testVariables.sh 
+ prefixe_suffixe=9000
+ var1=prefixe
+ var2=suffixe
+ var_full=prefixe_suffixe
+ echo
 
+ eval 'echo $prefixe_suffixe'
++ echo 9000
9000
+ echo 9000
9000
Je ne connaissais pas cette seconde syntaxe, quelle est la différence avec la première, laquelle vaut il mieux privilégier ?
merci d'avance,
ben
__________________
Meet the free software gang
ben.IT est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 16/03/2011, 22h01   #2
Membre éclairé
 
Avatar de FRUiT
 
Homme
Inscription : février 2011
Messages : 83
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 36
Localisation : France, Paris (Île de France)

Informations forums :
Inscription : février 2011
Messages : 83
Points : 326
Points : 326
Tu peux rajouter la syntaxe 3 :
FRUiT est déconnecté   Envoyer un message privé Réponse avec citation 20
Vieux 16/03/2011, 22h03   #3
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,

man bash dit que $[expression] est l'ancienne forme (bientôt obsolète) de $((expression))

ça fonctionne ici, parce que $prefixe_suffixe est un nombre (c'est bon à savoir); sinon, en bash, plutôt qu'utiliser eval, emploie l'indirection :${!prefixe_suffixe}
N_BaH est déconnecté   Envoyer un message privé Réponse avec citation 20
Vieux 17/03/2011, 13h51   #4
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
merci à vous 2, je ne connaissais pas l'indirection.
Voici un extrait qui traite du sujet : http://www.network-theory.co.uk/docs...Expansion.html

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.
en tous cas, voila qui va m'aider,
à bientôt,
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 19h16.


 
 
 
 
Partenaires

Hébergement Web