DASH Shell discussions
 help / color / mirror / Atom feed
From: Peter Jakobi <jakobi@acm.org>
To: dash@vger.kernel.org
Subject: Feature request: set -o pipefail
Date: Fri, 7 Aug 2009 15:18:33 +0200	[thread overview]
Message-ID: <20090807131833.GA30733@anuurn.compact> (raw)

Hi,

[Note:  I'm not on the list, so put jakobi@acm.org in cc: if I need to
reply. thanx, Peter]

Feature request:

implement  set  -o pipefail. This will allow the user to  request  the
return  code  to be the one of the first pipe command with a  non-zero
error. 

The  lack  of  which  can be quite painful in  scripting,  even  if  a
bourne-shell/posix subset would otherwise be quite sufficient.


Advantages:

This  allows use of pipes with proper error detection, which is  quite
helpful  for  init  scripts / cron or  generally  administration-style
scripts which exec shell commands from shell/perl/python/... scripts.


Usage case: 

have  a  command  like mv -i act on user input, while  keeping  output
(stdout+err) in a log AND returning mv's error code (or tee's if mv is
ok). in a perl script, this would be something like:

   system "set -o pipefail; mv -i foo bar </dev/tty 2>&1 | tee -a LOG";

Part  of  the problem is that the shell used for open/system is  often
hard-coded in the interpreter to be /bin/sh.


Work aound:

   1. as root: 

   restore /bin/sh to point to a more complete shell of bourne-ancestry.
   With the subsequent larger set of library dependencies, ...

   or

   2. non-root:

   first detect a suitable shell:
   # detect likely shells in path and /etc/shells
   # for each shell, try $tmp=`set -o pipefail 2>&1`
   # choose one that doesn't set a non-zero errror in $?.
   # if all else fails, try defaulting to bash or ksh

   system $shell, "-c", 
          'set -o pipefail; mv -i "$@" </dev/tty 2>&1 | tee -a LOG', 
          "world's most painful trivial system command",
          "foo", "bar";

btw,  wrappers  like script might help, besides being a pain  wrt  LOG
management.  But there's the slight of script -c false returning true.
Which makes e.g. script impossible to use.

-- 
cu
Peter
jakobi@acm.org

             reply	other threads:[~2009-08-07 13:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-07 13:18 Peter Jakobi [this message]
2009-08-12 12:51 ` Feature request: set -o pipefail Eric Blake
2009-08-13 14:35   ` Peter Jakobi
2009-08-13 23:15     ` Oleg Verych

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090807131833.GA30733@anuurn.compact \
    --to=jakobi@acm.org \
    --cc=dash@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox