This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes help with a shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "help with a shell script " Watch "help with a shell script " New topic
Author

help with a shell script

Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
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


formerly known as Cnu
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15961
    
  19

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 ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
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 ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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 ]

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
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.



I have no java certifications. This makes me a bad programmer. Ignore my post.
Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
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.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

A minor hint:
Instead of:


you could use


, at least on bash this works.


http://home.arcor.de/hirnstrom/bewerbung
Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
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
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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.
 
 
subject: help with a shell script
 
Similar Threads
Updates to Java GUI when Running UNIX Shell Script
Updates to Java GUI when Running UNIX Shell Script
Time complexity for jdbc connection
Updates to Java GUI when Running UNIX Shell Script
porting a .bat file to unix