From: Martin Vaeth on
John Kelly <jak(a)isp2dial.com> wrote:
><vaeth(a)mathematik.uni-wuerzburg.de> wrote:
>
>>case $1 in -*) false;; esac || echo "$start with -"
>
> I think that's harder to read.

Maybe, but it is standard and fast.
More important: You can use it like "test" in loops like

while ! case $arg in -*) false;; esac
do set_something_and_read_next_arg
done
From: Kenny McCormack on
In article <2067856.Hz00ifERbk(a)xkzjympik>, pk <pk(a)pk.invalid> wrote:
....
>also this works:
>
>if [[ "$a" == -* ]]; then
>...

Thanks. This is what I ended up using.

Note that it also works with "!=", which is what I really needed.

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

From: Janis Papanagnou on
On 20/06/10 18:22, Martin Vaeth wrote:
> John Kelly <jak(a)isp2dial.com> wrote:
>> <vaeth(a)mathematik.uni-wuerzburg.de> wrote:
>>
>>> case $1 in -*) false;; esac || echo "$start with -"
>>
>> I think that's harder to read.
>
> Maybe, but it is standard and fast.
> More important: You can use it like "test" in loops like
>
> while ! case $arg in -*) false;; esac
> do set_something_and_read_next_arg
> done

A few notes...

If, as your example seems to show, you intend to parse options you
should prefer using getopts, which is much clearer and more powerful

while getopts ...
do ...
done

Then, why did you use double negation, /false/ and /!/, which makes
your construct even less legible. Another point is that the negation
operator /!/ isn't guaranteed to be supported in non-modern shells.
And if you use a modern shell you can as well use the [[...]] test
construct which is a lot more legible that the more portable case.
Though, in case of using case, you may want to use this syntax

case $arg in (-*) false;; esac

(mind the parenthesis) which is more robust in certain lexical shell
contexts.

(There was another point I wanted to mention that I forgot... - anyway.)

Janis
From: Sven Mascheck on
Janis Papanagnou wrote:

> case $arg in (-*) false;; esac
>
> (mind the parenthesis) which is more robust in certain lexical shell
> contexts.

....namely the parenthesis form of command substitution
From: Wayne on
On 6/21/2010 9:35 AM, Janis Papanagnou wrote:
> On 20/06/10 18:22, Martin Vaeth wrote:
>> John Kelly <jak(a)isp2dial.com> wrote:
>>> <vaeth(a)mathematik.uni-wuerzburg.de> wrote:
>>>
>>>> case $1 in -*) false;; esac || echo "$start with -"
>>>
>>> I think that's harder to read.
>>
>> Maybe, but it is standard and fast.
>> More important: You can use it like "test" in loops like
>>
>> while ! case $arg in -*) false;; esac
>> do set_something_and_read_next_arg
>> done
>
> A few notes...
>
> If, as your example seems to show, you intend to parse options you
> should prefer using getopts, which is much clearer and more powerful
>
> while getopts ...
> do ...
> done
>
> Then, why did you use double negation, /false/ and /!/, which makes
> your construct even less legible.


The case construct returns 0 (true) if no cases match. So you
need to return false for a match, and then negate the result to have
true for a match and false for no match. I think the construct

if case "$word" in (-*) true;; esac

returns true (0) whether or not there is a match.

> Another point is that the negation
> operator /!/ isn't guaranteed to be supported in non-modern shells.

You make a good point. The case statement and "!" negation are part of
POSIX and thus are very portable across all modern and most historical
shells. The "[[ ... ]]" syntax is very useful and supported by the most
popular shells today, but it is not standardized. Solaris /bin/sh for
example doesn't support "[[ ... ]]", but doesn't support "!" either.

> And if you use a modern shell you can as well use the [[...]] test
> construct which is a lot more legible that the more portable case.

The shells of the 1970s and 1980s were brilliant, but not perfect and
time marches on. Today it seems one must choose between historical
compatibility and modern functionality and readability. I agree with
Janis, using "[[ ... ]]" for pattern matching is better if you use a
modern shell.

> Though, in case of using case, you may want to use this syntax
>
> case $arg in (-*) false;; esac
>
> (mind the parenthesis) which is more robust in certain lexical shell
> contexts.

But not all historical shells support this. I don't think Solaris /bin/sh
does.

>
> (There was another point I wanted to mention that I forgot... - anyway.)
>
> Janis

--
Wayne