1834747 Members
2542 Online
110070 Solutions
New Discussion

Re: strange behavior?

 
donald thompson_2
New Member

strange behavior?

A user encountered the following through the use of one of his scripts. But he presented the problem as follows:
as yourself and in your homedir,
export T=9 # the T is arbitrary
echo $T # should return 9
echo $T | grep [0-9] # should still return 9
cd /
echo $T | grep [0-9] # returns nothing and echo $? is 1
echo $T | grep [1-9] # returns nothing as well
echo $T | grep [2-9] # returns a 9

Root is the only place I have seen the grep fail. Why? Put quotes around the brackets and your fine. This doesn't sound normal....


This is an HP 11.0 system. The shell is KSH (altho I have seen this with CSH)
On this system, the version of ksh is 82.10.1.61 (PHCO_27418)
I have not seen this problem on systems without this version of ksh. But I have one system with this version of ksh which works 'as expected'.

thanks!
7 REPLIES 7
twang
Honored Contributor

Re: strange behavior?

Could you please try this:

# echo $SHELL
# which sh
# which ksh
# /usr/bin/sh <script>
steven Burgess_2
Honored Contributor

Re: strange behavior?

Hi

Well I have seen differing results between 2 similar servers

1st result was vice versa to yours but ok in my ~dir and / apart from

echo $T | grep [1-9]

Didn;t return enything

PHCO_22712 1.0 ksh(1) cumulative patch

On March QP machine different again

OK in ~dir and in / but also get return from all perms of grep

QPK1100 B.11.00.60.4 Quality Pack for HP-UX 11.00, March 2003


Hmmm

Steve
take your time and think things through
donald thompson_2
New Member

Re: strange behavior?

echo $SHELL results in /usr/bin/ksh
which sh results in /usr/bin/sh
which ksh results in /usr/bin/ksh

I beleive the user has tried his script using sh, but will ask.
Jordan Bean
Honored Contributor

Re: strange behavior?


Remember the shell will first attempt filename expansion when dealing with []. It will pass '[0-9]' to grep only if it doesn't find any files named 0 thru 9; Otherwise, it will pass a list of matching files (eg grep 3 5 7) to mess things up.



Jordan Bean
Honored Contributor

Re: strange behavior?


Oh yeah, may I suggest using grep -E '^[0-9]$' ?
Tim Adamson_1
Honored Contributor

Re: strange behavior?

Hi,

I agree with Jordan. If you have any files called 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 it will fail.

Check in / and I bet you find a file called 1 (and possibly 0).


Cheers!
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
donald thompson_2
New Member

Re: strange behavior?

prolog:
the reason the this test failed - was because there was a file named 1 in the root directory ofthe first box I tested. This test will fail for any directory which happens to have a numbered file in it.

thanks all for you help!