我正在为工作编写一个程序。
它连接到我们的 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 会返回结果,上面的方法就会起作用。
我基本上需要能够 grep/PGProcid= 来获取 PID 和 PPID 编号。这似乎仅在存在 1 & 10,11,12 等或 2 & 20,21,22 等的情况下不起作用。
我尝试过 Egrep,并使用 $'s,但它似乎总是跳过个位数!
编辑: 这是我在此服务器上尝试过的内容
# 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 目前在他们的服务器上处于 Activity 状态,但是下面没有给出结果
# ps -ef | grep /PGProcid=2$
# ps -ef | grep /PGProcid=2\$
# ps -ef | grep "/PGProcid=2$"
下面给出了结果,但也选取了其中包含 2 的任何内容,例如 22 等,其中我仅在 2 之后
# ps -ef | grep '/PGProcid=2$'
下面给出错误“没有这样的文件或目录”
# ps -ef | grep `/PGProcid=2$`
最佳答案
您的 shell 将尝试使用环境变量扩展 $
。您必须使用 \
:
$
grep /PGProcid=1\$
或“”
:
grep "/PGProcid=1$"
编辑:
更准确地说,您应该使用 \>
来匹配单词末尾的空字符串。由于 \
和 >
均由 shell 解释,因此您也应该保护它们:
grep /PGProcid=1\\\>
或
grep "/PGProcid=1\>"
如果您想要“单词匹配”(在我看来),您还可以尝试 -w
选项:
grep -w /PGProcid=1
关于java - Sco Unix Grep 特定号码(ID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22409389/