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

Re: Honoring a command



On Apr 4,  1:16pm, DervishD wrote:
}
} The number of beers I owe you is growing dangerously ;)

Especially considering that I rarely drink beer.  Send me a six-pack of
some unusual Spanish soft drink, or something. ;-}
  
}     alias scriptinit=$'emulate -L zsh ; trap \'return $LINENO\' ZERR'

An interesting tidbit I just noticed -- if you read the script with the
"source" or "." commands, $LINENO is reset to 1 when the trap runs, so
you don't get as useful a return value.

} The problem is that it doesn't work with the above usage of
} verbosely_watch, because the trap is never run due to the OR list. I
} must replace:
} 
}     { whatever.command 2> /dev/null || print "Error message"}
} 
}     by:
} 
}     { whatever.command 2> /dev/null || print "Error message";false}

Actually that won't work either, because that _always_ executes "false".
I think you meant

  { whatever.command 2> /dev/null || { print "Error message";false } }

} but then 'verbosely_watch' has no purpose at all!

You could write another little function:

    verbosely_fail() {
	local ret=$?
	[[ -p /dev/fd/1 ]] && print "$*"
	return ret
    }

Now this works:

  { whatever.command || verbosely_fail "Error message" } 2>/dev/null |
  	verbosely_watch "Doing whatever"

If you DON'T pipe to verbosely_watch, then verbosely_fail is silent and the
ZERR trap returns the line number as $?.  (Even if you do pipe it, the line
number is stored in $pipestatus[1], which may be useful for other tricks.)

} I think I'd better use 'scriptinit' only for muted
} scripts and verbosely_watch for the rest, using TRAPZERR for the
} error message. Is that a good idea?

That would work as well, but it means the error message is the same for
every command (unless you re-assign it somehow each time).



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