|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
The internal eval command interprets and expands a command line before the shell interprets and expands the line. This allows you to:
The eval command provides an extended feature allowing you to do special command processing. It is used to evaluate variables twice, so you can use the value of one variable as a variable name. The other popular use of eval is to process an input line as a command.
Following is the general format of the eval command.
eval [ command arg ... ]
The following list describes the arguments that may be passed to the eval command.
command |
The name of any valid command (internal or external). |
arg |
The arguments used by the specified command. The args are separated by a space. |
The shell expands arguments to eval using standard command processing rules. Then the shell forms a space-separated string of all the arguments. The shell reads the string as a command line and processes it again and executes it.
You can use eval to execute a command you read using the read command. For example,
cj> read CMD
cat /etc/group | grep mylogin
cj> eval "$CMD"
processes the CMD variable. If you enter any special characters, such as ; or |, eval expands them and the shell reprocesses the results before executing the command line. If you did not use the eval command, the CMD variable would not be interpreted correctly and certain commands would not run. Another example illustrates this concept.
cj> CMD='date | wc'
cj> $CMD
usage: date [-a sss.fff] [-u] [tformat] [mmddhhmm[yy]]
cj> eval $CMD
Sat Jan 5 22:54:04 CST 1989
The first line sets CMD to a command containing a pipe. The second command trys to execute the variable CMD. The execution fails because the pipe is not expanded and is passed to date as an argument. Thus date complains about a bad argument. The third line expands CMD to date | wc. The shell executes the line as a proper command.
The following example illustrates how you can use eval to expand variables within a command line to be the name of another variable.
cj> eval last='$'{$#} # expands to last positional parameter
cj> X=10
cj> Y=X
cj> echo '$'$Y
$X
cj> eval echo '$'$Y
10
cj> set One Two Three Four
cj> if eval [ ! -f \${$#} ]
then echo "$4: last argument must be a file!"
#exit 1
fi
This code checks for the last argument on the command line to be the name of a file that exists. The first line sets the positional parameters to One Two Three Four. Since Four is not the name of a file, your code will return the message about the last file. The exit is commented out so you are not logged off when the command executes.
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: February 28, 2008