help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Fwd: Re: Waiting for a sub-process to finish


From: Russell Lewis
Subject: [Help-bash] Fwd: Re: Waiting for a sub-process to finish
Date: Thu, 1 Jun 2017 14:45:06 -0700

Copying my reply to the list.
---------- Forwarded message ----------
From: "Russell Lewis" <address@hidden>
Date: Jun 1, 2017 14:44
Subject: Re: [Help-bash] Waiting for a sub-process to finish
To: "João Eiras" <address@hidden>
Cc:

A simple (though non-intuitive) solution is to pipe the entire command into
cat:
    long_command -o >(gzip -c > file.gz) | cat

Since both long_command and gzip have file handles to the same stdout, and
cat is reading from it, cat will block until both commands close their
stdout.

Of course, this could fail if they both close their stdout before the
process exits, but that's rare.

(I wish I remember where I found this, so I could give proper attribution.
Somewhere on StackOverflow, probably.)

Russ

On Jun 1, 2017 13:11, "João Eiras" <address@hidden> wrote:

Hello !

Here's my problem. I have a script that calls an external program that
output its stuff in plain text. I'm piping the output to a process of mine
to compress it. Something like

   long_command -o >(gzip -c > file.gz)

I do not want to touch stdout nor stderr as these can be clobbered by
status messages, and the correct way to receive the output is using the -o
parameter.

Immediately after the program finishes, I want to check that the produced
file is OK, regarding contents and whatnot. So something like.

  long_command -o >(gzip -c > file.gz)
  if gzip -dc file.gz | grep -q bad_line; then
    echo some error
  fi

That second like with the call to "gzip -dc" often returns an error:
  gzip: abort: corrupted input -- invalid deflate data

This happens because the subprocess ">(gzip -c > file.gz)" has not had time
to finish and close the output file.

Question: How can I force bash to wait until all subprocesses to finish ?

'wait' does not work here as these are not background tasks managed by the
job control.

A simple testcase follows. "my_command_3 ended" should be printed before
"my_command_4 ran" is.

Thank you !
_________________________________________

#!/bin/bash
function my_command_1 {
echo my_command_1 started >&2
cat "$1" | sed 's/$/+2/'
echo my_command_1 ended >&2
}
function my_command_2 {
echo my_command_2 started >&2
echo 1
echo my_command_2 ended >&2
}
function my_command_3 {
echo my_command_3 started >&2
cat | sed 's/$/+3/'
sleep 1
echo my_command_3 ended >&2
}
function my_command_4 {
echo my_command_4 ran >&2
}
my_command_1 <(my_command_2) > >(my_command_3)
wait # Doesn't work?
my_command_4
sleep 1.1


reply via email to

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