Java 通过Runtime.exec()执行管道ps unix命令时出现问题 问题:
当通过Java 通过Runtime.exec()执行管道ps unix命令时出现问题 问题:,java,unix,runtime.exec,java-6,Java,Unix,Runtime.exec,Java 6,当通过Runtime.exec(…)执行以下命令时,它在查找匹配的引号字符时失败,并出现意外的EOF 一个奇怪的是,错误消息后面有一个严重的字符,后跟两个单引号 然而,当我执行通过putty打印在日志中的命令时,它工作得很好 命令: 产生的错误: Java代码(Java 1.6…不要判断): 相关但不完全重复: 将其分解为令牌,这样就可以使用以下参数实际运行程序/bin/sh(shell): -c 'ps -eo uname,pid,ppid,nlwp,pcpu,pmem,psr,st
Runtime.exec(…)
执行以下命令时,它在查找匹配的引号字符时失败,并出现意外的EOF
一个奇怪的是,错误消息后面有一个严重的字符,后跟两个单引号
然而,当我执行通过putty打印在日志中的命令时,它工作得很好
命令:
产生的错误:
Java代码(Java 1.6…不要判断):
相关但不完全重复: 将其分解为令牌,这样就可以使用以下参数实际运行程序
/bin/sh
(shell):
-c
'ps
-eo
uname,pid,ppid,nlwp,pcpu,pmem,psr,start_time,tty,time,args
|
fgrep
...
shell对这些参数的解释如下:
-c 'ps -- the script to run consists of the apostrophe character, p, s (and nothing more)
-eo -- the name of the command being run is -eo
uname,pid,.... -- the first argument to the script is this
| -- the second argument to the script is this
fgrep -- the third argument to the script is this
...
-- but the script ignores the arguments and doesn't use them
这样你就得到了
-eo: -c: unexpected EOF while looking for matching `''
# the script named -eo, with the option -c having value 'ps,
# tried to find a closing ' to match the opening ' and it's not there
这个shell显然是(GNU)bash;许多GNU程序将数据字符串放入错误消息中,并用反引号和撇号将其包围,因为在几十年前流行的一种ASCII解释中,这些都是匹配的引号
取而代之的是,当shell(而不是StringTokenizer
)解析上述命令行时,使用为shell提供它获得的两个参数:
String[] cmdary = {"/bin/sh", "-c", "ps -eo stuff | fgrep this | fgrep that | fgrep -v fgrep"};
... Runtime.getRuntime().exec(cmdary);
但是,您不必运行三个fgrep,只需运行
ps
并将输入流作为行读取,然后使用String.contains
或类似工具在Java中测试它们。此外,您询问的大多数列永远不会用于匹配或结果,因此这只是浪费精力和混乱。相关但不完全重复:
将其分解为令牌,这样就可以使用以下参数实际运行程序/bin/sh
(shell):
-c
'ps
-eo
uname,pid,ppid,nlwp,pcpu,pmem,psr,start_time,tty,time,args
|
fgrep
...
shell对这些参数的解释如下:
-c 'ps -- the script to run consists of the apostrophe character, p, s (and nothing more)
-eo -- the name of the command being run is -eo
uname,pid,.... -- the first argument to the script is this
| -- the second argument to the script is this
fgrep -- the third argument to the script is this
...
-- but the script ignores the arguments and doesn't use them
这样你就得到了
-eo: -c: unexpected EOF while looking for matching `''
# the script named -eo, with the option -c having value 'ps,
# tried to find a closing ' to match the opening ' and it's not there
这个shell显然是(GNU)bash;许多GNU程序将数据字符串放入错误消息中,并用反引号和撇号将其包围,因为在几十年前流行的一种ASCII解释中,这些都是匹配的引号
取而代之的是,当shell(而不是StringTokenizer
)解析上述命令行时,使用为shell提供它获得的两个参数:
String[] cmdary = {"/bin/sh", "-c", "ps -eo stuff | fgrep this | fgrep that | fgrep -v fgrep"};
... Runtime.getRuntime().exec(cmdary);
但是,您不必运行三个fgrep,只需运行
ps
并将输入流作为行读取,然后使用String.contains
或类似工具在Java中测试它们。另外,您询问的大多数列将永远不会用于匹配或结果,因此这只是浪费精力和混乱。后引号、单引号、单引号正是此工具输出要“报价”的项目的方式。不幸的是,许多其他工具也使用这种格式,但我认为在缺少字符为“
(以及其他可能的字符)的情况下,它可以避免歧义。对于您的情况,您可以认为错误消息只是以”“
(dbl引号,单引号,dbl引号)结尾,而“
是有问题的字符。发生这种情况的原因超出了我的薪资等级。祝你好运!后引号、单引号、单引号正是此工具输出它要“引用”的项目的方式“。不幸的是,许多其他工具也使用这种格式,但我认为在缺少字符为”
(以及其他可能的字符)的情况下,它可以避免歧义。对于您的情况,您可以认为错误消息只是以“”
(dbl引号,单引号,dbl引号)结尾),这是一个有问题的字符。。发生这种情况的原因超出了我的工资等级。祝你好运!测试通过字符串数组,下一步。我不想在java中处理ps commind的完整结果,因为有大量的记录,如果我不需要的话,将有大量的进程执行此操作。一旦我们升级我们的Java版本,我将能够使用更好的工具,但目前这是我所拥有的。使用字符串数组修复了它。谢谢。测试通过字符串数组,下一步。我不想在Java中处理ps commind的完整结果,因为有大量记录,如果我不需要的话,将有大量进程执行此操作。一旦我们升级我们的Java版本,我将能够使用更好的工具,但现在这是我所拥有的。使用字符串数组修复了它。谢谢。