For executing command that is stored in variable the eval
command is used:
对于执行存储在变量中的命令,使用eval命令:
└──> 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
And the following which gives me errors:
以下给出了我的错误:
└──> 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" ...
4 个解决方案
#1
This will work
这会奏效
if timeout "$PROGRAM" "$OPT1" "$OPT2" ... ; then
echo Program ran successfully
else
echo Program terminated due to timeout
fi
#2
If it's about keeping commands in variables, this will work, although don't know if it's a 'proper bash way' to do it:
如果它是关于将命令保存在变量中,这将起作用,尽管不知道它是否是“正确的bash方式”:
command.sh:
#!/bin/bash
echo -e 'a\nb' | wc -l
run.sh:
#!/bin/bash
a="command.sh"
timeout 10 ./$a
#3
#4
Simple:
eval timeout 10 $a
Output:
2
#1
This will work
这会奏效
if timeout "$PROGRAM" "$OPT1" "$OPT2" ... ; then
echo Program ran successfully
else
echo Program terminated due to timeout
fi
#2
If it's about keeping commands in variables, this will work, although don't know if it's a 'proper bash way' to do it:
如果它是关于将命令保存在变量中,这将起作用,尽管不知道它是否是“正确的bash方式”:
command.sh:
#!/bin/bash
echo -e 'a\nb' | wc -l
run.sh:
#!/bin/bash
a="command.sh"
timeout 10 ./$a
#3
echo "$(timeout 10 echo -e 'a\nb' | wc -l)"
ORecho "$(timeout 2 echo "$(eval $a)")"
echo“$(timeout 10 echo -e'a \ nb'| wc -l)”或echo“$(timeout 2 echo”$(eval $ a)“)”
explanation 1 here : HERE
解释1:这里
explanation 2 here : HERE
解释2:这里
#4
Simple:
eval timeout 10 $a
Output:
2