【发布时间】:2019-05-14 10:48:11
【问题描述】:
以下是我的一些代码的简化版本:
#!/bin/bash
myfile=file.txt
interactive_command > $myfile &
pid=$!
# Use tail to wait for the file to be populated
while read -r line; do
first_output_line=$line
break # we only need the first line
done < <(tail -f $file)
rm $file
# do stuff with $first_output_line and $pid
# ...
# bring `interactive_command` to foreground?
我想在interactive_command 的第一行输出存储到变量后将其带到前台,以便用户可以通过调用此脚本与它进行交互。
但是,似乎在脚本上下文中使用fg %1 不起作用,并且我不能将fg 与PID 一起使用。有没有办法可以做到这一点?
(另外,有没有更优雅的方式来捕获输出的第一行,而不写入临时文件?)
【问题讨论】:
-
答案对你有用吗?
-
抱歉,最近比较忙,感谢您的回答!我实际上已经在使用
wait,它不太适合我的需求,因为它不允许与后台命令交互。不过,我还没有尝试过coproc,所以我会试一试!
标签: bash job-control