懒惰的评价猛砸 [英] Lazy Evaluation in Bash
本文介绍了懒惰的评价猛砸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有这样做懒的评价比下面的更优雅的方式:
Is there more elegant way of doing lazy evaluation than the following:
pattern='$x and $y'
x=1
y=2
eval "echo $pattern"
结果:
1 and 2
它的工作原理,但评估回声......
只是觉得草率,并可能以某种方式不安全。有没有更好的办法在Bash中做到这一点?
It works but eval "echo ..."
just feels sloppy and may be insecure in some way. Is there a better way to do this in Bash?
推荐答案
您使用的评估的是正确的,实际上它在shell编程一个pretty常见的模式。 评估的可能是一个安全隐患,确实,但只有当脚本获得权限或数据来自不受信任的来源。我想这不是你的情况吧?
Your use of eval is correct, in fact it's a pretty common pattern in shell programming. eval may be a security risk, indeed, but only if the script gains privileges or the data comes from an untrusted source. I imagine that's not your case, right?
顺便说一句,你也可以这样写:
By the way, you can also write this:
pattern='$x and $y'
x=1
y=2
eval "result=\"$pattern\""
这篇关于懒惰的评价猛砸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文