3

I am trying to run a bash script on my Ubuntu machine through Java. The bash script takes 2 input as parameters that i am passing as an array However, it does not seem to be passing the value of array[0] and array[1] to the bash script?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.omg.CORBA.portable.InputStream;

public class readBashScript {

    public static void readBashScript() {
        try {

            String[] array = {"ys1","R"};

            Process proc = Runtime.getRuntime().exec("var/www/red/marsh_webreset.sh /",array); 
            BufferedReader read = new BufferedReader(new InputStreamReader(
                    proc.getInputStream()));
            try {
                proc.waitFor();
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
            while (read.ready()) {
                System.out.println(read.readLine());
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
4
  • I've come up against similiar issues using Runtime.exec . I think my solution was to include the arguments directly in the program to execute String Commented Sep 29, 2014 at 18:58
  • The recommended approach is using ProcessBuilder, although with shellshock this seems a very risky piece of code. Commented Sep 29, 2014 at 18:59
  • when you say directly in the program do you mean in the bash script ? because i will be taking the arguments from the users real time Commented Sep 29, 2014 at 19:00
  • No, I mean after "var/www/red/marsh_webreset.sh /" Commented Sep 29, 2014 at 19:03

4 Answers 4

2

Take a look at some documentation.

The second argument you are passing to the exec method is:

envp -- array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.

I recommend taking a look at this and this.

If you want to pass environment variables, you can add them as an array but has to be in format "key=value".

IE:

$ ONE=1 TWO=2 shell.sh

You can then echo these variables in your shell script.

$ echo $ONE

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

2 Comments

This is a valid critique, but it doesn't solve to problem, or?
Edited to add practical usage.
1

You are passing the arguments incorrectly Try below code:

Process proc = Runtime.getRuntime().exec("/var/www/redbutton/marsh_webreset.sh "+array[0]+" "+ array[1]+" /");

Comments

0

It looks like you are calling the wrong Runtime.exec() method. You're passing in a command and an array of environment variables, but you want to pass in arguments to the process you're executing. Instead of exec(String, String[]) you want to call exec(String[]).

You likely want to look at the error stream as well - it probably has an informative error message. Additionally, I'm not sure that / at the end of the command string is helpful, or even valid. You probably also don't want to import org.omg.CORBA.portable.InputStream.

Comments

0

You should send each value of the array at a time. You can't send the array as it is as an argument to the bash script since it can't extract the values by itself.

Process proc = Runtime.getRuntime().exec("/var/www/redbutton/marsh_webreset.sh "+array[0]+" "+ array[1]+" /");

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.