1

I am writting this code for a JSP page that reads from an text input and passes the variables' values to a bash file as parameters, but it seems like the bash file doesn't take variables, it only worked when I passed real values instead of variables. How to pass a variable to this bash file? Here is the code:

<%
   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

 %>

Note that when passing a value to the script it got executed with the code below:

Process p = Runtime.getRuntime().exec("/root/changeip.cmd 10.0.100.18");
informatik01
  • 16,038
  • 10
  • 74
  • 104
spitti84
  • 121
  • 2
  • 4
  • 11
  • 1
    Right now you're sending the literal `"myArgument"`, not the *value* of `myArgument`. Also, you may need to use a `ProcessBuilder` to get it to accept arguments the way you want; I'm not sure. – Dave Newton Jan 11 '13 at 21:13
  • check this http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code – Kent Jan 11 '13 at 21:18

1 Answers1

1

What you want is:

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

P.S.
Actually I don't see where do you assign a value to myArgument after declaring it:
String myArgument = "";


Not really related, but you are doing it kind of PHP style. Read this: https://stackoverflow.com/a/3180202/814702

Community
  • 1
  • 1
informatik01
  • 16,038
  • 10
  • 74
  • 104
  • I should have written something like: myArgument = request.getValue("first"); where first is read from the text input. Do we just seperate arguments with a + when I am passing several argument to my script? – spitti84 Jan 14 '13 at 15:36
  • @spitti84 When using plus (+) with Strings in Java you concatenate them. In your case, myArgument is a [**String**](http://docs.oracle.com/javase/tutorial/java/data/strings.html) object. In your first example you just used "myArgument" as **string literal**, i.e. the result was a word `myArgument`. But you needed the value of the String object. That's why you actually needed `"/root/script.cmd " + myArgument`. As for the arguments to the script, you separate them with spaces. If an argument contains spaces enclose it in quotes. – informatik01 Jan 14 '13 at 23:52