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

Re: Bash help requested: Capturing command errors within pipes

Cameron Simpson wrote:
On 19Mar2009 18:55, Daniel B. Thurman <dant cdkkt com> wrote:
Cameron Simpson wrote:
Ok, but I strongly recommend you never us a regexp unquoted - the
necessary backslash nesting gets nasty real fast. A better way is like
  sed -e "$re"
The point here, unrelated to the pipe exit status issue, is to keep the
sed stuff easy to write and undamaged by shell substitution. [...]
Another person sent me private email
warning of the double-quotes, but thanks
for your information, it is interesting!

This is what I ended up doing, so please give
your constructive critiques, if you so like?

Untested, but here is how I would write each of you code pieces were I doing
it myself:



Your quotes are unneeded here. Plenty of people find the "" marks and in
similar cases, to use ${foo} instead of $foo elsewhere, but I find the
syntactic noise annoying if there's no other necessity.

Also, you should never use UPPER CASE names for script local variables (i.e.
that are not to be exported). I give some background why here:




re1="-e '/$pat/s/^.*Received:\[from\s\+\(.*\)\s\+(.*/\1/'"
re2="-e '/^.*cdkkt.com$/d'"

Note that this "$" should probably be backslashed. As it happens, $/ is
not a shell special variable, so it is left alone. However, has you used
$# or one of the others ($!, et al) there would have been substitution
done, mangling your sed code.

rex="sed $re1 $re2"

I would strongly reommend putting this in a sed script file, perhaps
like this:

  cat >sedf$$ <<X
  rex="sed -f sedf$$"

Hmm, actually, maybe not. Maybe like this:

  sedline="/$pat/s/^.*Received:\[from\s\+\(.*\)\s\+(.*/\1/; /^.*cdkkt.com\$/d"

and then:

  sed -e "$sedline"

in your pipeline lower down.

echo -en "\033[0;36m>> \033[0;35mExtracting data from:\n \
 [\033[0;34m${TRACKER}\033[0;35m and appending to\n \

I usually put escape sequences into their own variables for readbility,
like this:


Then you can talk about ${tty_green}, ${tty_normal} etc in your text;
somewhat more readable. You can do even better than this, actually.
Since not all terminals use the ANSI colour escapes, it is better to use
tput, like this:


and then in your script:

  esc_tracker=`with-colour green echo "$TRACKER"`

and then use ${esc_tracker} in messages. And so forth.

out=$(grep "$pat" "${TRACKER}" | \
         eval "$rex" | sort -n | \
         uniq >> "${TFILE}"); ret="$?";

if [ "$ret" -gt 0 ] || \
  [ "$PIPESTATUS[0]" -gt 0 ] || \
  [ "$PIPESTATUS[1]" -gt 0 ] || \
  [ "$PIPESTATUS[2]" -gt 0 ] || \
  [ "$PIPESTATUS[3]" -gt 0 ]; then
  echo -e " \033[0;31mFailed.\n   \
  \033[0;35m-- ErrorStatus:<ret=$ret>,\
 PipeStatus:<$PIPESTATUS[ ]>\n\
 -- out:<$out>\033[0m"
  echo -e "\033[0;31m>> \033[0;31m$PROG exited.\033[0m\n"
  exit 1
echo -e " \033[0;32mDone.\033[0m";

I guess the ugly part is the multiple $PIPESTATUS[n]
in the if statement test block. but perhaps this is the best
I can do?

Nah. You can do this:

  if out=$(grep "$pat" "$tracker" | $rex | sort -un >>"$tfile")
    case " $PIPESTATUS" in
      *' '[1-9]*)
        echo exit ok, but PIPESTATUS=$PIPESTATUS
      *)echo exit ok and PIPESTATUS all zeroes
    echo exit not ok

Um, you are aware that $out will always be empty? You have sent
standard out to the temp file. Standard error is not collected by back
ticks or $(); it will be displayed directly and not land in $out.
You probably need to go:

  out=$(exec 2>&1; grep .... >>"$tfile")

An easy test is like this:

  out=$(ls /no/such/file >>temp)
  echo "out=[$out]"


  out=$(exec 2>&1; ls /no/such/file >>temp)
  echo "out=[$out]"

Thanks a LOT!  I have updated my code following your
recommendations!  Guys like you are sure a help!

It sure was an eye-opener for me when it comes to
assigning variables with double-quotes.  I thought
it was needed, but little did I know!

Thanks for the education, it is appreciated!

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