本文介绍了在执行后台任务的脚本上执行bash命令替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想返回启动后台任务的脚本的结果.命令替换运算符等待后台任务,从而使调用变慢.我创建了以下示例来说明问题:
I'd like to return the results of a script that also kicks off a background task. The command substitution operator waits for the background task, making the call slow. I created the following example to illustrate the problem:
function answer {
sleep 5 &
echo string
}
echo $(answer)
是否可以在不等待命令创建的任何后台作业的情况下调用命令?
Is there a way to call a command without waiting on any background jobs it creates?
谢谢
标记
推荐答案
问题是 sleep
继承了stdout并将其保持打开状态.您可以直接重定向标准输出:
The problem is that sleep
inherits stdout and keeps it open. You can simply redirect stdout:
answer() {
sleep 5 > /dev/null &
echo "string"
}
echo "$(answer)"
这篇关于在执行后台任务的脚本上执行bash命令替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!