Chinaunix首页 | 论坛 | 博客
  • 博客访问: 266765
  • 博文数量: 56
  • 博客积分: 1190
  • 博客等级: 少尉
  • 技术积分: 640
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-21 17:05
文章分类

全部博文(56)

文章存档

2014年(2)

2013年(4)

2012年(46)

2011年(4)

我的朋友

分类: LINUX

2012-03-25 20:31:22

shell eval问题(2008-07-22 16:26:04)
标签: shell eval 杂谈 分类: 技术心得

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.

 

转自:http://blog.sina.com.cn/s/blog_540862bb0100a2vd.html

阅读(1462) | 评论(0) | 转发(0) |
0

上一篇:Mysql 触发器

下一篇:SHELL参数介绍

给主人留下些什么吧!~~