# FreeBSD 9.0-RC2: grep bug?



## petervanderdoes (Nov 18, 2011)

I just upgraded my server to FreeBSD 9.0-RC2 64bit and may have stumbled upon a bug.
I SSH into the box and run the following command from cli:


```
ps -u
```

Output

```
USER    PID  %CPU %MEM    VSZ    RSS  TT  STAT STARTED    TIME COMMAND
pdoes  1537   0.0  0.1  17572   3876   1  Is    1:37PM 0:00.15 -bash (bash)
pdoes 49162   0.0  0.1  17572   3920   2  Ss    1:48PM 0:00.44 -bash (bash)
pdoes 49691   0.0  0.0  14328   1480   2  R+    3:12PM 0:00.00 ps -u
```


```
ps -u|grep bash
```

Output:

```
USER    PID  %CPU %MEM    VSZ    RSS  TT  STAT STARTED    TIME COMMAND
pdoes  1537   0.0  0.1  17572   3876   1  Is    1:37PM 0:00.15 -bash (bash)
pdoes 49162   0.0  0.1  17572   3920   2  Ss    1:48PM 0:00.44 -bash (bash)
pdoes 49691   0.0  0.0  14328   1480   2  R+    3:12PM 0:00.00 ps -u
```


Now I resize the window so not the whole line is displayed:


```
ps -u
```

Output

```
USER    PID  %CPU %MEM    VSZ    RSS  TT  STAT STARTED    TIME COMM
AND
pdoes  1537   0.0  0.1  17572   3876   1  Is    1:37PM 0:00.15 -ba
pdoes 49162   0.0  0.1  17572   3920   2  Ss    1:48PM 0:00.45 -ba
pdoes 49702   0.0  0.0  14328   1480   2  R+    3:15PM 0:00.01 ps
```


```
ps -u|grep bash
```

Output
Nothing

Is this a bug or is this standard behavior in FreeBSD? This happens with gnu grep as well as bsdgrep.

I only recently installed FreeBSD so I'm not sure if this is normal behavior or a bug. If I run the same command with grep on a Ubuntu box it will show the lines with bash. Both are grep 2.9


----------



## oops (Nov 18, 2011)

Could this be similar to ... ?
http://docs.freebsd.org/cgi/mid.cgi?A6C97867-DFDB-4253-868B-779E03E2ADA4


----------



## wblock@ (Nov 19, 2011)

It's really hard to tell what's happening without more detail.  Does "Output Nothing" mean no prompt and it just stops responding?  Could be something to do with the ssh client doing something unexpected on resize.  Which ssh client is it?


----------



## petervanderdoes (Nov 19, 2011)

With nothing I mean no output is produced. I'm still connected to the machine.

The whole thing would be:

```
$ps -u|grep bash
$
```


----------



## wblock@ (Nov 19, 2011)

Still could be almost anything.  How is the window resized?  Does it work as expected if you don't resize?


----------



## petervanderdoes (Nov 19, 2011)

If I resize the window back so bash shows up 
`ps -u | grep bash` works as expected,

If I have this in my window:

```
USER    PID  %CPU %MEM    VSZ    RSS  TT  STAT STARTED    TIME COMM
AND
pdoes  1537   0.0  0.1  17572   3876   1  Is    1:37PM 0:00.15 -ba
pdoes 49162   0.0  0.1  17572   3920   2  Ss    1:48PM 0:00.45 -ba
pdoes 49702   0.0  0.0  14328   1480   2  R+    3:15PM 0:00.01 ps
```


```
ps -u|grep ba
```

Output:

```
pdoes  1537   0.0  0.1  17572   3876   1  Is    1:37PM 0:00.15 -ba
pdoes 49162   0.0  0.1  17572   3920   2  Ss    1:48PM 0:00.45 -ba
```

If I don't resize the window grepping on something that falls outside the window fails as well.

One other thing it could be is a bug in the ps command, If the ps command doesn't give the full information to grep, grep won't "see" what you are grepping for.


----------



## darcsis (Nov 19, 2011)

I can reproduce this in gnome-terminal. If I resize the window so that the output of *ps -ax* contain incomplete lines, the invisible parts are really INVISIBLE. grep doesn't see those.


----------



## wblock@ (Nov 19, 2011)

It's the -u option to ps(1).  It only displays processes that it can fit within the width of the current window.


----------



## funky (Nov 19, 2011)

Also note the -w option to ps. When specified twice ps will print the output regardless of the window size (again see ps(1)).

In general, for debugging pipes tee(1) might help:
[cmd=]ps -u | tee ps_u_out.txt | grep bash[/cmd]


----------



## petervanderdoes (Nov 19, 2011)

wblock@ said:
			
		

> It's the -u option to ps(1).  It only displays processes that it can fit within the width of the current window.





			
				funky said:
			
		

> Also note the -w option to ps. When specified twice ps will print the output regardless of the window size (again see ps(1)).
> 
> In general, for debugging pipes tee(1) might help:
> [cmd=]ps -u | tee ps_u_out.txt | grep bash[/cmd]



I did the tee, thanks for the tip, on three distributions: FreeBSD, CentOS and Ubuntu.
[cmd=]ps -u | tee debug.txt[/cmd]

In FreeBSD the contents of debug.txt is truncated according to the window size, but in Ubuntu and CentOS it's not truncated. On screen it's truncated in all three distributions.

Does this mean it's not a bug and it is expected behavior in FreeBSD?


----------



## mix_room (Nov 21, 2011)

wblock@ said:
			
		

> It's the -u option to ps(1).  It only displays processes that it can fit within the width of the current window.



Where is this stated? man ps gives the following reply to me:



> -r      Sort by current CPU usage, instead of the combination of control-
> ling terminal and process ID.
> 
> -u      Display information associated with the following keywords: user,
> ...



I can not reproduce the error.


----------



## funky (Nov 21, 2011)

Again ps(1):

```
[...]
ENVIRONMENT
     The following environment variables affect the execution of ps:

     COLUMNS  If set, specifies the user's preferred output width in column
	      positions.  By default, ps attempts to automatically determine
	      the terminal width.
[...]
```
Check your current setting via the command *echo $COLUMNS* or via *tput cols*. You can get the current window size (independent of $COLUMNS) via the command *stty size*. The only strange behavior of *ps* I get, is when I set the $COLUMNS variable smaller than 64, then *ps* seems to simply ignore $COLUMNS and uses the column value stty size provides.

So, to make it simple, use *ps* together with -ww to make sure you get all the output you need.


----------

