我正在运行一个接受命令并提供终端输出的程序,但是一旦完成执行命令就不会终止,而是等待下一个命令。我想使用bash脚本从文件中读取行,将这些行作为命令发送到程序,然后将程序的输出写入文件。
我一直在尝试这样的事情:
program
while read line
do
$line # I want this sent as a command to the program
$variable=(`another command to program`)
echo $variable >> $2
done <$1
但这在第3行失败了;该命令不会发送到程序。
答案 0 :(得分:1)
在bash 4.x中:
coproc program
while read -u "${COPROC[0]}" line; do
echo "something" >&"${COPROC[1]}"
done
使用命名管道可以(稍微更痛苦地)制作旧壳。