12

Unix gurus!

I have a Java program which passes some parameters to a Servlet. The Servlet enters the info into a DB and returns back the ID of a row created back to the java program that calls it. The Java program is run in a Unix shell script, which later goes on to call another java program Java Program_2 (say).

My issue is this - I need to pass the ID we get from Java Program as a parameter to Java Program_2 in that same shell script. ARe there any best practice for this?

Things i am working with so far -

1) Make the java program return an exit code with System.exit(). 2 questions with this - How do i catch the exit code in a variable in the shell? Is this the right way to do it? Exit code is actually meant for returning the success parameter of the program...

2) Write the output in a file java Java_Program >opt.txt. If I do this, then how do I read the contents of opt.txt in a shell variable again?

Thanks a lot!

Edit: I should have mentioned this before actually... the programs are in different machines. I ssh into the other machine using the script..and then run java program 2. Hence, I cannot pipe the two.

1
  • 1
    Just check $?. If it is 0, the program is considered to have exited without error, while if it is non-zero, it is considered to have a failure exit. Commented May 20, 2011 at 15:43

5 Answers 5

13

I would not recommend using the exit status to carry data, for the reasons you've stated. Catching the exit status depends on what shell you're using, but in Bash, the special $? variable contains the exit status of the last process executed.

Writing data to stdout is far more idiomatic. In Bash, you capture it as follows:

output=$(java Java_Program)

or:

output=`java Java_Program`

(You will often hear arguments that the first syntax is to be preferred.)

You can then feed this to stdin of your next process with:

echo $output > java Java_Program_2

More simply, you can simply pipe your processes together:

java Java_Program | java Java_Program_2
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, this works perfectly. However I should have added another thing...the two programs are in different machines. I ssh into the other and then run the second java program (in the same shell script). Now the issue is.. I cannot use the variable on one machine on the other.
@Jai: Fair enough. By the way, did you just downvote this? If you just updated your question to include a crucial detail, it doesn't really make sense to then go and downvote all the people who gave you correct answers to the original question!
No no no... I did not. I had actually upvoted you before. Even i found it weird that this answer got downvoted
@Jai: Ok then. Someone downvoted at the same time as you left your comment. Oh well!
5

I'm not sure if I missed something but it sounds to me that you could just let the first program write to stdout and pipe both programs together, couldn't you? You wouldn't even need a shellscript.

3 Comments

I should have mentioned this before actually... the programs are in different machines. I ssh into the other machine using the script..and then run java program 2
@Jai no problem - just use ssh other-machine-name java Program1 | java Program2
@Robin Green - Works! Thanks a lot!
2

In your Java Program print out the id using System.out.println(id);

In your shell script you can execute the Java Program and store the id in a variable. For example:

ID=$(java JavaProgram)

Now, execute Java Program_2 with the id:

java JavaProgram2 $ID

In Java Program_2, the ID will come into your main method in args[0].

You can even do this in a single step:

java JavaProgram2 $(java JavaProgram)

By the way, if you have output in a file called opt.txt you can read its contents into a variable like this:

CONTENTS=$(cat opt.txt)

1 Comment

Hi, this works perfectly. However I should have added another thing...the two programs are in different machines. I ssh into the other and then run the second java program (in the same shell script). Now the issue is.. I cannot use the variable on one machine on the other. I will still go with this as the answer to my question.
0

I wouldn't go with option 1 as the range of exit codes you can use is very limited, some of them have special meanings, and it's not portable.

For option 2, simply use variable="$(command)" or

variable="`command`"

(I used double quotes in case there are any spaces in the output of command, but I guess that's dumb because your parameter must be a single number!)

In your case you could either use cat opt.txt as the command, or you could cut out the middleman and directly call the first Java program in your command (then you wouldn't need an opt.txt file at all). You can even cut out the variable and do it all in one line.

2 Comments

export is generally unnecessary, unless you intend variable to become an environment variable visible to child processes.
The quotes are senseless in the assignment; the parenthesis or the deprecated backticks capture the whole output. var=$(echo "foo bar")* is sufficient - just for the output it makes a difference, whether you use echo $var or echo "$var", but you don't save them later, if you use them in the assignment. *)There are two spaces between foo and bar.
0

So you capture the output of the ssh-command:

java Prg2 $(ssh host java Prg1))

Using the exit code is not a good idea, because exit-codes, different from 0, normally indicate error.

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.