Operating System - HP-UX
1836417 Members
2434 Online
110100 Solutions
New Discussion

Error in user setting making grep behave badly

 
SOLVED
Go to solution
Charles Frenette
New Member

Error in user setting making grep behave badly

HI,
I have an unexpected behaviour for one particular user on my HP-US 11i system
The following command: echo 7 | grep [0-9] is not working properly in a script or on the prompt.
example:
maestro@[dm3czo]echo 7 | grep [0-9]
1: TERM=vt100
1: MAESTROLINES=0
1: PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/opt/maestro:/opt/maestro/bin:.:/opt/syncsort380/lib:/stage/syncsort350/lib ; export PATH
1:SHLIB_PATH=/opt/syncsort/lib:/opt/syncsort380/lib/:/stage/syncsort350/lib; export SHLIB_PATH
1: trap "echo 'logout'" 0
1: umask 007
1:tput sgr0
1:tput sgr0

This is like the content of the .profile is passed to the grep command.

But this command is working just fine for others user:

cfrenett@dm3czo:/home/cfrenett>echo 7 | grep [0-9]
7

I have try to locate the difference in the settings of theses users and I cannot find any major differences.
Do you have an idea where I can look to correct this problem?
Thanks,
Charles.
5 REPLIES 5
Mel Burslan
Honored Contributor

Re: Error in user setting making grep behave badly

echo 7 command converting itself to env output did not make much sense, but chek to see if the shells of the two users are the same.
________________________________
UNIX because I majored in cryptology...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Error in user setting making grep behave badly

Grep is behaving exactly as it is supposed to; you scripting is sloppy because consider the case when there is a file (or files) "0" - "9" in the current working directory. The shell dutifully expands your pattern and supplies them as your argument to grep.

The fix is quite simple
echo 7 | grep '[0-9]'

although a more rigourous test would also anchor the target pattern:

echo "7" | grep -E '^[0-9]$'

This will only match a single digit.
If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

Re: Error in user setting making grep behave badly

Start with verifying the actual commands:

type grep
type echo
type 7

The type command (actually whence -v) tells you what the string will produce when the shell sees that string. The results should be:

> grep is a tracked alias for /usr/bin/grep
> echo is a shell builtin.
> 7 not found.

then look at the environment by typing the commands:

alias
set

Look for strange character strings in the alias and set commands.

BTW: the PATH statement has several concerns:

/bin hasn't existed for more than a dozen years. The correct path is /usr/bin. /bin is a temporary transition link.

/usr/local/bin should be checked for the correct permissions. The default is 777 which is unacceptable for PATH usage. Make sure /usr/local/bin is 775 or 755.

:.: is in the path. All seasoned admins will agree that having . (the current working directory) is a bad security risk.


Bill Hassell, sysadmin
Peter Nikitka
Honored Contributor

Re: Error in user setting making grep behave badly

Hi,

Clay's response (matching a filename surely put you to the cause of your problem. I want to add:
- your problem is NOT user specific - if you change to the path, where the command was called by that user, every user with read acces to that directory would get that result.
- to get an exact match, you can use
echo 7 | grep -x '[0-9]'
The option '-x' anchors your pattern at the beginning and at the end of a line lile '^[0-9]$' but is valid for the whole match:
egrep -x '[0-9]|aa' matches a line containing exactly one digit or a single 'aa'.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Charles Frenette
New Member

Re: Error in user setting making grep behave badly

Thanks to all of you.
The problem was that a file 1 was existing in the home of that user and contains the output of the env commant.
And the correction to the grep resolve my issue.

Thanks again.
Charles.