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

Re: trying to create a "|| failed" function

On Thu, Mar 14, 2013 at 9:55 PM, Alex Satrapa <grail@xxxxxxxxxxxxxx> wrote:

> Or go even further and have "failed" run the command:
>   notify_if_failed foo with parameters
> Then notify_if_filed would contain run $@, evaluate the result and return
> the appropriate exit code, performing any required logging along the way.

Hrm. I hadn't thought about that.

Here's what I have:

log () {

export LOG="$HOME/Desktop/$NAME.log.txt"

zmodload zsh/datetime

TIME=$(strftime %Y-%m-%d--%H.%M.%S "$EPOCHSECONDS")

OUTPUT=`$@ 2>&1`


if [[ "$EXIT" != "0" ]]

 # failed
echo "$NAME [$TIME]: $@ failed\nOUTPUT: >$OUTPUT<\nExit: >$EXIT<" | tee -a

exit $EXIT

so now I can run

log foo

and have it logged and exit if it necessary.  This seems very useful.

Hrm. Actually, I can add logging of successful commands which have output
as well. Again, very handy.

Anything I can or "should" do differently in the function above?  Always
open to ideas, as I usually find that I don't think of the most efficient
way of doing things, I just find something that works (or seems to) and go
with that :-)



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