问题描述
来自 linux,特别是来自树莓派,我试图在后台启动一个程序,获取该程序的输出,并根据输出传递给后台程序其他字符串。 据我所知,正确的方法是使用命名管道,但我不太明白它们是如何工作的。
nordvpn login
nordvpn connect
nordvpn logout
...
每个都可以给出不同的输出。
目前我写的代码是这样的,效果不是很好,还有什么东西不见了,怎么办?
#!/bin/bash
pipe=/tmp/vpnpipe
trap "rm -f $pipe" EXIT
if [[ ! -p $pipe ]]; then
mkfifo $pipe
echo "pipe created"
fi
EMAIL="try"
PASSWORD="tryme"
nordvpn login > pipe &
VPN_PID=$!
echo "pid: $VPN_PID"
while true
do
if read line <$pipe; then
echo "$line"
#first problem,after starting the program nothing is actually read from the pipe and the bash
#script hangs here waiting for something even if in reality when "nordvpn login" is run normally
#it responds immediately on the command line with:
#"Please enter your login details.
#Email / Username: "
if [[ "$line" == 'Email / Username:' ]]; then
echo "username"
#at this point,how do I correctly pass the email first and then the password?
fi
if [[ "$line" == 'Password:' ]]; then
echo "password"
fi
if [[ "$line" == 'Welcome to nordVPN! You can Now connect to VPN by using nordvpn connect.' ]]; then
nordvpn connect > pipe &
fi
fi
done
echo "done"
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)