[英]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);
脚本被调用,但仅使用描述开关的第一个字符串,其余描述丢失。
当我在Unix上运行它时,它可以很好地工作,并且可以获得所需的完整描述。
那是一个奇怪的格式字符串。 我认为没有理由在命令行中将所有注释字符串左键填充为250个字符,如果comment
包含空格,那么在将结果传递给perl
之前,shell会将其拆分为单词
答案似乎是将格式说明符括在引号中,并转义注释字符串中的所有引号,以便它们不终止参数。
我还将多余的字符宽度更改为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.