Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a question from newbie 1

Status
Not open for further replies.

unprophete

Technical User
Apr 9, 2010
31
DE
Hello,
what does the eval do?

in a ksh script I have found:

PROCLST='ps -ef|grep cal7w|grep -v grep'

and later in the same script...

if [ `eval $PROCLST | wc -l` -ne 0 ]
then
echo "`eval $PROCLST`"
....

what does eval do in the above test and later in "echo" command? is it correct?
what if the eval would be removed in both cases (in the 'test' replaced by echo)? what would be the difference then in functionality?

could someone please explain it (maybe on some examples) like to a newbie?

Best regards,
Matthias.
 
on my AIX box there is nothing about eval in ksh's manual.. :-(

$ man ksh | grep eval

yes, yes, I know I can google - I did it and found single poor sentence - not enough for newbie on manual pages....


§ eval [ arg . . . ]
The arguments are read as input to the shell and the resulting command(s) executed.

Any axample(s) please to see how this stuff works in real? What is the real benefit of using it?

And don't be angry for my asking.
 
eval $PROCLST
is the same as:
ps -ef|grep cal7w|grep -v grep

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Basically eval (which is short for "evaluate") can be used to parse the provided command line an extra time before executing it.

It can be useful if you need to dynamically change the command that is to be run using variables, so if in your example the PROCLIST variable can have different values depending on some logic, then it needs to be evaluated later to actually run it. The reason is because the shell pipeline is broken down into its individual elements before variables are substituted. In your example you need the variable substition to occur first, because it contains pipes.

Am I making sense?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top