LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-22-2003, 04:53 AM   #1
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
passing java parameters to a unix script


Hi,

I would like to know if it's possible to pass parameters from a java application to a unix script.

I have a unix script that launches the application. If fatal errors occur in the application, they are caught and the application exits with System.exit(-1).

I would like the unix script to know that the application has stopped and more importantly, if it has stopped in a correct way of if something has gone wrong.

Any help would be greatly appreciated!

greetz
 
Old 08-22-2003, 05:57 AM   #2
Andy@DP
Member
 
Registered: Aug 2003
Location: Aberdeen, UK.
Distribution: Debian, Ubuntu
Posts: 208
Blog Entries: 2

Rep: Reputation: 30
As I understand it the whole java system.exit(x) method is designed for just that purpose. The quitting with a number lets a calling script know what has happened after it has called a program/other script.

I know no unix scripting but pseudo-code would look something like this.
(result is a variable to hold result of call to app)

result = call to application.

if result equals 0
application executed OK so do something
else if result equals -1
application failed so do something else

I think thats how to catch the system.exit() number. A unix scripter may give a proper code solution but thats something to work with.
 
Old 08-22-2003, 06:18 AM   #3
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
I know the general structure, but I need a specific example for unix.

btw, the system.exit returns 0 if everything went ok, any other integer means something went wrong.
 
Old 08-22-2003, 06:33 AM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
one way you can do it like this, basically use "if !" or if depending on what way your checking

#!/bin/bash


if ! java -cp /home/myhome test /home/myhome test; then
echo "Error Occured";
fi


same as suggested above really
 
Old 08-22-2003, 06:46 AM   #5
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
Tried it and it works now. Thank you very much!
 
Old 08-25-2003, 03:26 AM   #6
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
Okay, in addition to the post above:

if ! java -cp /home/myhome test /home/myhome test;
then echo "Error Occured";
fi

works fine.

However, is there a possibility to test for the actual return value from the java program?

example: the java program can do a System.exit(-1) at one line and a System.exit(-2) at another location. Can I test this in the script?

I already tried

if java -cp /home/myhome test /home/myhome test = -1;
then echo "Error -1 Occured";
fi

but that didn't work.

Any suggestions?
 
Old 08-25-2003, 04:08 AM   #7
Andy@DP
Member
 
Registered: Aug 2003
Location: Aberdeen, UK.
Distribution: Debian, Ubuntu
Posts: 208
Blog Entries: 2

Rep: Reputation: 30
Just did a little search and came up with the following. It may not be 100% correct but hopefully it will help you.

if ["`java -cp /home/myhome test /home/myhome test `" = "-1"]; then
echo "Error -1 happened";
else
if ["`java -cp /home/myhome test /home/myhome test `" = "-2"]; then
echo "Error -2 happened";
else
echo "No problems there!";
fi

Note the ` (back quote - key left of 1). This means run the command and substitute the output (hopefully the system.exit output). Hope this helps, actually I hope it works!

Let me know how you get on!
 
Old 08-25-2003, 04:39 AM   #8
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
Thanks for the suggestion Andy@DP, but it didn't work.

I also tried

if [ `java -cp /home/myhome test /home/myhome test ` -eq "-1" ];
then echo "Error -1 happened";
fi

but I get 'unknown test operator' as error message.
 
Old 08-25-2003, 04:46 AM   #9
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 30
There is another possibility.

You can check the return value of the program just completed in the shell script by checking the shell variable '$?'. For example , your program return any value between -1 to -9 depending upon the errors, you can check it IMMEDIATELY by checking the variable $?. It holds the value 0 if no value is returned and the program has executed successfully, and other values if the program returns it.

To output the value, just use '$ echo $?'
 
Old 08-25-2003, 04:55 AM   #10
Andy@DP
Member
 
Registered: Aug 2003
Location: Aberdeen, UK.
Distribution: Debian, Ubuntu
Posts: 208
Blog Entries: 2

Rep: Reputation: 30
OK, I think I may have an answer this time.

run the script (dunno how to do this exactly, hopefully you do)
java -cp /home/myhome test /home/myhome test;

use the special variable $? (returns the exit variable from the last command) and test that for the result.

Hopefully this helps!

EDIT: just realised that the above post must have been entered as I was typing. Stealing my glory

Last edited by Andy@DP; 08-25-2003 at 04:57 AM.
 
Old 08-25-2003, 04:57 AM   #11
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
I just tried echo $?, and the value it shows is 255.

Of course, this is not the value returned by the program, the program returns either -1 or -2 when it runs into problems.
 
Old 08-25-2003, 04:58 AM   #12
Andy@DP
Member
 
Registered: Aug 2003
Location: Aberdeen, UK.
Distribution: Debian, Ubuntu
Posts: 208
Blog Entries: 2

Rep: Reputation: 30
Was that with the script working or not, which value should it have returned?

Just noticed you said you typed echo $?, did you type that or $ echo $?

Last edited by Andy@DP; 08-25-2003 at 05:02 AM.
 
Old 08-25-2003, 05:05 AM   #13
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 30
echo $? returns the value of the last completed program. So, you have to check for it immediately after you complete the java program. So, if you check it after your script is executed, you have the return value of your script.

Try executing your program, i.e, '$ java progname' and now type '$ echo $?' and you will get the return value. You can also try it with other commands such as ls.

Type '$ ls djfnf' and you will get -1
Type '$ jksnfkn' and you will get 255 (I don't remember).

So, try it and post the reply.

PS.. nephilim, I am happy to see that we both think alike, and also I have beat you in this!!!
 
Old 08-25-2003, 05:07 AM   #14
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 30
The '$' in '$ echo $?' is just your promt.
 
Old 08-25-2003, 05:09 AM   #15
Andy@DP
Member
 
Registered: Aug 2003
Location: Aberdeen, UK.
Distribution: Debian, Ubuntu
Posts: 208
Blog Entries: 2

Rep: Reputation: 30
OK I know that now, just a temp moment of insanity!!
I just ran $ echo $? (obviously wrong!) and then echo $?, it returned 127, should have been -1.

So I'm thinking 127 = -1 and 255 = -2, possibly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing parameters to network modules muru Linux - Networking 4 10-21-2005 10:44 AM
Passing parameters to a shell script neocookie Linux - General 5 10-20-2005 11:44 AM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 02:40 PM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM


All times are GMT -5. The time now is 01:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration