Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使脚本在后台zenity对话框关闭时终止_Bash_Zenity - Fatal编程技术网

Bash 使脚本在后台zenity对话框关闭时终止

Bash 使脚本在后台zenity对话框关闭时终止,bash,zenity,Bash,Zenity,我在这里编写了一个相当简单的脚本,用于使用zenity显示一个文本信息对话框,并从远程TCP连接连续读取数据并在对话框中显示。这很有效。。。但是,如果关闭zenity对话框,我希望整个脚本终止 有办法做到这一点吗?我认为我无法检查while循环中的任何内容,因为从远程TCP连接读取数据时脚本可能会暂停 #!/bin/bash on_exit() { zenity --display=:0 --error --text="Script has exited." & }

我在这里编写了一个相当简单的脚本,用于使用
zenity
显示一个文本信息对话框,并从远程TCP连接连续读取数据并在对话框中显示。这很有效。。。但是,如果关闭
zenity
对话框,我希望整个脚本终止

有办法做到这一点吗?我认为我无法检查while循环中的任何内容,因为从远程TCP连接读取数据时脚本可能会暂停

#!/bin/bash

on_exit() {
        zenity --display=:0 --error --text="Script has exited." &
}

# Options
while getopts "a:p:t:" OPTION; do case "$OPTION" in
        a) address="$OPTARG";;
        p) port="$OPTARG";;
        t) title="$OPTARG";;
esac; done

exec &> >(zenity --display=:0 --text-info --title=$title || exit)
                               # doesn't make a difference?  ↑
                               # also tried &&
trap "on_exit" EXIT

while read data < /dev/tcp/$address/$port; do
        echo $data
        # ...
        # do some other stuff with the information
        # ...
done

我没有安装
zenity
,所以我尝试用这个脚本来说明这个想法

程序
terminal+cat
(emmouring
zenity
)由在后台运行的函数
\u dspMsg
执行(子进程)
cat
连续显示来自文件(
$m
)的消息,该文件是命名管道;当
terminal+cat
退出时,父进程被
kill
ed

#!/bin/bash
                              # 1) named pipe
m=`mktemp -u /tmp/msg-XXXX=`  #    get a temporary filename (for named pipe)
mkfifo "$m"                   #    create that named pipe
trap "echo END; rm $m" EXIT   #    remove that file when exit
                              # 2) zenity
_dspMsg(){                    #    continuously display messages
  urxvt -e bash -c "cat <$m"  #    terminal+cat is used in place of zenity
  kill $1                     #    kill parent pid
}                             #    to be run in background
_dspMsg $$ &                  #    $$ = proccess id
                              # 3) TCP info feeds
cat >>"$m"                    #    feeding messages using cat
                              #    cat is used in placed of TCP data feed
同时,另一个
cat
进程将消息写入管道
$m
(生成TPC信息提要);当
\u dspMsg
退出时,它将被杀死

#!/bin/bash
                              # 1) named pipe
m=`mktemp -u /tmp/msg-XXXX=`  #    get a temporary filename (for named pipe)
mkfifo "$m"                   #    create that named pipe
trap "echo END; rm $m" EXIT   #    remove that file when exit
                              # 2) zenity
_dspMsg(){                    #    continuously display messages
  urxvt -e bash -c "cat <$m"  #    terminal+cat is used in place of zenity
  kill $1                     #    kill parent pid
}                             #    to be run in background
_dspMsg $$ &                  #    $$ = proccess id
                              # 3) TCP info feeds
cat >>"$m"                    #    feeding messages using cat
                              #    cat is used in placed of TCP data feed

您可以通过管道将
while
循环的所有输出传输到zenity中,从而不再需要
exec&>

while read data < "/dev/tcp/$address/$port"; do
        echo "$data"
done | zenity --display=:0 --text-info --title="$title"
读取数据时
是否可以将Zenity执行放在等待终止并在终止后终止目标脚本的包装器中?在
>(…)
内部调用
exit
意味着退出子shell。您需要
kill
parrent PID来终止parrent shell。请考虑使用命名管道(不需要安装任何额外的代码)来进行更简单的编码。我应该的。这看起来很有希望<代码>$$对我来说是新的。我不知道有这样一种方法来获取当前脚本。不过我星期一得试试。今天的时间到了。
while read data < "/dev/tcp/$address/$port"; do
        echo "$data"
done | zenity --display=:0 --text-info --title="$title"