Return and save String variable from shell script to Java

I want to save the value in a variable in this script:

#!/bin/sh
filename=$1

var= $filename | sed 's/([A-Z])/ 1/g';

return var;

(Firstable, I am not sure if it is correct), and after thar, get this value and use it in my Java program, like this:

Process s = Runtime.getRuntime().exec("./CreateTitle.sh "+filename);
OutputStream exitVal = s.getOutputStream();
System.out.println("Process exitValue: " + exitVal.toString());

I think I am soing something wrong, because in the last sysout, I don’t get the wished result, but this one:

Process exitValue: [email protected]

Any help? Thanks in advance

Answer

Its not OutputStream that you need. Here’s the code you need

BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream()));
String output = "", line = "";
while((line = br.readLine()) != null) {
    output += line;
}
System.out.println(output);

You should look at Mark’s answer too. He covers the problem with shell script.

EDIT: Here’s the changed script

#!/bin/sh
filename=$1

var = $filename | sed 's/([A-Z])/ 1/g';

echo var;

I’m not very comfortable with shell scripting. Someone correct me if there’s anything wrong here.

Leave a Reply

Your email address will not be published. Required fields are marked *