如何在bash中组合超时和eval命令 [英] How to combine timeout and eval commands in bash

查看:80
本文介绍了如何在bash中组合超时和eval命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于执行存储在变量中的命令,使用 eval 命令:

For executing command that is stored in variable the eval command is used:

└──> a="echo -e 'a\nb' | wc -l"
└──> eval $a
2

但是如何将其与结合超时命令?我尝试了以下操作,这给了我错误的输出:

But how can it be combined with timeout command? I've tried following which gives me wrong output:

└──> timeout 10 $a
'a
b' | wc -l

以下内容使我出错:

└──> timeout 10 "$a"
timeout: failed to run command `echo -e \'a\\nb\' | wc -l': No such file or directory

└──> timeout 10 $(eval $a)
timeout: failed to run command `2': No such file or directory

└──> timeout 10 $(eval "$a")
timeout: failed to run command `2': No such file or directory

问题也可以成立:如何确定以下命令是否正确执行?

The question can also stand: How can I be sure that following command is executed properly?

timeout 10 "$PROGRAM" "$OPT1" "$OPT2" ...


推荐答案

简单:

a="echo -e 'a\nb' | wc -l"
eval timeout 10 $a

输出:

2

这篇关于如何在bash中组合超时和eval命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆