no output from perl script

Harry Putnam reader at newsguy.com
Mon Apr 22 21:04:02 EST 2002


Robert Silge <rsilge at mac.com> writes:

> 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, 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;
}





More information about the rsync mailing list