[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Testing standard output and exit statuses from commands in a pipe
From: |
Greg Wooledge |
Subject: |
Re: Testing standard output and exit statuses from commands in a pipe |
Date: |
Mon, 27 Apr 2009 10:24:21 -0400 |
User-agent: |
Mutt/1.4.2.2i |
On Mon, Apr 27, 2009 at 05:02:26PM +0300, Angel Tsankov wrote:
> I'd like to pipe the output from a command, say A, to another command, say
> B, then check if both commands completed successfully (i.e.with exist status
> 0)
PIPESTATUS
An array variable (see Arrays below) containing a list of exit
status values from the processes in the most-recently-executed
foreground pipeline (which may contain only a single command).
> and, if so, compare the standard output from command B to some string.
> How can I do this in a bash script?
Ah, that makes it much trickier. You can't use PIPESTATUS when you're
capturing the output of a pipeline with a command substitution.
arc1:~$ x() { echo x; exit 1; }
arc1:~$ y() { echo y; exit 0; }
arc1:~$ x|y; echo "${PIPESTATUS[@]}"
y
1 0
arc1:~$ q=$(x|y); echo "${PIPESTATUS[@]}"
0
In this case, you will need either a temporary file, or a named pipe.