Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

hel with the eval command

Posted on 2009-03-30
6
Medium Priority
?
293 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

688 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