strange grep behavior... (280 Views)
Reply
Regular Advisor
Jenni Wolgast
Posts: 110
Registered: ‎02-05-2007
Message 1 of 7 (280 Views)

strange grep behavior...

This has me stumped... We are moving from a PA-RISC rp7440 to a new Itanuim blade server (I was the admin on the old server but moved on to another job and am just helping on the new one so I don't know all the specs). The old server was on 11v1/11.11, the new one is 11v3/11.31. The way our systems work some users get upper case login ID's and some get lower and I had a couple scripts that used this distinction to affect how they ran. To do this I would have lines like this:

 

echo $LOGNAME | grep -q [A-Z]

if [[ $? -eq 0 ]]

then

...

 

Worked fine for years on the old system. Started doing some testing on the new system and grep is not always returning the same, correct results... I have a lower case ID so the above test should fail, but it doesn't... Check out these results from some command line tests:

 

/home/jwolgast (p3k) <proddb> echo X${LOGNAME}X

XjwolgastX

/home/jwolgast (p3k) <proddb> echo ${LOGNAME} | grep -q [A-Z]

/home/jwolgast (p3k) <proddb> echo $?

0

/home/jwolgast (p3k) <proddb> echo ${LOGNAME} | grep -q [a-z]

/home/jwolgast (p3k) <proddb> echo $?

0

/home/jwolgast (p3k) <proddb>

 

WTH? Why am I not getting a 1 return from echo ${LOGNAME} | grep -q [A-Z]? We even tried puting single quotes around the grep chars with the same results... I'm baffled, how can this not be working? It isn't pointing to anywhere funny and I don't have an alias that is forcing a -i or anything:

 

/home/jwolgast (p3k) <proddb> which grep

/usr/bin/grep

/home/jwolgast (p3k) <proddb> alias | grep grep

ld='ll | grep '\''/'\'

o='ps -ef | grep [o]racle'

psg='ps -ef | grep'

psq='ps -ef | grep quick'

/home/jwolgast (p3k) <proddb>

 

Any ideas??? TIA!!!

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 2 of 7 (267 Views)

Re: strange grep behavior... (evil locales and ranges)

[ Edited ]

>grep is not always returning the same

 

Are you using the American Nerd (C) locale?  If not, you can't trust things.

 

>We even tried putting single quotes around the grep chars

 

(That's always a good idea.  Especially if you have a single char file in your directory.)

 

>how can this not be working?

 

You are depending on American Nerd concepts and you should set LANG=C.

 

This script will find ALL bad locales where your script will fail:

# looks for locales where upper == lower
for loc in $(locale -a); do
   echo abc | LANG=$loc grep -q "[A-Z]"  # sometimes works
   if [ $? -eq 0 ]; then
      echo "$loc: BAD"
   else
      echo "$loc: good"
   fi
   echo abc | LANG=$loc grep -q "[[:upper:]]"  # works
   if [ $? -eq 0 ]; then
      echo "$loc [:upper:]: BAD"
   else
      : echo "$loc [:upper:]: good"
   fi
done

 

You can be locale neutral by using a bigger hammer, character classes: "[[:upper:]]"

Or using just: LC_COLLATE=C

Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,074
Registered: ‎02-23-2005
Message 3 of 7 (265 Views)

Re: strange grep behavior...

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 4 of 7 (261 Views)

Re: strange grep behavior... (quoting)

>Without quotation, you could be looking at the files in the current working directory.

 

Yes but this will cause the opposite problem.  You'll not be matching, when you think you should.  (Unless you are using -v for non-matching logic.)

I.e. For a case of file "Y", you'll only match IDs that have "Y", not all upper case letters.

Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,074
Registered: ‎02-23-2005
Message 5 of 7 (253 Views)

Re: strange grep behavior... (quoting)

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 6 of 7 (251 Views)

Re: strange grep behavior... (quoting)

>Perhaps. Or, perhaps not.

 

You don't believe Jenni when she said it still failed after quoting?  ;-)

 

>including spurious matching, even without an exotic locale.

 

Yes, I forgot about the multiple file match case.

Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,074
Registered: ‎02-23-2005
Message 7 of 7 (245 Views)

Re: strange grep behavior... (quoting)

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation