Solved

hel with the eval command

Posted on 2009-03-30
6
274 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now