• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running Shell Script from Java Program

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic