环境: Linux/Docker,bash 4.2 版。
问题:
server-app
接受带有交互式 shell 的命令。server-app
client.sh
通过管道接收来自 a 的命令。server-app
写入由 接收的标准输出client.sh
。client.sh
考虑server-app
的输出以确定进一步的命令。server-app
可能还要求输入密码 => 也就是说,终端的标准输入必须server-app
与client.sh
.
我尝试了多种设置coproc
。但是,我要么陷入停滞状态,要么没有收到数据。如何实现如下所示的设置bash
?
.------>------------------------.
.---------. | .------------. stdin | .-----------.
| user |-------' | server-app |<-------+-------| client.sh |
| console |<--+-------------| | .--------->| |
'---------' | stdout '------------' | '-----------'
'--------------------------------'
我的尝试:
coproc server.app
function expect { local expectation=$1
# read from pipe until $expectation occurs in the input stream
# when found, echo the line to 'stdout'
echo "EXPECT: '$expectation'"
while true; do
read text <&"${COPROC[0]}"
if [[ "$text" == *"$expectation"* ]]; then
echo $text
break
fi
done
}
function send { local command=$1
# send $command through pipe
echo "SEND: $command"
echo "$command" >&"${COPROC[1]}"
}
expect "Conected to URL"
send "open"
expect "Session keepalive"
send "session open"
# use the reported session identifier to setup the user command
session_n=$(expect "Identifier of Session created" | cut -d' ' -f5)
command=$(echo "$user_command" | sed -e "s/SESSION/$session_n/g")
最后三行仅演示了对server-app
' 输出的可能处理。这又有什么问题呢?怎么可能带去上班。