Devang Kamdar
Devang Kamdar

Reputation: 5927

Is it possible to capture a system.out.print from java program in a csh variable?

I know this is lame way to do it. We need to capture a string that is dynamically generated in a Java program in the calling shell script and then use it later.

Also this is to be done in an existing csh file.

We considered the option of exporting it to an environment variable using putenv/setenv ... is that a better option? If so how to use it?

There seems to be lack of example code on net on how to effectively use sentenv() or putenv() and what libraries to import for the same.

System class that has the getenv() method does not show my putenv() or setenv(). Any help is highly appreciated.

Thanks.

Upvotes: 1

Views: 2445

Answers (2)

unwind
unwind

Reputation: 400029

You should be able to just use command substitution, using backtick syntax just as in bash (in bash nowadays, the more usable $-syntax is recommended). Like so:

$ set A=`java MyProgram`
$ echo $A

or something very similar

You cannot use the environment; it's not possible to communicate "backwards" using it; a child process cannot change the parent's environment.

UPDATED: Added the 'set' keyword in the example, d'oh.

Upvotes: 2

SS.
SS.

Reputation: 109

Extending from the first answer:

You probably want to do this: $ A=java MyProgram | grep <something> $ echo $A

where is what you like to capture, assuming the MyProgram output more than one line.

If the java program uses System.err.print...instead of System.out.print.... you need to enhanced to $ A=java MyProgram 2>&1 | grep <something>

hope it helps :-)

Upvotes: 0

Related Questions