1

I have an Stand alone Application which runs Shell Script(with parameters)in Ubuntu.

ProcessBuilder pb1 = new ProcessBuilder("sh","deltapackage_app.sh","part_value","pathtofile"); 
Process process1 = pb1.start();

I am taking parameter through GUI. Now same thing i want to implement in web application where i can take inputs form web page and send it to server and then server will execute the shell script with parameters.

Can any one suggest me the best way of doing this. What things should i use to do this.

I Know i have to learn many things about server. Or can i use same code with Browser based Application.

1
  • You should take care about security, if You allow users to provide parameters to shell scripts. Plain/unquoted/unescaped shell parameters allow process execution on the command line, e.g. downloading executables from the internet... Commented Jun 28, 2012 at 8:24

1 Answer 1

1

Consider the following line of code:

Process p = Runtime.getRuntime().exec("/bin/sh -c /bin/ls > ls.out");

This is intended to execute a Bourne shell and have the shell execute the ls command, redirecting the output of ls to the file ls.out. The reason for using /bin/sh is to get around the problem of having stdout redirected by the Java internals. Unfortunately, if you try this nothing will happen. When this command string is passed to the exec() method it will be broken into an array of Strings with the elements being "/bin/sh", "-c", "/bin/ls", ">", and "ls.out". This will fail, as sh expects only a single argument to the "-c" switch. To make this work try:

    String[] cmd = {"/bin/sh", "-c", "/bin/ls > out.dat"};
    Process p = Runtime.getRuntime().exec(cmd);

Since the command line is already a series of Strings, the strings will simply be loaded into the command array by the exec() method and passed to the new process as is. Thus the shell will see a "-c" and the command "/bin/ls > ls.out" and execute correctly.

http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.