How to use "eval" (was: create variables in a loop?)

Liste des GroupesRevenir à cu shell 
Sujet : How to use "eval" (was: create variables in a loop?)
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shell
Date : 29. May 2024, 14:01:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83bk4o93gh.fsf_-_@helmutwaitzmann.news.arcor.de>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
 gazelle@shell.xmission.com (Kenny McCormack):
Dr Eberhard W Lisse  <nospam@lisse.NA> wrote:
I would like to do something like >
>
for i in USD GBP
do
$i=somevalue
done
>
does not work, of course. >
>
The general answer is to use "eval", so something like: >
>
for i in USD GBP
do
eval $i=somevalue
done
>
But this doesn't scale well if "somevalue" is other than a simple string. >
 First assign "somevalue" to a variable, not using "eval".  Then  assign that variable's value to the variable named by "$i", using  "eval"    somevariablename=... &&
   # Note, that in the following assignment "..." may be any
   # valid shell expression expanding to a string, not just
   # a simple string:
   #
   somevalue=... &&
   # Note, how the apostrophes are placed around the right
   # hand part of the following assignment to be "eval"ed
   # but not around the name of the receiving variable to be
   # assigned to:
   #
   eval "$somevariablename"'="$somevalue"'

Date Sujet#  Auteur
29 May 24 * create variables in a loop?12Dr Eberhard W Lisse
29 May 24 +- Re: create variables in a loop?1David W. Hodgins
29 May 24 +* Re: create variables in a loop?2Kenny McCormack
29 May 24 i`- How to use "eval" (was: create variables in a loop?)1Helmut Waitzmann
29 May 24 +- Re: create variables in a loop?1Lew Pitcher
29 May 24 +- Re: create variables in a loop?1Helmut Waitzmann
29 May 24 +- Re: create variables in a loop?1Chris Elvidge
29 May 24 +* Re: create variables in a loop?3Janis Papanagnou
29 May 24 i`* Re: create variables in a loop?2Eberhard W Lisse
29 May 24 i `- Re: create variables in a loop?1Kenny McCormack
30 May 24 +- Re: create variables in a loop?1Lawrence D'Oliveiro
30 May 24 `- Re: create variables in a loop?1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal