Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running Shell Script from Java Program

 
SAM KUMAR
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to call a shell script from Java and I am not sure how can I find out whether the script ran sucessfully or not.
Shell Script:ftpscript.sh
ftp -n testServer << EOD
user userName Password
bin
cd /Reports
put rpt1.csv
quit
Java Code Snippet
-----------------
Runtime myRuntime = Runtime.getRuntime();
String cmd = "command ftpscript.sh";
Process ps = myRuntime.exec(cmd);
ps.waitFor();
ps.destroy();
System.out.println("Exit Status: " + ps.exitValue() + " " + ps.toString());
Lets say user password is invalid, in that case ftpscript.sh will unable to ftp the file but from Java point of view I am getting Exit Status as ZERO (Prociess.exitValue()).
Any ideas how to know whether FTP script was sucessful or not.
Thanks.
SK
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes -- rewrite the ftp script to return an error status when it fails!
Otherwise you have to use the javax.sentience package (JSR-10023) which won't be ready for another 20 years or so.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes -- rewrite the ftp script to return an error status when it fails! You might have the script test for the existence of the file by running the ftp "ls" command on the server, and piping the result through "grep". Be careful because grep returns non-zero on success, zero on failure!

Otherwise you have to use the javax.sentience package (JSR-10023) which won't be ready for another 20 years or so.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic