分类: LINUX
2012-03-25 20:31:22
Problem No.1
Description:
It is requested by Bin, our project leader who is excellent in C programming, now he is focusing on this new testing project.
He would want to know how to execute a command string.
ex.
if cmdString="echo test", how can we run the cmdString.
He used to deal it with a not effecient method.
-----------------------------------------------
# echo $cmdString > tempscript
# source tempscript
-----------------------------------------------
Yes, of course, "source" command can bring up any file without execution privilege. But it is a waste of memory and execution effeciency.
I gave him the answer to this problem which seems easier to touch.
------------------------------------------------------
# $cmdString
-------------------------------------------------------
ex.
# cmdString="cd /home"
# $cmdString
Directly use this parameter for execution.
Till now, it seems that this issue is well done, but it is only a start for me.
After minutes, problem came out again.
if cmdString="sed 's/hello/Hello/' file"
$cmdString can not be competent for it.
Now, eval shows its capability.
Grammar:
eval command-line
where command-line is a normal command line that you would type at the terminal. When you put eval in front of it, however, the net effect is that the shell scans the command line twice before executing it.
You can try
#eval $cmdString
eval consider "$cmdString" a command line after the first scan, then execute in the second time.
Today, Little Zhu asked me a problem which also related to "eval" (in both our minds).
Little Zhu is our fellow students who is recently in Baidu Beijing, and in department of testing.
Problem No.2
Description:
t1=1
t2=2
t3=3
t4=4
tt='$t1 $t2 $t3 \$t4'
the expected result is "1 2 3 \4"
I think "eval" should be used, but I don't know how to use it. I tried some ways, and found the simple result.
# eval echo $tt
It is not exectly correct in this problem because of "\" which should be translated again. If we want to get the expected result, we should define
tt='$t1 $t2 $t3 \\$t4
tt='$t1 $t2 $t3 \$t4' may lead to "1 2 3 $t4" which can be "eval" to "1 2 3 4".
Try more then.