0

I have a programme I am writing for work.

it connects onto our Sco Unix server and runs a command, which most of the time works fine

String command = "ps -eo ruser,pid,ppid,stime,etime,tty,args | sort -k4 | grep /PGProcid="+ProcID+"\\ | grep -v grep"; 

Which when output can look like the following for example

ps -eo ruser,pid,ppid,stime,etime,tty,args | sort -k4 | grep /PGProcid=1\ | grep -v grep

However if I try and do this for a single number (normally 1 but not restricted to it) I get no results, even though I know the results exist.

for example if I have the following results on the server

# ps -ef | grep /PGProcid=1
 name 29175 29174  0 02:55:57  ttyp15    00:00:00 /xxx/xxx/xxx/prog6 /PGProcid=14
 person2 28201 28199  0 01:15:27  ttyp13    00:00:00 /xxx/xxx/xxx/prog1 /PGProcid=1

Then if I do the following

# ps -ef | grep /PGProcid=1\

I get no results but I know there is results for 1, the above will work if i use double digits like 14 will bring back the results.

I basically need to be able to grep for the /PGProcid= to get the PID and PPID numbers. This only seems to not work where there's 1 & 10,11,12 etc or 2 & 20,21,22 so on.

I have tried Egrep, and using $'s but it always seem to skip the single digit numbers!

EDIT: Here's what i have tried on this server

  # echo $SHELL
  /bin/sh
  ps -ef | grep PGProcid=2
  amanda 23602 25207  0 09:22:58       ?    00:00:06 /xxxxxx /PGProcid=2
  amanda 25207 25203  0   Feb-28       ?    00:00:01 /xxxxxx /PGProcid=2
  root 26389 26034  0 05:15:22   ttyp6    00:00:00 grep PGProcid=2
  amanda 26042 23602  0 04:46:16       ?    00:00:04 /xxxxxx /PGProcid=2

so 2 is active currently on their server however the below give no results

  # ps -ef | grep /PGProcid=2$
  # ps -ef | grep /PGProcid=2\$
  # ps -ef | grep "/PGProcid=2$"

The below gives results but also picks up anything with a 2 in it so 22 etc where im only after 2

   # ps -ef | grep '/PGProcid=2$'

Below gives an error "No such file or directory"

   # ps -ef | grep `/PGProcid=2$`
Matthieu
  • 2,736
  • 4
  • 57
  • 87

1 Answers1

1

Your shell will try to expand $ with an environment variable. You have to protect it $ with either a \:

grep /PGProcid=1\$

or "":

grep "/PGProcid=1$"

Edit: To be more precise, you should use the \> to match the empty string at the end of a word. And as both \ and > are interpreted by the shell, you should protect them as well:

grep /PGProcid=1\\\>

or

grep "/PGProcid=1\>"

If you want to have a "word match" (which it seems to me), you can also try the -w option:

grep -w /PGProcid=1
Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • That hasn't gone as well as planned. That seemed to work on that server, however another server is now giving me no results same grep line single digit that does exist but the line doesn't pick it up – Christopher Lewis Mar 14 '14 at 16:03
  • @ChristopherLewis is the other server using the same shell (i.e both using bash, ksh, ...)? You might want to try single quotes `'` instead of double `"` as single quotes won't expand anything. – Matthieu Mar 14 '14 at 16:12
  • They appear to be running different shells, one is using /usr/bin/bash the other is using /bin/sh I guess my options are editing the profiles on the servers so all shells match, or trying to find some common ground? – Christopher Lewis Mar 14 '14 at 16:18
  • @ChristopherLewis using the same shell would be easier for you as you could use the same scripts everywhere. However, if not possible, try using simple quotes `'`. – Matthieu Mar 14 '14 at 16:20
  • Sorry removed that reply from my comment I did try single quotes but it didn't like those either found no results, if I can find a way of getting it to work on the /usr/bin/bash I could add in a check to see what shell that server ran and execute a different command – Christopher Lewis Mar 14 '14 at 16:24
  • I managed to change the shell to SH, however it still doesnt find the ID in the original format so now im more confused ! – Christopher Lewis Mar 14 '14 at 16:44
  • @ChristopherLewis it should work with bash (my tests were done with bash). Check the `man sh` and `man bash` for differences about character protection. – Matthieu Mar 14 '14 at 16:55
  • I tried all options, ive updated my original post to show what ive tried, looking at the escape items \ should be enough however it doesnt seem to roll that way – Christopher Lewis Mar 14 '14 at 17:22
  • @ChristopherLewis edited my answer for you to try the `\>` match or `-w` option of grep. It looks fine on my bash. Tell me if it works. If it does not, try asking the question on Unix Stackexchange. – Matthieu Mar 14 '14 at 18:19
  • Hi Matthieu thanks again for the help. -w doesn't work on my shell, illegal option. I do use -w on the Mac servers we have. The sco ones don't like this. the "/PGProcid=1\>" also brings back no results. However i did modify the grep /PGProcid=1\\\> and removed the > from the end giving me grep /PGProcid=1\\\ this seems to be working at present, but im guessing if I find another server that doesn't like it im back to square one! Thanks for the help anyway and ill try the command and see if anything causes any more issues and if it does ill try putting it up on the unix one! Thanks again – Christopher Lewis Mar 17 '14 at 08:53