我正在java中调用一个shell脚本,它接受2个参数。
private void invokeShellScript(String script,String subject,String message)
{
String shellCmd = null;
try
{
shellCmd = script.trim() + " " + subject + " " + message;
Process process=Runtime.getRuntime().exec(shellCmd);
process.waitFor();
}
catch(Exception e)
{
LOGGER.error("Exception occured while invoking the message report script");
}
}
当我将主题和消息传递给 shell 脚本时,它没有正确解析内容。
这里说如果主题=“你好,这是一封测试邮件”。 然后 shell 脚本认为主题是 Hello,消息是 This。
这里我猜测字符串中的空格导致了问题。
我该如何解决这个问题。
最佳答案
在将字符串传递给 shell 时尝试引用它们。
所以要么
"\"Hello This is a test mail\""
或
"'Hello This is a test mail'"
(在java中)
关于java - java调用shell脚本的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16651290/