【问题标题】:Passing values as bash file arguments through a JSP page通过 JSP 页面将值作为 bash 文件参数传递
【发布时间】:2013-01-11 21:07:06
【问题描述】:

我正在为从文本输入读取并将变量值作为参数传递给 bash 文件的 JSP 页面编写此代码,但似乎 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");

【问题讨论】:

  • 现在您发送的是文字 "myArgument",而不是 myArgument。此外,您可能需要使用 ProcessBuilder 让它以您想要的方式接受参数;我不确定。

标签: bash jsp cmd argument-passing


【解决方案1】:

你想要的是:

Process p = Runtime.getRuntime().exec("/root/script.cmd " + myArgument );

附言
实际上,在声明 myArgument 之后,我看不到您在哪里为其赋值:
String myArgument = "";


真正相关,但你这样做是一种 PHP 风格。阅读:https://stackoverflow.com/a/3180202/814702

【讨论】:

  • 我应该这样写:myArgument = request.getValue("first");其中 first 是从文本输入中读取的。当我将多个参数传递给我的脚本时,我们是否只是用 + 分隔参数?
  • @spitti84 在 Java 中将加号 (+) 与字符串一起使用时,您可以将它们连接起来。在您的情况下, myArgument 是一个 String 对象。在您的第一个示例中,您只是将“myArgument”用作字符串文字,即结果是一个单词myArgument。但是您需要 String 对象的值。这就是你真正需要"/root/script.cmd " + myArgument 的原因。至于脚本的参数,你用空格分隔它们。如果参数包含空格,请将其括在引号中。
猜你喜欢
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多