Solved

hel with the eval command

Posted on 2009-03-30
6
283 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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