Make script complete when background maturity dialog is closed

I wrote a fairly simple script here that is designed to display a text information dialog box with zenityand continuously read data from a remote TCP connection and display it in a dialog box. This works ... However, I would like the whole script to end if I close the dialog zenity.

Is there any way to do this? I don’t think I can check anything in the while loop, because the script can be stopped while reading data from a remote TCP connection.

#!/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

Note. . This will be launched on IGEL Linux. I have no way to install additional packages. So, ideally, the solution I'm looking for is native to Bash.


Update

exec. @BachLien .

PID=$$
exec &> >(zenity --display=:0 --text-info --title=$title; kill $PID)
+4
2

zenity, script, .

terminal+cat (emmulating zenity) _dspMsg, ( ); cat ($m), ; kill ed, terminal+cat .

cat proccess messsages $m (emmulating TPC infomation feeds); , _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

:

  • .
  • , script, urxvt xterm, iTerm , .

, , , ():

#!/bin/bash

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

m=`mktemp -u /tmp/msg-XXXX=`
mkfifo "$m"
trap "zenity --display=:0 --error --text='Script has exited.' & rm $m" EXIT

_dspMsg(){
  zenity --display=:0 --text-info --title="$title" <"$m"
  kill $1
}

_dspMsg $$ &

while read data < /dev/tcp/$address/$port; do
  echo $data >>"$m"
done
+1

while zenity, exec &>.

while read data < "/dev/tcp/$address/$port"; do
        echo "$data"
done | zenity --display=:0 --text-info --title="$title"
0

Source: https://habr.com/ru/post/1693430/


All Articles