Re: Bash help requested: Capturing command errors within pipes

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

 



Cameron Simpson wrote:
On 19Mar2009 12:19, Daniel B. Thurman <dant@xxxxxxxxx> wrote:
OK, I found out why I had a problem!

I setup an intentional sed error:
$ (re="-e '\'s/b/h/'"; echo "boo" | sed $re | echo "done"; echo ${PIPESTATUS})
done
sed: -e expression #1, char 1: unknown command: `''
0

[ I'm sure you know everything I say below based on previous posts, but
  it seems worth saying purely for other readers...
]

Ok, but I strongly recommend you never us a regexp unquoted - the
necessary backslash nesting gets nasty real fast. A better way is like
this:

  re='s/b/h/'
  sed -e "$re"

or for complex stuff, write a file called "sedf" (for example)
containing a full sed script:

  s/b/h/
  /foo/d
  /bah/b again

etc. Then:

  sed -f sedf

The point here, unrelated to the pipe exit status issue, is to keep the
sed stuff easy to write and undamaged by shell substitution.

On the pipe stuff, another approach (less convenient than PIPESTATUS),
is a flag approach like you original attempt, but using files:

  badness=/tmp/foo$$bad
  { x || touch $badness; } \
  | { y || touch $badness; } \
  | { z || touch $badness; }
  if [ -f $badness ]
  then
    rm $badness
    echo badness happened >&2
  fi

It has the advantage of working in non-pipe circumstances (nested
subshells, etc).

Cheers,
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?

TRACKER="Tracker.log"
SFILE="Bad_Hosts_File.txt"
TFILE="Bad_Hosts_File_$$.txt"
pat="EACC"
re1="-e '/$pat/s/^.*Received:\[from\s\+\(.*\)\s\+(.*/\1/'"
re2="-e '/^.*cdkkt.com$/d'"
rex="sed $re1 $re2"

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

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
fi
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?

Dan

--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux