|
Home > Archive > Unix Shell > February 2006 > zsh: how to make a function fail?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
zsh: how to make a function fail?
|
|
|
|
I am trying to define a zsh function that fails (i.e. exits setting
$? to some nonzero value) when certain condition holds, but I can't
figure out how to do it. If I do something like:
foo () {
exit 1
}
the enclosing shell terminates when one executes the resulting foo
command. But if I do
foo () {
return 1;
}
the command terminates normally without setting $?.
What must I do?
Thanks!
kj
PS: It never ceases to amaze me how *insanel difficult* is to find
out basic information about shell programming, and zsh programming
in particular. I spent a good 45 minutes looking for the answer
to this *elementary* question before I posted. It's truly depressing.
If anyone knows of a decent, *well-organized* source of info on
zsh programming, please let me know. (I suspect I know all the
standard sources, and if so, I can confidently say that they all
suck, badly, but I don't understand why this is so.)
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
| |
| Stachu 'Dozzie' K. 2006-02-13, 6:04 pm |
| On 13.02.2006, kj <socyl@987jk.com.invalid> wrote:
>
>
>
> I am trying to define a zsh function that fails (i.e. exits setting
> $? to some nonzero value) when certain condition holds, but I can't
> figure out how to do it. If I do something like:
>
> foo () {
> exit 1
> }
>
> the enclosing shell terminates when one executes the resulting foo
> command. But if I do
>
> foo () {
> return 1;
> }
>
> the command terminates normally without setting $?.
>
> What must I do?
You must have bug somewhere else.
#v+
[dozzie@hans dozzie]$ echo $ZSH_VERSION
4.2.5
[dozzie@hans dozzie]$ foo() { return 1; }
[dozzie@hans dozzie]$ foo; echo $?
1
[dozzie@hans dozzie]$
#v-
> PS: It never ceases to amaze me how *insanel difficult* is to find
> out basic information about shell programming, and zsh programming
> in particular.
Really? I think `info zsh' is good enough for zsh programming and
`man bash' for shell programming in general (at least I learned that
this way).
> I spent a good 45 minutes looking for the answer
> to this *elementary* question before I posted. It's truly depressing.
Why didn't you simply check your statement ("foo() { return 1; }") in
clean environment?
--
Feel free to correct my English
Stanislaw Klekot
| |
| Stephane Chazelas 2006-02-13, 6:04 pm |
| On Mon, 13 Feb 2006 17:16:19 +0000 (UTC), kj wrote:
[...]
> the enclosing shell terminates when one executes the resulting foo
> command. But if I do
>
> foo () {
> return 1;
> }
>
> the command terminates normally without setting $?.
[...]
That should work.
maybe you have an old zsh versions that had problems with
precmd() functions that would reset $?.
foo() {
return 1
}
foo() {
false
}
When either of those functions are called the exit status should
be set to 1.
> PS: It never ceases to amaze me how *insanel difficult* is to find
> out basic information about shell programming, and zsh programming
> in particular. I spent a good 45 minutes looking for the answer
> to this *elementary* question before I posted. It's truly depressing.
> If anyone knows of a decent, *well-organized* source of info on
> zsh programming, please let me know. (I suspect I know all the
> standard sources, and if so, I can confidently say that they all
> suck, badly, but I don't understand why this is so.)
Learn how to use the info command efficiently. Especially the
"g", "i" and "m" keys and completion.
For instance, here, I would have typed:
$ info zsh
ifun<Tab><Tab>
or
gfun<Tab><Tab>
Pick the topic that matches best what you're looking for.
ireturn<Return>
leads you to:
return [ N ]
Causes a shell function or . script to return to the invoking
script with the return status specified by N. If N is omitted,
the return status is that of the last command executed.
If return was executed from a trap in a TRAPNAL function, the
effect is different for zero and non-zero return status. With zero
status (or after an implicit return at the end of the trap), the
shell will return to whatever it was previously processing; with a
non-zero status, the shell will behave as interrupted except that
the return status of the trap is retained. Note that the numeric
value of the signal which caused the trap is passed as the first
argument, so the statement `return $((128+$1))' will return the
same status as if the signal had not been trapped.
For a more user-friendly manual than the reference one, see the
user's guide: http://zsh.dotsrc.org/Guide/ which can be read in
a variety of formats.
See http://zsh.dotsrc.org/Guide/zshguide03.html#l48 for the
functions, though it doesn't seem to mention the exit status.
See http://zsh.dotsrc.org/Guide/zshguide03.html#l41 for details
About return.
--
Stephane
|
|
|
|
|