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

Re: expansion in conditional expression patterns




On 13.10.2009, at 11:07, Peter Stephenson wrote:

On Mon, 12 Oct 2009 19:51:14 +0200
Sebastian Stark <seb-zsh@xxxxxxxxxxx> wrote:
% limit=5
% if [[ 1 != <2-$limit> ]]; then print outside; fi
zsh: parse error: condition expected: 1

What part of this do I misunderstand?

Logically this ought to work: parameter substitution is performed before pattern matching or globbing. However, "<" is rather overloaded and when the expression is parsed the shell only decides it's a numeric glob if the expression is in the strict form with digits. It could probably be made smarter in this case, where "<" can't be a redirection, but as a knock-on effect pattern matching would have to be made smarter about deciding later
on if the expression was a numeric glob, so it's not entirely trivial.

Thanks for clarifying this.

Your best bet for now is to do numeric comparisons with -ge and -le or with <= and >= in (( ... )) expressions. You can use an "eval" but that's probably
a bit too ugly for what you're trying to do.

I just did this:

  if [[ ! ( ($cmdnum -ge 1) && ($cmdnum -le $limit) ) ]]

Maybe I should have asked differently because there might be a better solution for what I'm trying to do. I have an array of commands, $cmds, and a user supplied $cmdnum. Now I want to check wether the user entered a valid number. ($limit would be $#cmds in this example).

Is there a better way to check wether the user entered a) a number and b) in the range 1..$#cmds?


Sebastian



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