Operating System - HP-UX
1748205 Members
3446 Online
108759 Solutions
New Discussion юеВ

Re: strange grep behavior...

 
Jenni Wolgast
Regular Advisor

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!!!

 

6 REPLIES 6
Dennis Handly
Acclaimed Contributor

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

>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

Steven Schweda
Honored Contributor

Re: strange grep behavior...

 
Dennis Handly
Acclaimed Contributor

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.

Steven Schweda
Honored Contributor

Re: strange grep behavior... (quoting)

 
Dennis Handly
Acclaimed Contributor

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.

Steven Schweda
Honored Contributor

Re: strange grep behavior... (quoting)