我有一个正在写的工作程序。

它连接到我们的Sco Unix服务器上并运行命令,大多数情况下该命令运行良好

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


例如,何时输出如下所示

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


但是,如果我尝试对单个数字执行此操作(通常为1,但不限于此),即使我知道结果存在,也不会获得任何结果。

例如,如果我在服务器上有以下结果

# 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


然后,如果我执行以下操作

# ps -ef | grep /PGProcid=1\


我没有得到任何结果,但是我知道有1的结果,如果我使用14这样的两位数将返回结果,则上述方法将起作用。

我基本上需要能够对/ PGProcid =进行grep以获得PID和PPID编号。这似乎仅在有1&10,11,12等或2&20,21,22等的情况下不起作用。

我尝试了Egrep,并使用$来代替,但它似乎总是跳过单位数字!

编辑:
这是我在此服务器上尝试过的

  # 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


因此2当前在其服务器上处于活动状态,但是以下未给出结果

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


下面给出了结果,但也选择了带有2的任何内容,因此22以此类推,其中im仅在2之后

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


下面给出了错误“没有这样的文件或目录”

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

最佳答案

您的shell将尝试使用环境变量来扩展$。您必须用$保护它\

grep /PGProcid=1\$


""

grep "/PGProcid=1$"


编辑:
更准确地说,应使用\>匹配单词末尾的空字符串。而且由于\>都由外壳程序解释,因此您也应该保护它们:

grep /PGProcid=1\\\>


要么

grep "/PGProcid=1\>"


如果您想使用“单词匹配”(在我看来),也可以尝试使用-w选项:

grep -w /PGProcid=1

10-02 05:13
查看更多