曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 6052|回复: 0
打印 上一主题 下一主题

[概念] eval

[复制链接]

4917

主题

5879

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34382
跳转到指定楼层
楼主
发表于 2009-1-23 11:00:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
eval [arg ...]
  The args are read and concatenated together into a  single  command.  This command is then read and executed by the shell, and its exit status is returned as the value of eval.  If there  are no args, or only null arguments, eval returns 0.
(args 被读取并连结成单独一个命令。接着,这个命令被 shell 读取并执行,其退出状态作为 eval 的值返回。如果没有 args ,或者只有空的声明,eval 就只返回 0)

举例说明:
foo=10
x=foo
y='$'$x
echo $y

上面的脚本输出: $foo

使用 eval 修改上面的脚本
foo=10
x=foo
eval y='$'$x
echo $y

结果输出为 10。因此,eval 看起来有点像给 foo 附加了个 $ 。也就是说,它得到了一个变量的值,然后再把这个值当成一个变量,最后求出这个变量的值。

eval 很有用,它能产生新的代码且可以动态地运行。它尽管使得脚本的调试变得复杂,但它另一方面可以使你处理一些困难甚至看起来是不可能的事情。

----------------
参考:
(1)man
(2)beginning.linux.programming 4 edition
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-15 07:57 , Processed in 0.059846 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表