我正在为一个JSP页面编写此代码,该页面从文本输入读取并将变量的值作为参数传递给bash文件,但似乎bash文件不接受变量,它只在我通过时才起作用实际值而不是变量。 如何将变量传递给此bash文件? 这是代码:
<%
String myArgument = "";
if (request.getParameter("submit")==null)
{
%>
<form method="POST" action="/tomcat/webapps/project/jsp/runCMD.jsp" id=form2>
<input type=text id=first value="${first}" Title="IP adress here" >
<input type="submit" value="Submit Changes" name="submit"
onClick="if(runOnSubmit()){getSomethingWithAjax('ChangeIP.jsp'+
getAllFormElementsAndMakeIntoURI(true),'',
'hereIsTheMainHeaderSpan',false,false);}">
<%
}//end if
//else if submitted
else {
request.setAttribute("myArgument", myArgument);
try {
Process p = Runtime.getRuntime().exec("/root/script.cmd myArgument );
p.waitFor();
System.out.println("Waiting for the System to reboot" + p.exitValue());
} catch (Exception e) {
System.out.println(e.getMessage());
out.println("There was an error while submitting ");
}
}//end else
%>
请注意,在将值传递给脚本时,它会使用以下代码执行:
Process p = Runtime.getRuntime().exec("/root/changeip.cmd 10.0.100.18");
答案 0 :(得分:1)
你想要的是:
Process p = Runtime.getRuntime().exec("/root/script.cmd " + myArgument );
<强> P.S。强>
实际上我没有看到在声明之后你在哪里为 myArgument 赋值:
String myArgument = "";
不真的很相关,但你做的是那种PHP风格。阅读:https://stackoverflow.com/a/3180202/814702