Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm stuck on what's the best way to automate the following set of commands in perl:

cvs login Logging in to :pserver:ebizadmin@localhost:2401/usr/local/cvsroot CVS password: cd /home/js/apache/dd cvs import -m \"First configuration\" apache/dd ebizadmin start

I started a perl script using Expect, and managed to get as far as logging into cvs, but then wasn't sure how to run the cd, and cvs import commands (Sorry haven't used Expect before). This is as far as I've got but obviously it doesn't work:

#!/usr/bin/perl use Expect; #$Expect::Debug = 1; my $cvsroot=":pserver:ebizadmin@localhost:/usr/local/cvsroot"; my $password="secret"; $timeout=3; my $exp = Expect->spawn("cvs login") or die "Cannot spawn cvs login: $ +!\n";; my $spawn_ok; $exp->expect($timeout, [ 'CVS password: $', sub { my $fh = shift; print $fh "$password\n"; print $fh "cd /home/js/apache/dd\n"; print $fh "cvs import -m \"First configuration\" apa +che/dd ebizadmin start"; exp_continue; } ], [ timeout => sub { die "No login.\n"; } ]

Any pointers on the right way to do this would be much appreciated. By the way, the Cvs module on cpan doesn't do cvs import before tells me to use that!

Many thanks,

js.

Replies are listed 'Best First'.
Re: automating some cvs commands
by Tanktalus (Canon) on Nov 12, 2005 at 17:53 UTC

    A few ways to do this. First, the way you're trying to do this. You want to spawn "/bin/sh". And the first thing you want to do is expect a prompt, and then send it "cvs login". Then when that is done, you can send the shell more commands.

    Alternately, you can spawn cvs login, and once that is done, you can use chdir (in perl, don't send it anywhere), and then you can just use system to run your import. Note here that I generally like to use the list version of system, not the single string version:

    system(qw(cvs import -m), "First configuration", qw(apache/dd ebizad +min start));
    although the way you're doing it would work, too. You can also get rid of the backslashes by using different surrounding quotes:
    system('cvs import -m "First configuration" apache/dd ebizadmin star +t');
    Have you sent a note to the author of the Cvs module to ask about adding the import functionality?

      Tanktalus,

      Thanks for your reply. I see what to do now. I hadn't sent a note to the author, but will do as you suggest.

      Many thanks,

      js.