微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

bash linux 在后台启动一个程序并与之通信

如何解决bash linux 在后台启动一个程序并与之通信

来自 linux,特别是来自树莓派,我试图在后台启动一个程序,获取该程序的输出,并根据输出传递给后台程序其他字符串。 据我所知,正确的方法是使用命名管道,但我不太明白它们是如何工作的。

我想在后台启动的程序是nordvpn,我使用的命令是:

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 举报,一经查实,本站将立刻删除。