Here is the first Perl script I've tried to write to run rsync. It seems to work, but I don't see the progress like I should. I apologize if it's sloppy, I should add that it's not only the first perl script I've written for rsync, it's the first perl script I've written at all. ___________________________ #!/usr/bin/perl -w print " This command will DELETE EVERYTHING in Remote~/Research UNLESS it is also found in Local~/Research. Do you really wish to do this? [N/yes]: "; $answer = <STDIN>; chomp $answer; if ( $answer eq "yes" ) { print "\nOK, here we go.\n"; `rsync -auvz --progress --delete -e ssh Remote:/home/user/rsync-testing /home/user` } else { print "\nabort mission\n"; } ____________________________ Thanks for the help. -Rob
You are quite right. I fixed that up, here is the current incarnation of the script, but still no output revealed. Also if anyone who actually knows something about perl could tell me how to have it reprint the first print command if a valid choice is not entered that would be pretty slick. I could add an "exit" choice so that it would keep giving you a chance to get it right until you type exit. But that's not really essential. Seeing what's going on is (or at least close to essential). -Rob _______________________ #!/usr/bin/perl -w print " Which computer do you wish to delete files from? [Remote/Local]: "; #The user should now type one of the above choices, running that part of the script $machine = <STDIN>; chomp $machine; if ( $machine eq "Local" ) { print " This command will DELETE EVERYTHING in Local~/Research UNLESS it is also found in Remote~/Research. Do you really wish to do this? [N/yes]: "; $answer1 = <STDIN>; chomp $answer1; if ( $answer1 eq "yes" ) { print "\nOK, here we go.\n"; `rsync -auvz --progress --delete -e ssh Remote:/home/user/rsync-testing /home/user` } else { print "\nI am NOT deleting anything from Local\n"; } } else { print "You have not selected a valid machine. \nPlease rerun cleansync and type in a machine name (case sensitive).\n" } ; if ( $machine eq "Remote" ) { print " This command will DELETE EVERYTHING in Remote~/Research UNLESS it is also found in Local~/Research. Do you really wish to do this? [N/yes]: "; $answer2 = <STDIN>; chomp $answer2; if ( $answer2 eq "yes" ) { print "\nOK, here we go. \n"; `rsync -auvz --progress --delete -e ssh /home/user/rsync-testing Remote:/home/user/` } else { print "I am NOT deleting anything from Remote \n"; }; } _____________________ On 4/22/02 6:25 PM, "Mike Rubel" <mrubel@galcit.caltech.edu> boldly proclaimed:> > Hi Robert, > This is not directly related to your question, but I do notice one issue > here: > >> This command will DELETE EVERYTHING in Remote~/Research >> UNLESS it is also found in Local~/Research. > > ... > >> if ( $answer eq "yes" ) { >> print "\nOK, here we go.\n"; >> `rsync -auvz --progress --delete -e ssh >> Remote:/home/user/rsync-testing /home/user` >> } > > I think you may have the order of the source and destination reversed; the > command above will rsync from Remote:/home/user/rsync-testing to > /home/user, deleting anything in /home/user that is not already in > Remote:/home/user/rsync-testing. But it sounds, from the comment above, > like this is not what you intended to do... > > Mike > >
The filehandle trick is a fine way to do it, but I want to point out that some of the output comes on STDERR, so you might want to add a "2>&1" just before the pipe in your open()..... that is, of course if you want that info... stuff like: opendir(test/license.management/logsnap): Permission denied opendir(test/test/license.management/logsnap): Permission denied link_stat sjtnetsvr:/tmp : No such file or directory rsync error: partial transfer (code 23) at ../../../../src/rsync-2.5.5rc1/main.c(578) Tim Conway tim.conway@philips.com 303.682.4917 Philips Semiconductor - Longmont TC 1880 Industrial Circle, Suite D Longmont, CO 80501 Available via SameTime Connect within Philips, n9hmg on AIM perl -e 'print pack(nnnnnnnnnnnn, 19061,29556,8289,28271,29800,25970,8304,25970,27680,26721,25451,25970), ".\n" ' "There are some who call me.... Tim?" Harry Putnam <reader@newsguy.com> Sent by: rsync-admin@lists.samba.org 04/22/2002 10:00 PM To: <rsync@lists.samba.org> cc: (bcc: Tim Conway/LMT/SC/PHILIPS) Subject: Re: no output from perl script Classification: Robert Silge <rsilge@mac.com> writes:> You are quite right. I fixed that up, here is the current incarnation ofthe> script, but still no output revealed. > > Also if anyone who actually knows something about perl could tell me howto> have it reprint the first print command if a valid choice is not entered > that would be pretty slick. I could add an "exit" choice so that itwould> keep giving you a chance to get it right until you type exit. > > But that's not really essential. Seeing what's going on is (or at least > close to essential).Rob, here is a little different approach. It makes use of variable to a greater extent, and prints a menu where choices are made until one takes you out of the program. It also makes use of subroutines to contain the various actions. You'll note I used the notation `&subroutine' to call the functions. There are other ways, but I like that one. You'll notice too that I only filled in the first subroutine to give the idea, the others are just simple print and exit. You'll have to file them in. Now about seeing the output of the rsync command. There are several ways. Using the backtic like you did is one. But I'd use hte notation qx(command args targets). Just for clarity. It works same as backtics. But you need to put it in a print statement to see the output like this: print qx($cmd $flags $remote $local); exit; Another way and probably the cleanest is to use the filehandle approach where you use perls filehandle notation to run a process # Notice the pipe at the end. That allows you to get at the output in a # while loop. open(PROC,"$cmd $args $remote $local|"); while(<PROC>) { # print each line. But you could do other stuff here too print $_; } # Close the process close(PROC); exit; Either of those techniqes will do it. I'm leaving it up to you to see how to fit them into opt setup. But basically just insert the filled out code in the appropriate sub function. I did'nt debug this so it is definitely only an example of one way approach the problem. =======================================#!/usr/bin/perl -w ## BEGIN VARIABLE[s] =========================================== $remote = "SOME.MACH:/home/user/rsync-testing"; $local = "/home/user"; $cmd = "/path/to/rsync"; $flags = '-auvz --progress --delete -e ssh'; ## BEGIN SCRIPT BODY ========================================== ## Start up a while loop that will display a menu until we exit ## or our choice completes the program do { do { &display; chomp($opt = <STDIN>); }while ($opt < 0 && $opt >3); if ($opt == 1) { &opt1 } if ($opt == 2) { &opt2 } if ($opt == 3) { &opt3 } ## A rule that keeps the loop running until we opt out. } while ($opt != 3); ## BEGIN FUNCTION[S] ==========================================sub display { print <<EOM; Which computer do you wish to delete files from? (type corresponding number) 1] Remote 2] Local 3] quit EOM } sub opt1 { print "we are doing option one\n"; print <<EOM; This command will DELETE EVERYTHING in Local~/Research UNLESS it is also found in Remote~/Research. Do you really wish to do this? (Anything but a \`yes' here will abort the action) [N/yes]: EOM chomp($answer = <STDIN>); if ($answer eq "yes") { print "\nOK, here we go.\n"; print " This is an example only so it only prints a possible command\n"; print "Here is where to use the qx notation or PROC notation\n"; print "$cmd $flags $remote $local\n"; exit; } } sub opt2 { print "we are doing option two\n"; exit; } sub opt3 { print "We are doing option three... we gone ..bye bye\n"; exit; } -- To unsubscribe or change options: http://lists.samba.org/mailman/listinfo/rsync Before posting, read: http://www.tuxedo.org/~esr/faqs/smart-questions.html
I see a lot of good advice has been posted about stuff that is quite good to know about perl. A simpler solution though might be the File::Rsync module available on CPAN. I use it a lot and it works very well. It will generate the rsync command line for you, and it can be given a callback which will be called with each line of rsync's output. Or you can grab the output as a list after the command completes. The only shortfall is it gets output one line at a time, so while --verbose will work well with it, --progress is less useful because it updates the percent ticker without moving on to the next line, so your callback won't see it until that file is done. -- Joseph Annino Consulting - Perl, PHP, MySQL, Oracle, etc. jannino@jannino.com - http://www.jannino.com On 4/22/02 7:17 PM, "Robert Silge" <rsilge@mac.com> wrote:> Here is the first Perl script I've tried to write to run rsync. It seems to > work, but I don't see the progress like I should. I apologize if it's > sloppy, I should add that it's not only the first perl script I've written > for rsync, it's the first perl script I've written at all. > > ___________________________ > #!/usr/bin/perl -w > > print " > This command will DELETE EVERYTHING in Remote~/Research > UNLESS it is also found in Local~/Research. > Do you really wish to do this? > [N/yes]: "; > > $answer = <STDIN>; > chomp $answer; > > > if ( $answer eq "yes" ) { > print "\nOK, here we go.\n"; > `rsync -auvz --progress --delete -e ssh > Remote:/home/user/rsync-testing /home/user` > } > > else { > print "\nabort mission\n"; > > } > ____________________________ > > Thanks for the help. > > -Rob >