Re: Bash help requested: Capturing command errors within pipes

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

 



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,
-- 
Cameron Simpson <cs@xxxxxxxxxx> DoD#743
http://www.cskk.ezoshosting.com/cs/

You don't stop riding because you get old, you get old because you stop
riding.         - Anon. on rec.motorcycles

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