Transferred from: http://blog.csdn.net/w_ww_w/article/details/7075867
Eval can read a series of parameters and then execute them according to the characteristics of the parameters themselves. Eval is a shell built-in command that can be used by the shell to see its usage. Unlimited number of parameters, separated from each other by semicolons.
eval [parameter]
The eval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for a time that does not function. This command scans the variable two times. These variables, which need to be scanned two times, are sometimes referred to as complex variables. But the variables themselves are not complicated. The eval command can also be used to echo simple variables, not necessarily complex variables.
1. The eval command can also be used to echo simple variables, not necessarily complex variables
For example:
[[Email protected]~]$ Name=valuebank
[Email protected] ~]$ eval echo $NAME
Valuebank
[Email protected] ~]$ echo $NAME
Valuebank
2. Execute command with string
First we first create a small file named Test that contains some text in this small file. Next, assign the cat test to the variable myfile, and now we e ch o the variable to see if we can execute the above command.
[Email protected] ~]$ VI test
[email protected] ~]$ cat test
Hello World!!!
I am a Chinese boy!
Assigns the cat testf to the variable myfile
[Email protected] ~]$ myfile= "Cat test"
If we e c h o The variable, we will not be able to list the contents of the T es t file.
[Email protected] ~]$ echo $myfile
Cat Test
Let's try the E v a l command and remember that the E v a l command will scan the variable two times.
[Email protected] ~]$ eval $myfile
Hello World!!!
I am a Chinese boy!
As can be seen from the above results, the use of the E v A l command can not only displace the variable, but also to execute the corresponding command. The
A scan was performed with variable substitution, and the second scan performed the command cattest contained in the string.
3. Commands can also be used to display the last parameter passed to the script
[email protected] ~]$ cat Test1
#!/bin/bash
echo "Total of the arguments passed $#"
echo "The process Id is $$"
echo "Last argument OS" $ (eval echo\$$#) ""
[Email protected] ~]$./test1 Value Bank test last
Total of the arguments passed 4
The process Id is 21545
Last argument OS last
In the above script, the E v A l command first puts $ $ #解析为当前s the number of arguments to the He l l, and then on the second scan
The last parameter is drawn.
4. Give each value a variable name
You can give a value a variable name. Here are some explanations for this, assuming there is a file named Test2:
[email protected] ~]$ cat Test2
CCTV 5
Changel SPORTS
Like YES
You want the first column in the file to be the variable name, and the second column to be the value of the variable, so you can:
[email protected] ~]$ cat Test2
Commany TQ
LANGUE 中文版
Like YES
[email protected] ~]$ cat Test3
#!/bin/bash
While read NAME VALUE
Do
Eval "${name}=${value}"
Done <test2
echo "$COMMANY $LANGUE $LIKE"
[Email protected] ~]$./test3
TQ 中文 YES
Reference:
Http://www.cnblogs.com/huzhiwei/archive/2012/03/14/2395956.html
Http://www.runoob.com/linux/linux-comm-eval.html Rookie Tutorial
Shell eval Usage