简体   繁体   English

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

[英]Perl script called from Java only takes partial string as switch

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

the script is called but only takes the first string for the description switch and the rest of the description is lost. 脚本被调用,但仅使用描述开关的第一个字符串,其余描述丢失。

When I run it in Unix it works fine and I get the entire description I'm looking for. 当我在Unix上运行它时,它可以很好地工作,并且可以获得所需的完整描述。

That is a strange format string. 那是一个奇怪的格式字符串。 I see no reason to left-pad all comment strings to 250 characters in a command line, and if comment contains whitespace then the shell will split it into words before passing the result to perl 我认为没有理由在命令行中将所有注释字符串左键填充为250个字符,如果comment包含空格,那么在将结果传递给perl之前,shell会将其拆分为单词

The answer appears to be to enclose the format specifier in quotes, and to escape all quotes in the comment string so that they don't terminate the parameter 答案似乎是将格式说明符括在引号中,并转义注释字符串中的所有引号,以便它们不终止参数。

I've also changed the superfluous character width to a precision , which will limit the amount of text inserted if the value of comment happens to be longer than 250 characters 我还将多余的字符宽度更改为precision ,如果comment的值恰好超过250个字符,这将限制插入的文本量

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM