开发者

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: 开发者_Go百科

Process exitValue: java.io.BufferedOutputStream@3e25a5

Any help? Thanks in advance


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.


This article will help you.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜