inexplicable zsh error
am 15.09.2007 00:42:28 von kj
The docs for zsh describe this special syntax for if-else statements:
if list { list } [ elif list { list } ] ... [ else { list } ]
(BTW, that's a verbatim quote.)
Ignoring the optional elif part, the special syntax given for a
one-branch if statement is:
if list { list }
and for two-branch if-else statement it is:
if list { list } else { list }
Indeed
% X=1; if [[ X = 1 ]]; { echo OK }
works fine, but
% X=1; if [[ X = 2 ]]; { echo OK } else { echo NOK }
fails with the error
zsh: parse error near `else'
I figured that this had something to do with adding a ';' somewhere,
so I added ';' everywhere I could think of, but the error persists.
I have also tried every multi-line version of this that I could
think of, but nothing worked.
What's the correct way to write an if-else statement using this
special syntax? And is this information anywhere in the docs?
TIA!
kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Re: inexplicable zsh error
am 15.09.2007 07:11:41 von Cyrus Kriticos
kj wrote:
> The docs for zsh describe this special syntax for if-else statements:
>
> if list { list } [ elif list { list } ] ... [ else { list } ]
>
> (BTW, that's a verbatim quote.)
>
http://zsh.sourceforge.net/Doc/Release/zsh_5.html#SEC22 says:
if list then list [ elif list then list ] ... [ else list ] fi
> % X=1; if [[ X = 2 ]]; { echo OK } else { echo NOK }
>
> fails with the error
My suggestion:
X=1; if [[ $X -eq 1 ]]; then echo OK; else echo NOK; fi
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: inexplicable zsh error
am 15.09.2007 10:49:00 von Stephane CHAZELAS
2007-09-14, 22:42(+00), kj:
>
>
>
> The docs for zsh describe this special syntax for if-else statements:
>
> if list { list } [ elif list { list } ] ... [ else { list } ]
That's the alternative "shortloop" form. It also says that the
condition must be one of [[ ... ]] or (( ... )) for
if/while/until.
[...]
> % X=1; if [[ X = 2 ]]; { echo OK } else { echo NOK }
Take out the ;
% X=1; if [[ X = 2 ]] { echo OK } else { echo NOK }
> What's the correct way to write an if-else statement using this
> special syntax? And is this information anywhere in the docs?
[...]
It says the condition must be terminated by "]]" or "))". If you
write:
if [[ X = 1 ]]; { ...
Then you're in the first (standard) form.
--
Stéphane
Re: inexplicable zsh error
am 15.09.2007 11:19:22 von Cyrus Kriticos
Stephane CHAZELAS wrote:
> 2007-09-14, 22:42(+00), kj:
>
> [...]
>> % X=1; if [[ X = 2 ]]; { echo OK } else { echo NOK }
>
> Take out the ;
>
> % X=1; if [[ X = 2 ]] { echo OK } else { echo NOK }
% X=1; if [[ X -eq 2 ]] { echo OK } else { echo NOK }
% zsh --version
zsh 4.3.2 (i686-pc-linux-gnu)
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: inexplicable zsh error
am 15.09.2007 11:20:36 von Cyrus Kriticos
Stephane CHAZELAS wrote:
> 2007-09-14, 22:42(+00), kj:
>
> [...]
>> % X=1; if [[ X = 2 ]]; { echo OK } else { echo NOK }
>
> Take out the ;
>
> % X=1; if [[ X = 2 ]] { echo OK } else { echo NOK }
% X=1; if [[ $X = 2 ]] { echo OK } else { echo NOK }
% zsh --version
zsh 4.3.2 (i686-pc-linux-gnu)
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: inexplicable zsh error
am 15.09.2007 11:27:29 von Stephane CHAZELAS
2007-09-15, 11:20(+02), Cyrus Kriticos:
> Stephane CHAZELAS wrote:
>> 2007-09-14, 22:42(+00), kj:
>>
>> [...]
>>> % X=1; if [[ X = 2 ]]; { echo OK } else { echo NOK }
>>
>> Take out the ;
>>
>> % X=1; if [[ X = 2 ]] { echo OK } else { echo NOK }
>
> % X=1; if [[ $X = 2 ]] { echo OK } else { echo NOK }
>
> % zsh --version
> zsh 4.3.2 (i686-pc-linux-gnu)
Those forms have been in zsh for ages.
Try with zsh -f. Then, it may be interesting to know what in
your ~/.z* or /etc/z* triggers the behavior you're observing.
$ if [[ X = 2 ]] { echo OK } else { echo NOK }
NOK
$ echo $ZSH_VERSION
4.2.4
$ if [[ X = 2 ]] { echo OK } else { echo NOK }
NOK
$ echo $ZSH_VERSION
4.3.4
--
Stéphane
Re: inexplicable zsh error
am 15.09.2007 11:56:57 von Cyrus Kriticos
Stephane CHAZELAS wrote:
> 2007-09-15, 11:20(+02), Cyrus Kriticos:
>> Stephane CHAZELAS wrote:
>>>
>>> % X=1; if [[ X = 2 ]] { echo OK } else { echo NOK }
>> % X=1; if [[ $X = 2 ]] { echo OK } else { echo NOK }
>>
>> % zsh --version
>> zsh 4.3.2 (i686-pc-linux-gnu)
>
> Those forms have been in zsh for ages.
>
> Try with zsh -f. Then, it may be interesting to know what in
> your ~/.z* or /etc/z* triggers the behavior you're observing.
ok.
% cat /etc/zsh/* ~/.zshrc | grep -vE '^(#|$)'
READNULLCMD=${PAGER:-/usr/bin/pager}
if [[ "$TERM" != emacs ]]; then
[[ -z "$terminfo[kdch1]" ]] || bindkey -M emacs "$terminfo[kdch1]" delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]"
beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M emacs "$terminfo[kend]" end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M emacs "$terminfo[kich1]"
overwrite-mode
[[ -z "$terminfo[kdch1]" ]] || bindkey -M vicmd "$terminfo[kdch1]"
vi-delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M vicmd "$terminfo[khome]"
vi-beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M vicmd "$terminfo[kend]" vi-end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M vicmd "$terminfo[kich1]"
overwrite-mode
[[ -z "$terminfo[cuu1]" ]] || bindkey -M viins "$terminfo[cuu1]"
vi-up-line-or-history
[[ -z "$terminfo[cuf1]" ]] || bindkey -M viins "$terminfo[cuf1]"
vi-forward-char
[[ -z "$terminfo[kcuu1]" ]] || bindkey -M viins "$terminfo[kcuu1]"
vi-up-line-or-history
[[ -z "$terminfo[kcud1]" ]] || bindkey -M viins "$terminfo[kcud1]"
vi-down-line-or-history
[[ -z "$terminfo[kcuf1]" ]] || bindkey -M viins "$terminfo[kcuf1]"
vi-forward-char
[[ -z "$terminfo[kcub1]" ]] || bindkey -M viins "$terminfo[kcub1]"
vi-backward-char
[[ "$terminfo[kcuu1]" == ""* ]] && bindkey -M viins
"${terminfo[kcuu1]/O/[}" vi-up-line-or-history
[[ "$terminfo[kcud1]" == ""* ]] && bindkey -M viins
"${terminfo[kcud1]/O/[}" vi-down-line-or-history
[[ "$terminfo[kcuf1]" == ""* ]] && bindkey -M viins
"${terminfo[kcuf1]/O/[}" vi-forward-char
[[ "$terminfo[kcub1]" == ""* ]] && bindkey -M viins
"${terminfo[kcub1]/O/[}" vi-backward-char
[[ "$terminfo[khome]" == ""* ]] && bindkey -M viins
"${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == ""* ]] && bindkey -M viins "${terminfo[kend]/O/[}"
end-of-line
[[ "$terminfo[khome]" == ""* ]] && bindkey -M emacs
"${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == ""* ]] && bindkey -M emacs "${terminfo[kend]/O/[}"
end-of-line
fi
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin \
/usr/sbin /usr/bin /sbin /bin /usr/X11R6/bin
unalias run-help
autoload run-help
autoload -U compinit
compinit
> $ if [[ X = 2 ]] { echo OK } else { echo NOK }
> NOK
with "zsh -f":
% X=1; if [[ X = 2 ]] { echo OK } else { echo NOK }
NOK
% X=1; if [[ X = 1 ]] { echo OK } else { echo NOK }
NOK
% X=1; if [[ $X = 2 ]] { echo OK } else { echo NOK }
NOK
% X=1; if [[ $X = 1 ]] { echo OK } else { echo NOK }
OK
% X=1; if [[ X -eq 2 ]] { echo OK } else { echo NOK }
NOK
% X=1; if [[ X -eq 1 ]] { echo OK } else { echo NOK }
OK
% X=1; if [[ $X -eq 2 ]] { echo OK } else { echo NOK }
NOK
% X=1; if [[ $X -eq 1 ]] { echo OK } else { echo NOK }
OK
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: inexplicable zsh error
am 15.09.2007 13:30:41 von kj
In Stephane CHAZELAS writes:
>2007-09-14, 22:42(+00), kj:
>>
>>
>>
>> The docs for zsh describe this special syntax for if-else statements:
>>
>> if list { list } [ elif list { list } ] ... [ else { list } ]
>That's the alternative "shortloop" form. It also says that the
>condition must be one of [[ ... ]] or (( ... )) for
>if/while/until.
OK, I see... I was fooled (badly) by the fact that this *appears*
to work:
% X=1; if [[ X = 1 ]]; { echo OK }
OK
But so does this:
% X=2; if [[ X = 1 ]]; { echo OK }
OK
I wish I'd tried that; I might have gotten a clue...
Thanks.
kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Re: inexplicable zsh error
am 15.09.2007 13:57:57 von Stephane CHAZELAS
2007-09-15, 11:30(+00), kj:
[...]
> OK, I see... I was fooled (badly) by the fact that this *appears*
> to work:
>
> % X=1; if [[ X = 1 ]]; { echo OK }
> OK
>
> But so does this:
>
> % X=2; if [[ X = 1 ]]; { echo OK }
> OK
>
> I wish I'd tried that; I might have gotten a clue...
[...]
It should be noted that [[ a = b ]] is a string comparison, "X"
is neither "1" nor "2", whatever the value of a variable called
X is. So [[ X = 1 ]] or [[ X = 2 ]] will always be false.
(( X == 1 )) is an arithmetic comparison. Here the shell expands
the values of variables even if there's no $ in front of them.
You can also do [[ X -eq 1 ]] for an arithmetic comparison.
Note that the fact that
if [[ X = 1 ]]; { echo OK }
returns OK, looks like a bug to me. It should instead prompt you
with something like:
if> ...
and that until you enter "then", because you've not reached the
end of the condition part of your if statement.
It does that for me (4.3.4), so it may have been a bug fixed in
a recent version of zsh. What version of zsh are you using?
--
Stéphane