* trying to create a "|| failed" function
@ 2013-03-15 1:39 TJ Luoma
2013-03-15 1:55 ` Alex Satrapa
0 siblings, 1 reply; 4+ messages in thread
From: TJ Luoma @ 2013-03-15 1:39 UTC (permalink / raw)
To: Zsh-Users List
[-- Attachment #1: Type: text/plain, Size: 892 bytes --]
I'm trying to create a function which I can define in .zshenv which will
let me log when a command fails.
Here's what I end up doing most of the time:
LOG=$HOME/.foo.log
if ((! foo ))
then
echo "$0: command 'foo' failed" | tee -a "$LOG"
exit 1
fi
if ((! bar ))
then
echo "$0: command 'bar' failed" | tee -a "$LOG"
exit 1
fi
Here is what I'd like to be able to do instead
foo || failed
bar || failed
if 'failed' get called, I want the shell script (whatever called `foo` and
`bar`) to exit 1, and I want to log the name of the command that failed
(and, ideally, the name of the script that `foo` or `bar` was in when it
failed)
(These are simple examples, of course, but it gets more complicated when
`foo` or `bar` are longer commands, or part of a loop, etc.)
Is there a way to do this?
If so, I'd appreciate any hints suggestions, or examples you could give.
Thanks
TjL
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: trying to create a "|| failed" function
2013-03-15 1:39 trying to create a "|| failed" function TJ Luoma
@ 2013-03-15 1:55 ` Alex Satrapa
2013-03-15 3:19 ` TJ Luoma
0 siblings, 1 reply; 4+ messages in thread
From: Alex Satrapa @ 2013-03-15 1:55 UTC (permalink / raw)
To: Zsh-Users List
On 15/03/2013, at 12:39 , TJ Luoma <luomat@gmail.com> wrote:
> foo || failed
>
> bar || failed
You could pass a parameter into failed:
foo || failed 'foo'
bar || failed 'bar'
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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: trying to create a "|| failed" function
2013-03-15 1:55 ` Alex Satrapa
@ 2013-03-15 3:19 ` TJ Luoma
2013-03-15 5:08 ` Phil Pennock
0 siblings, 1 reply; 4+ messages in thread
From: TJ Luoma @ 2013-03-15 3:19 UTC (permalink / raw)
To: Alex Satrapa; +Cc: Zsh-Users List
[-- Attachment #1: Type: text/plain, Size: 1140 bytes --]
On Thu, Mar 14, 2013 at 9:55 PM, Alex Satrapa <grail@goldweb.com.au> 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`
EXIT="$?"
if [[ "$EXIT" != "0" ]]
then
# failed
echo "$NAME [$TIME]: $@ failed\nOUTPUT: >$OUTPUT<\nExit: >$EXIT<" | tee -a
"$LOG"
exit $EXIT
fi
}
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 :-)
Thanks!
TjL
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: trying to create a "|| failed" function
2013-03-15 3:19 ` TJ Luoma
@ 2013-03-15 5:08 ` Phil Pennock
0 siblings, 0 replies; 4+ messages in thread
From: Phil Pennock @ 2013-03-15 5:08 UTC (permalink / raw)
To: TJ Luoma; +Cc: Alex Satrapa, Zsh-Users List
On 2013-03-14 at 23:19 -0400, TJ Luoma wrote:
> OUTPUT=`$@ 2>&1`
> # failed
> echo "$NAME [$TIME]: $@ failed\nOUTPUT: >$OUTPUT<\nExit: >$EXIT<" | tee -a
> "$LOG"
> Anything I can or "should" do differently in the function above?
OUTPUT=`"$@" 2>&1`
Because: $@ drops empty elements, so passing empty parameters to a
command will be dropped, which leads to interesting debugging sessions.
Then in the echo line I quoted, you probably want $* instead of $@
because this is an echo message and you don't want it split apart.
% foo() { echo "snert $@ ni" }
% foo alpha beta
snert alpha ni snert beta ni
%
That's caused by "setopt rc_expand_param" being turned on.
So: $@ is a good habit to get into using, but you usually want "$@",
even in zsh, and for echo situations, you still want $*.
-Phil
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-03-15 5:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-15 1:39 trying to create a "|| failed" function TJ Luoma
2013-03-15 1:55 ` Alex Satrapa
2013-03-15 3:19 ` TJ Luoma
2013-03-15 5:08 ` Phil Pennock
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).