【问题标题】:Apache commons exec run pipe command failsApache commons exec 运行管道命令失败
【发布时间】:2013-03-14 15:14:57
【问题描述】:

我希望 Apache Commons-exec 运行:

git 状态 |头-n1 |切-c13-

但似乎无法执行命令行并给出错误,有什么想法吗?

CommandLine cmdLine = CommandLine.parse( "git status | head -n1 | cut -c13-" );
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory( file );
executor.execute( cmdLine );

错误:

error: unknown switch `n'
usage: git status [options] [--] <filepattern>...

【问题讨论】:

    标签: git apache-commons-exec


    【解决方案1】:

    CommandLine.parse 不会创建 bash shell 来解释您的管道。

    如“How to pipe a string argument to an executable launched with Apache Commons Exec?”中所述:

    您不能添加管道参数 (|),因为 [这里,在您的情况下] 'git status' 命令不会接受。
    解释的是 shell(例如 bash)当您在 shell 中键入命令行时,管道会进行特殊处理

    您应该使用 ByteArrayInputStream 将一个命令的输出提供给另一个命令;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 2018-05-15
      • 2017-01-22
      • 2013-09-13
      • 2019-04-28
      • 2023-03-11
      相关资源
      最近更新 更多