Solved

hel with the eval command

Posted on 2009-03-30
6
290 Views
Last Modified: 2013-12-26
Hi ,Im trying to improv my unix skills but am struggling to get to grips with the eval command.
i understand that it interperets the command then executes it, however im  little confused be4cause i dnt know when i need to use the command and where i need to space out characters.i.e.
eval example=\$row$rowindex -  this works but i dnt know why i need the eval there and why i need to use "\"
please answer the above and
Provide some basic examples moving to  more advanced uses for the command
please note i am looking for dummy proof examples to start off with.
0
Comment
Question by:isarasoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 24017556
the \ means do not interpret $ as an operator

here is an example

server1=host1
server2=host2
server3=host3
server4=host4
for i in 1 2 3 4
do
  echo $h$i
  eval echo \$server$i
done

 
0
 
LVL 1

Author Comment

by:isarasoo
ID: 24017686
thats fine but why not space out the second $
i.e.
eval echo \$server\$i
can you also explain the double dollar in the following eval command
for ((i=0;i<=n;i++; ))
{
eval echo how does this work\$$i
}
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24018276
I will start with a modified copy of the script and its output

server1=host1
server2=host2
server3=host3
server4=host4
server=myname
for i in 1 2 3 4
do
  echo $server$i
  eval echo \$server$i
done

------------- output -------------

myname1
host1
myname2
host2
myname3
host3
myname4
host4

As you can see from the output the 1st echo is concatenating values of variables server and i
The 2nd one with eval is dynamically changing the variable name i.e. it leaves \$server as $server and substitues $i with its value so the variable name becomes server1, server2, ...

the \ before $ will stop eval from treating it as $server and hence replacing it with its value, while $i is replaced by its value
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:isarasoo
ID: 24018562
Hi just to break it down

eval echo \$server$i
1)
interprets $server1...4
executes $server1....4
or
2) does it get the value
i.e.
host1....4

finally, in some eval commands where you use a for loop i have seen the double dollar sign to take the value from ii.e.

for ((i=0;i<=$#;i++; ))
{
eval echo "how does this work \$$i"
}
please explain why in some cases like above you need a double dollar sign

Sorry for being a pain, i just want to make sure im confident with using eval

thanks
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 250 total points
ID: 24018805
I will start with the 2nd one:

shell script has positional parameters which are arguments passed to the shell script from command line. e.g. when your execute

myscript my name is omar

the script myscript gets 4 arguments: my name is omar

the shell script can access these arguments using $1 $2 $3 $4 . the number of arguments is in $#

now to have a loop that covers a variable number of arguments, you need to have a dynamic way of changing the positional parameter reference. here the statement

eval echo "how does this work \$$i"

means that eval will scan the rest of the line and skip the first $ and leave it as is since it is preceded with \ and put the value of variable i in place of $i , which produces

echo "how does this work $1"

for the first loop where i value is 1, then

echo "how does this work $2"

where i value is 2, and so on.

regarding my example, I will take one example from it: server=myname server2=host2 and i=2

echo $server$i

results in

myname2

since it replaces $server with value myname and $i with value 2

eval echo \$server$i

results in

echo $server2

which is executed and results in

host2

the value of server2

0
 
LVL 1

Author Closing Comment

by:isarasoo
ID: 31564268
thats perfect, thanks for your help
0

Featured Post

MongoDB Through a MySQL Lens

This article looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question