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

Just started perl. Like 2 days ago. Right now i'm *trying* to use it instead of a batch file, because of its better substitution featrues. The only problem is that i cant seem to get it to run, i think partly cause it is a windows machine.

The script is supposed to dump a sever to a local disk, as a rudimetary form of backup. Mainly this is being done for learning.

So here it is.

#!/usr/bin/perl #Perl script to backup sever. Jan 7th 2004. $server = drive; chomp($date = system("date \t")); system("mkdir C:\Server Backup\$date"); system("xcopy "C:\Documents and Settings\PD\My Documents\* c:\server B +ackup\test\"";
Now i always get errors about improper escapes, and when i use -w, i get problems with main:date. I dont really know what is wrong with this.

20040108 Edit by jeffa: Changed title from 'help.'

Replies are listed 'Best First'.
Re: Improper escaping error when executing system command
by Roger (Parson) on Jan 08, 2004 at 01:46 UTC
    The backslash and double quotes need to be escaped. But you could replace \ with /, and change the double quote in xcopy to single quote... or you could do it the Perl way ;-)

    use strict; use warnings; use File::NCopy qw/copy/; my %config = ( server_dir => "D:/ServerPath", source_dir => "C:/Documents and Settings/PD/My Documents", ); # get date in yyyymmdd format my ($year, $month, $day) = (localtime(time))[5,4,3]; my $date = sprintf "%04d%02d%02d", $year+1900, $month+1, $day; # create target directory my $dest = "$config{server_dir}/$date"; mkdir $dest or die "Can not create directory $dest!"; # copy files print "Copying files for '$date'\n"; copy \1, $config{source_dir}, $dest;


Re: Improper escaping error when executing system command
by ysth (Canon) on Jan 08, 2004 at 01:19 UTC
    In a double-quoted string, \ is used to escape characters with special meanings. Since you actually want a \ in the string, put \\ instead.

    Update: system doesn't return the output of the date command. You want chomp($date = `date`) for that. And I'm unclear on what that \t is supposed to be for.

Re: Improper escaping error when executing system command
by davido (Cardinal) on Jan 08, 2004 at 01:32 UTC
    On a windows machine it's unlikely that #!/usr/bin/perl is the proper path to your Perl interpreter. It is also possible that the shebang line isn't even doing much for you in a Windows environment, aside from specifying perl runtime switches.

    Next, where does drive come from? That's not a core function I know of.

    Others have mentioned that backticks should be used in place of system when you're trying to capture the output. See perlop.

    Your final system command (system("xcopy "...) has a few problems. First, its parenthesis aren't balanced. Next, you can't just put quotes inside of quotes without escaping them... not if you expect Perl to know what you mean.

    Welcome to the monastery, welcome to Perl, enjoy the ride!


    Dave

Re: Improper escaping error when executing system command
by BrowserUk (Patriarch) on Jan 08, 2004 at 04:17 UTC

    A useful thing to know when constructing strings for use with system on win32 is qq operator.

    Using the qq// quoting operator removes the need for escaping your double quotes whilst retaining interpolation (substitution). You will still need to double your backslashes though.

    Although perl lets you get away with using forward slashes for most everything internally, many external commands, and all those built into cmd.exe (dir type rename cd rd etc.) don't work with forward slashes in paths.

    system qq[ dir "d:\\program files\\win*.*" ]; my $src = "d:\\program files\\*.*"; system qq[ xcopy /s "$src" x: ];

    This avoids most of the need for most backwacking and makes thing clearer to read.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

Re: Improper escaping error when executing system command
by whiteskin (Initiate) on Jan 08, 2004 at 03:17 UTC
    Thanks guys. I wasent really sure how to do the file operations. I havent really gotten that far along yet. I cant say that i knew that i could flip the slashes though. Thanks, for all your help.