How to use [[ string =~ regexp ]]?

2006-05-21 Thread Peter Volkov
Hello.

Please CC my email to answers as I'm not subscribed to the list.

I have problems using =~ operator. I've tried to search for answer, but
failed. I'm using GNU bash, version 3.1.17. Can anybody give me some
examples of usage?

I really do not understand why

$ [[ string =~ [a-z] ]]  echo something
something

echo me something. IIUC the regular expression [a-z] matches any single
letter, so how string string matches one letter?

Seems that I missed the point, or did I encounter bug?

Thank you for any help,
Peter.


signature.asc
Description: This is a digitally signed message part
___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


locally declared arrays do not act as arrays

2006-05-21 Thread Andrew Stitt
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H  -I.  -I. -I./include -I./lib   -O -march=pentium-m -mmmx 
-mfpmath=sse -msse2 -pipe -ffast-math -funroll-loops -O3
uname output: Linux courier 2.6.11 #6 Sun May 8 23:59:20 PDT 2005 i686 unknown 
unknown GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 3.0
Patch Level: 16
Release Status: release

Description:
when I declare a local variable as an array it still acts as a scalar,
not as an array.

I discovered that if I seperate the local declaration from the
initialization, the variable properly acts as an array.

Repeat-By:

f() {
  local B=(a b)
  echo $B
  C=(a b)
  echo $C
}
A=(a b)
echo $A
f

Produces:
a
(a b)
a

Workaround:
split the declaration and initialization:
local B
B=(a b)



___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Paul Jarc
Peter Volkov [EMAIL PROTECTED] wrote:
 $ [[ string =~ [a-z] ]]  echo something
 something

[a-z] matches only one charater, but the pattern is not required to
match against the entire string.  You can force it to match the whole
string by using ^ to anchor the pattern to the beginning of the
string, and $ to anchor it to the end:
[[ string =~ ^[a-z]$ ]]


paul


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Mike Stroyan

On 5/21/06, Peter Volkov [EMAIL PROTECTED] wrote:


I have problems using =~ operator. I've tried to search for answer, but
failed. I'm using GNU bash, version 3.1.17. Can anybody give me some
examples of usage?

I really do not understand why

$ [[ string =~ [a-z] ]]  echo something
something

echo me something. IIUC the regular expression [a-z] matches any single
letter, so how string string matches one letter?


The =~ regexp match will match a substring by default.  You can use ^ and $
to anchor the expression to the start and end of the string.  You
won't get a match
with
[[ string =~ ^[a-z]$ ]]  echo match
But you will get a match with
[[ string =~ ^[a-z]{6}$ ]]  echo match
because it matches the correct number of characters.

--
Mike Stroyan
[EMAIL PROTECTED]


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Chet Ramey
Peter Volkov wrote:
 Hello.
 
 Please CC my email to answers as I'm not subscribed to the list.
 
 I have problems using =~ operator. I've tried to search for answer, but
 failed. I'm using GNU bash, version 3.1.17. Can anybody give me some
 examples of usage?
 
 I really do not understand why
 
 $ [[ string =~ [a-z] ]]  echo something
 something
 
 echo me something. IIUC the regular expression [a-z] matches any single
 letter, so how string string matches one letter?
 
 Seems that I missed the point, or did I encounter bug?

regexec(3) returns success for that pattern, so the [[ command succeeds.
I'm not sure why, unless it thinks it doesn't have to match the entire
string.

The real question is whether or not quoting the pattern should work as
it does with other [[ pattern matching operators, which is to quote any
characters special to the matching engine.  I think it should, for
consistency.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
   Live Strong.  No day but today.
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Chet Ramey
Paul Jarc wrote:
 Peter Volkov [EMAIL PROTECTED] wrote:
 $ [[ string =~ [a-z] ]]  echo something
 something
 
 [a-z] matches only one charater, but the pattern is not required to
 match against the entire string.  You can force it to match the whole
 string by using ^ to anchor the pattern to the beginning of the
 string, and $ to anchor it to the end:
 [[ string =~ ^[a-z]$ ]]

Paul is correct; that is the piece I was missing (I am distracted by
the Cleveland-Detroit game).

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
   Live Strong.  No day but today.
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: locally declared arrays do not act as arrays

2006-05-21 Thread Chet Ramey
Andrew Stitt wrote:
 Configuration Information [Automatically generated, do not change]:
 Machine: i686
 OS: linux-gnu
 Compiler: gcc
 Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' 
 -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' 
 -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
 -DHAVE_CONFIG_H  -I.  -I. -I./include -I./lib   -O -march=pentium-m -mmmx 
 -mfpmath=sse -msse2 -pipe -ffast-math -funroll-loops -O3
 uname output: Linux courier 2.6.11 #6 Sun May 8 23:59:20 PDT 2005 i686 
 unknown unknown GNU/Linux
 Machine Type: i686-pc-linux-gnu
 
 Bash Version: 3.0
 Patch Level: 16
 Release Status: release
 
 Description:
 when I declare a local variable as an array it still acts as a scalar,
 not as an array.
 
 I discovered that if I seperate the local declaration from the
 initialization, the variable properly acts as an array.

This was fixed in bash-3.1.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
   Live Strong.  No day but today.
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Bob Proulx
Chet Ramey wrote:
 Peter Volkov wrote:
  Please CC my email to answers as I'm not subscribed to the list.
  $ [[ string =~ [a-z] ]]  echo something
  something
 ...
 The real question is whether or not quoting the pattern should work as
 it does with other [[ pattern matching operators, which is to quote any
 characters special to the matching engine.  I think it should, for
 consistency.

Could you give an example of how you think it should work?  (Just
curious and trying to keep up.)

Are you thinking that this should match:

  [[ string =~ [a-z] ]]  echo matched || echo not matched

Which this should not match?

  [[ string =~ [a-z] ]]  echo matched || echo not matched

Thanks
Bob


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Chet Ramey
Bob Proulx wrote:
 Chet Ramey wrote:
 Peter Volkov wrote:
 Please CC my email to answers as I'm not subscribed to the list.
 $ [[ string =~ [a-z] ]]  echo something
 something
 ...
 The real question is whether or not quoting the pattern should work as
 it does with other [[ pattern matching operators, which is to quote any
 characters special to the matching engine.  I think it should, for
 consistency.
 
 Could you give an example of how you think it should work?  (Just
 curious and trying to keep up.)

The documentation for [[ says this about the == and != operators,
which take patterns as the right-hand side:

Any part of the pattern may be quoted to force it to
be matched as a string.

It seems reasonable that quoting any part of the rhs to the =~
operator should cause it to behave in the same manner.

Since the arguments to [[ don't undergo any of the expansions that
require quoting to protect them, there's no reason for =~ to act
differently than the other operators that do pattern matching.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
   Live Strong.  No day but today.
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Bob Proulx
Chet Ramey wrote:
 It seems reasonable that quoting any part of the rhs to the =~
 operator should cause it to behave in the same manner.
 
 Since the arguments to [[ don't undergo any of the expansions that
 require quoting to protect them, there's no reason for =~ to act
 differently than the other operators that do pattern matching.

That makes sense to me.  Thanks for the clarification and education.

Bob


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash