I am displaying a command progress in a bash script. The command output is piped to zenity --progress and can eventually run for a long time. I want to abort it (kill that command) if I cancel the zenity dialog:
( echo 0; command; echo 100 ) | if ! zenity --progress
then DO_SOMETHING_TO_KILL_command
fi
All the solutions I have found either:
generically kill "command" with pgrep, pidof, pkill, killall, etc. This is not what I want since there might be many such "command" running.
create a fifo to output the PID of "command" (command & echo $! >some_fifo; wait) and then read from it after the pipe.
Solution 2. does what I want, but in an overcomplicated way (see e.g. an example here (in French)). I want to avoid fifos or temp files if possible. It seems as if it could be done with an output redirection to a file descriptor at most, but I cannot figure exactly how.
Note: command substitution for the whole group with redirection e.g., $( ( ... command& echo >&3 ... | zenity --progress ) 3>&1 ) -- which is a generic soution in cases of this type -- does not work here because the $(...) waits until the whole subshell is completed.