views:

57

answers:

3

Hello
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: java.io.BufferedOutputStream@3e25a5

Any help? Thanks in advance

+2  A: 

This article will help you.

kgiannakakis
I checked it before.Thanks anyway
mujer esponja
It's a very interesting article. Thanks for the link.
Tomas Narros
A: 

Basically you can't do that.

The shell environment variable is a local variable to that shell the process that spawned it cannot see the data in the shell except for the return code which is an integer.

You can write the shell variable to stdout and then Java can read that.

Also your code does print out the correct thing the address of the stream - to get the values in the stream you need to open it and read from the stream

In your example I would use Java's regex classes.

Mark
+2  A: 

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.

Nithesh Chandra
b.readLine() is null, so maybe must I change something from the script?
mujer esponja
Edited the answer.
Nithesh Chandra
change the last lines: echo $filename | sed 's/\([A-Z]\)/ \1/g'; and that's it!! Thanks!!
mujer esponja