从Java调用的Perl脚本仅将部分字符串作为开关

时间:2016-06-27 07:23:31

标签: java perl

String myCommand = String.format("perl script/path -id 1304444077 -description %250s -owner %s", comment, owner);
process = Runtime.getRuntime().exec(myCommand);

调用脚本,但只接受描述开关的第一个字符串,其余的描述都将丢失。

当我在Unix中运行它时工作正常,我得到了我正在寻找的整个描述。

1 个答案:

答案 0 :(得分:0)

这是一个奇怪的格式字符串。我认为没有理由在命令行中将所有注释字符串保留为250个字符,如果comment包含空格,那么在将结果传递给perl之前,shell会将其拆分为单词

答案似乎是将格式说明符括在引号中,并转义注释字符串中的所有引号,以便它们不会终止参数

我还将多余的字符宽度更改为精度,如果comment的值恰好超过250个字符,则会限制插入的文本数量< / p>

String myCommand = String.format("perl script/path -id 1304444077 -description \"%.250s\" -owner \"%s\"",
        comment.replaceAll("\"", "\\\""),
        owner.replaceAll("\"", "\\\""));

process = Runtime.getRuntime().exec(myCommand);