From: Kenny McCormack on 20 Jun 2010 11:24 Currently, I use case/esac to test to see if a string starts with a dash: case $1 in case -*) ... esac But it would be more convenient to be able to do it with "test" syntax, i.e.: [ $1 {something} - ] I think there is some syntax to do that, but I don't know it. -- (This discussion group is about C, ...) Wrong. It is only OCCASIONALLY a discussion group about C; mostly, like most "discussion" groups, it is off-topic Rorsharch [sic] revelations of the childhood traumas of the participants...
From: pk on 20 Jun 2010 11:17 Kenny McCormack wrote: > Currently, I use case/esac to test to see if a string starts with a dash: > > case $1 in > case -*) ... > esac > > But it would be more convenient to be able to do it with "test" syntax, > i.e.: > > [ $1 {something} - ] > > I think there is some syntax to do that, but I don't know it With bash, this seems to work: a='-foo' if [[ "$a" =~ ^- ]]; then echo "$a starts with a dash" fi also this works: if [[ "$a" == -* ]]; then ....
From: John Kelly on 20 Jun 2010 11:47 On Sun, 20 Jun 2010 15:24:42 +0000 (UTC), gazelle(a)shell.xmission.com (Kenny McCormack) wrote: >Currently, I use case/esac to test to see if a string starts with a dash: > >case $1 in >case -*) ... >esac > >But it would be more convenient to be able to do it with "test" syntax, >i.e.: > >[ $1 {something} - ] > >I think there is some syntax to do that, but I don't know it. Notice this: ts=abc; [ '-' == "${ts:0:1}" ] && echo $? (does not echo) ts=-abc; [ '-' == "${ts:0:1}" ] && echo $? 0 The offset 0 (first character in string) can trip you up though, the bash man page says: >Substring indexing is zero-based unless the positional parameters >are used, in which case the indexing starts at 1. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: Martin Vaeth on 20 Jun 2010 11:53 Kenny McCormack <gazelle(a)shell.xmission.com> wrote: > Currently, I use case/esac to test to see if a string starts with a dash: > [..] > But it would be more convenient to be able to do it with "test" syntax, > i.e.: > > [ $1 {something} - ] > > I think there is some syntax to do that, but I don't know it. [ "${1%-}" != "$1" ] But why should "test" be more convenient when you can also use "case" as part of an "if" or also in && or || chains: if ! case $1 in -*) false;; esac then echo "$1 starts with -" fi case $1 in -*) false;; esac || echo "$start with -"
From: John Kelly on 20 Jun 2010 12:04 On 20 Jun 2010 15:53:54 GMT, Martin Vaeth <vaeth(a)mathematik.uni-wuerzburg.de> wrote: >case $1 in -*) false;; esac || echo "$start with -" I think that's harder to read. I had to test it to be sure it worked. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
|
Next
|
Last
Pages: 1 2 3 Prev: substituting a blank character Next: Changing SAS proc format to if-then-else |