• 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

help with a shell script

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I'm beginning to learn to write simple shell scripts. It's not as bad a learning experience as I thought.

I am a bit stuck with one of my scripts right now, and I am hoping that someone will suggest the right thing.
This is a fairly newbie question.

I have a java program that generates a file.
This file has to be uploaded to a server via ftp.

I have written two shell scripts, one to run the java program and the other to upload the file. I am calling them in crontab so that they are executed everyday at a specific time with a difference of half an hour. Although this is working, this is not very good as the file upload script is being called after half an hour. If for some reason my java program takes more than half an hour for execution the file upload scipt will upload incomplete file, or if my program finishes in 5 minutes file upload will wait unnecessarily for 25 minutes.

What I would like to do is call this file upload script as soon as the java program finishes its execution. So what should I be doing, catch the exit code of the java program and call the file upload script?

I would appreciate any suggestions.

Thanks,
Cnu
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually you can do something as simple as:

java -jar MyProgram && doftp || echo "Uh-oh"

For simplicity I made the ftp inside a "black box" script here.

Actually, I usually use scp these days, as it's both more secure and simpler to run in batch, but an FTP script works, too.

Almost forgot: In the case of a cronjob, stdout and stderr usually get emailed to you. Otherwise the "echo" would get lost.
[ September 28, 2006: Message edited by: Tim Holloway ]
 
Pavan Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Thanks very much for the answer, It did work when I tested this.

However, I hit upon another problem. I've tried several different ways and tried to read about shell script parameters a couple times. But this seems like a tough nut to crack.

Today I generated a couple reports and I had to upload them via ftp to a server. instead of sending each file manually, I thought I would make use of another script to list all the files and send one at a time looping through the list by calling the other script that I have. I found a sample which was easy to understand and pretty neat.


my other script looks like the following

The target directory I am passing in contains whitespaces. I am including that argument in double quotes(I've also tried single quotes). When I echo the variable right before I establish the ftp session, it seems fine. But when it comes to cd, the path is broken up and the program is complaining no such directory found.

I would really appreciate if someone points me in the right direction here.

Thanks for your time.

Regards,
Cnu
[EDIT] problem with angled brackets with arguments fixed
[ September 28, 2006: Message edited by: cnu sri ]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. I don't think FTP works properly with spaces in names at all. If the destination is a Windows machine can you get away by using the DOS form of the name such as LONGNA~1 ?

As an aside, have you found out about the FTP "mput" command yet? Setting up and tearing down an FTP connection is not a quick process, so if all you are doing is transferring a binch of files from one dir to another over FTP, I'd do something more like:



and call it using

send [hostname] [dest dir]

But I'd second the recommendation for using scp instead. It's simpler, more flexible, more secure and faster too!

scp *.xls username@hostname:destdir
[ September 29, 2006: Message edited by: Frank Carver ]
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, I hate to rain on anyone's parade when writing shell scripts, but this exactly the kind of thing that ANT is designed for.

For instance, you could use something like.

 
Pavan Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank,

Thanks very much for the answer.
Specially the mput thing. Unfortunately scp doesn't seem to be an option. when I did man scp, it said no entries.

We are on SunOS 5.8.

When I establish a session either from dos prompt or from the unix box, and then ic I cd to the directory, it's working fine. when I call the same thing from the script, it doesn't. Right now I'm just uploading to a directory under their home directory, so instead of using the full path, I am specifying the directory name. There are no white spaces in that name. So for now, it seems fine.

Thanks for your help.

Lewin, Thanks for your ant task suggestion, When I have taken this up actually the first thought was to either write a java class or update the existing class to upload the files, or to use and ant task, unfortunately I could not convince my team members about that. I also thought this may be an opportunity to learn a little bit of unix as well.

Best Regards,
Cnu.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A minor hint:
Instead of:


you could use


, at least on bash this works.
 
Pavan Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stefan,

Thanks for the tip.
I am using mput command as Frank Suggested.

But, I can enhance my script by using


instead of



Thank you,
Cnu
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing to keep in mind when using mput, from my understanding you are using this process in batch mode(crontab), I would specifiy when you access the ftp command, otherwise the mput will prompt you for a response before each file is transfered.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic