Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: More verbose && ?



2010-01-20 17:34:38 +0100, Richard Hartmann:
> Hi all,
> 
> I am wondering if there is a way to make && more verbose. Specifically,
> I am interested in where it failed in case it does.
> 
> Consider this, admittedly crafted, example:
> 
> 
> rm dontexist
> touch exist
> echo yes && cat exist 2>/dev/null && cat dontexist 2>/dev/null && echo no
> 
> 
> This will not tell you _where_ it failed, just that it did (and even
> that only implicitely via return codes). I would prefer something along
> the lines of:
> 
> 
> Error executing "echo yes && cat exist 2>/dev/null && cat dontexist
> 2>/dev/null && echo no": "cat dontexist" failed.
> 
> Or just
> Error(1): "cat dontexist"
> 
> 
> (1) being the, optional, return code being displayed.
[...]

try:

r() { last_run_command=${(j: :)*}; "$@"; }
report() {
  exit_code=$?
  printf >&2 'Error(%d): "%s"\n' "$exit_code" "$last_run_command"
  return "$exit_code"
}

r echo yes &&
  r cat exist 2>/dev/null &&
  r cat dontexist 2>/dev/null &&
  echo no || report

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author