pipe - bash pipestatus in backticked command? -


in bash, if execute couple of commands piped inside of backticks, how can find out exit status of first command?

i.e. in case, trying "1". can via pipestatus[0] if not using backticks, doesn't seem work when want saving output:

## pipestatus[0] works give me exit status of 'false': $ false | true; $ echo $? ${pipestatus[0]} ${pipestatus[1]}; 0 1 0  ## doesn't work: $ a=`false | true`; $ echo $? ${pipestatus[0]} ${pipestatus[1]}; 0 0 

more generally, trying accomplish: save last line of output of program variable, able tell if program failed:

$ myvar=` ./someprogram | tail -1 `; $ if [ "what put here" ]; echo "program failed!"; fi 

ideally i'd understand going on, not answer is.

thanks.

try set pipefail option. returns last command of pipeline failed. 1 example:

first disable it:

set +o pipefail 

create perl script (script.pl) test pipeline:

#!/usr/bin/env perl  use warnings; use strict;  if ( @argv ) {      die "line1\nline2\nline3\n"; } else {     print "line1\nline2\nline3\n"; } 

run in command-line:

myvar=`perl script.pl | tail -1` echo $? "$myvar" 

that yields:

0 line3 

it seems correct, let see pipefail enabled:

set -o pipefail 

and run command:

myvar=`perl script.pl fail 2>&1 | tail -1` echo $? "$myvar" 

that yields:

255 line3 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -