1

I have a variable parameter ( retrieved from a getText field in Seleium Automation) that i want to assign the value into a specefic place in a shell script :

In java this is what i do :

String ref = workcreation.getfield_ref().getText(); 


  try {  ProcessBuilder pb = new ProcessBuilder("/home/script.sh");
            Process p = pb.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                System.out.println(line);

                System.out.println("in " + reader);

            }
            p.waitFor();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

In script script.sh, i want to assign the value of ref into the parameter &ref in that exact place :

##it is the value of &ref that i want to get 
if [  -d "/data/techVersion_$ref" ]; then
echo "le dossier  existe dans cccc "

else
echo " le dossier  n'existe pas dans  cccc !"
exit
fi

what can i do?

2 Answers 2

3

You can pass arguments to your command/script by ProcessBuilder.

In your shell script, you can read the argument. A simple example is:

ProcessBuilder pb = new ProcessBuilder("/home/script.sh", "hello");

in your script:

echo "variable set in java: $1"
Sign up to request clarification or add additional context in comments.

Comments

1

The ProcessBuilder takes an arbitrary number of Strings as arguments. The first one has to be the executable itself, in your case it is the script "/home/script.sh". You are currently just passing the executable as a single argument. Just add the parameters for your script to the constructor call of ProcessBuilder.

Your line

ProcessBuilder pb = new ProcessBuilder("/home/script.sh");

should be replaced by this

ProcessBuilder pb = new ProcessBuilder("/home/script.sh", "firstArg", "secondArg");

or you create a List<String> containing the executable as first element followed by the parameters, like

List<String> execPlusArgs = new ArrayList<String>();
execPlusArgs.add("/home/script.sh");
execPlusArgs.add("firstArg");
execPlusArgs.add("secondArg");
ProcessBuilder pb = new ProcessBuilder(execPlusArgs);

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.