Solved

hel with the eval command

Posted on 2009-03-30
6
282 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
incorrect file reading in bash 7 110
mapBully challenge 6 136
Showing a username in UNIX and using that name in the script 4 35
remove one line from a file in solaris 8 29
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…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

831 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