help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: left side of pipe


From: Pascal
Subject: Re: left side of pipe
Date: Mon, 24 Jan 2022 00:46:16 +0100

thanks for your feedback Greg,

the only exit code I finally need is that of yad so yad can be run without
being placed in the background.
if it returns 252, it means that the operation has been canceled by the
user and then I can kill ewfacquire with kill $writer.

here is my script now :

#!/bin/bash

set -o pipefail

ewf_options="-c best -u -w" # acquire options

yad_global_options="--title=test --sticky" # global options
yad_progress_options="--progress --geometry=640x320 --progress-text=copying
--pulsate" # progress options
yad_progress_options="$yad_progress_options --auto-close --enable-log
--log-expanded --button=Cancel:252" # more progress options

touch ${err:=/tmp/test.err} ${log:=/tmp/test.log}
mkfifo ${fifo:=/tmp/ewfyad.fifo}
trap "rm $fifo" exit
img=/tmp/test.img

ewfacquire $ewf_options -t $img /dev/sda1 1> $fifo 2> $err & ewf=$!
yad $yad_global_options $yad_progress_options < <( tee $log < $fifo | sed
-u 's/^/#/g' )

ret=$?
img=$img.E01
if [ $ret -ne 0 ]
then
kill $ewf
rm $err $log $img
if [ $ret -ne 252 ]
then
yad $yad_global_options --fixed --text="Error" --button=Cancel
--image=emblem-noread
fi
exit $req
fi
if [ -s $err ]
then
yad $yad_global_options --fixed --text="$( sed '/^$/d' $err )"
--button=Cancel --image=emblem-noread
rm $log $img
exit 1
elif ! grep -q SUCCESS <( tail -1 $log )
then
rm $err $log $img
yad $yad_global_options --fixed --text="Error" --button=Cancel
--image=emblem-noread
exit 143
else
yad $yad_global_options --fixed --text="Copied" --button=Return
--image=emblem-default
rm $err
fi

Le dim. 23 janv. 2022 à 22:17, Greg Wooledge <greg@wooledge.org> a écrit :

> On Sun, Jan 23, 2022 at 08:33:27PM +0100, Pascal wrote:
> > ewfacquier -t /tmp/test /dev/sda1 |
> > yad --progress --progress-text "copying..." --pulsate --auto-close
> > --auto-kill --button=Cancel:252
>
> I'm guessing "yad" is an X11 program, not a terminal program.
>
> > it works fine as long as I don't stop yad.
> > if I stop yad, the ewfacquire process continues to run for a few more
> > (long) seconds even though the script is finished.
>
> That's normal and expected behavior.  When a pipe is destroyed, the
> process that's writing to the pipe will receive a SIGPIPE the next
> time it attempts to write to the pipe.  That's the point where it dies.
>
> > how to stop ewfacquire when canceling yad (while keeping the pipe exit
> code
> > in the opposite case for the if test) ?
>
> You could simply let it go until it dies to SIGPIPE on its next attempted
> write.  This is how most things work.
>
> If you feel you MUST kill it as soon as possible, then you'll have to
> rewrite basically every single thing you're doing to achieve that goal.
> Instead of using an anonymous pipe, use a named pipe, and run the reader
> and writer processes as background jobs with the PIDs remembered by the
> script.  Set up a "wait -n" on the two PIDs, and when the wait returns
> (meaning that one of them has died), send a SIGTERM to both PIDs, so
> that the other one will also be killed.  Then clean up the named pipe.
>
> #!/bin/bash
> mkdir -p ~/.cache
> pipe=$HOME/.cache/ewf-yad-fifo
> trap 'rm -f "$pipe"' EXIT
> mkfifo "$pipe"
> ewfacquier -t /tmp/test /dev/sda1 > "$pipe" & writer=$!
> yad --progress --progress-text "copying..." --pulsate --auto-close \
>     --auto-kill --button=Cancel:252 < "$pipe" & reader=$!
> wait -n "$reader" "$writer"
> kill "$reader" "$writer"
>
> But... wait, you also said you wanted to retrieve an exit status.  That
> makes it even harder.  At this point, you're going to have to make some
> additional assumptions.
>
> Let's make this assumption: "the reader (yad) will always exit first".
>
> From there, we can just wait on the reader, and retrieve its exit status
> that way.  Then we only have to kill the writer.
>
> #!/bin/bash
> mkdir -p ~/.cache
> pipe=$HOME/.cache/ewf-yad-fifo
> trap 'rm -f "$pipe"' EXIT
> mkfifo "$pipe"
> ewfacquier -t /tmp/test /dev/sda1 > "$pipe" & writer=$!
> yad --progress --progress-text "copying..." --pulsate --auto-close \
>     --auto-kill --button=Cancel:252 < "$pipe" & reader=$!
> wait "$reader"
> status=$?
> kill "$writer"
> exit "$status"
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]