Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Output redirection problem in Windows ActivePerl

by sagarkha (Acolyte)
on Sep 20, 2010 at 12:08 UTC ( #860823=perlquestion: print w/replies, xml ) Need Help??
sagarkha has asked for the wisdom of the Perl Monks concerning the following question:

system("ping $_ >/root/scripts/log.txt"); The above code works for me in Linux But in windows, the simililar code does not work system("ping $_ >c:\temp\log.txt"); By using, single cotes, i can use it like system('ping 10.56.1.1 >c:\temp\log.txt'); But my scripts requires this command to be used in a loop, but this do +esn't permit to use scalar variable. system('ping $_ >c:\temp\log.txt'); So this command doesnt work

Replies are listed 'Best First'.
Re: Output redirection problem in Windows ActivePerl
by Corion (Pope) on Sep 20, 2010 at 12:15 UTC

    Backslashes in double quotes behave differently from backslashes in single quotes. Your \t in c:\temp gets interpreted as tab character in double quoted strings. See perlop.

    The solution is to check the command you're about to execute:

    my $cmd = "ping $_ >c:\temp\log.txt"; print "About to run [$cmd]\n"; system($cmd) == 0 or die "Couldn't run [$cmd]: $!/$?/$^E";

    In the short run, doubling all backslashes will do what you want:

    my $cmd = "ping $_ >c:\\temp\\log.txt"; print "About to run [$cmd]\n"; system($cmd) == 0 or die "Couldn't run [$cmd]: $!/$?/$^E";
Re: Output redirection problem in Windows ActivePerl
by cdarke (Prior) on Sep 20, 2010 at 15:28 UTC
    Depending on the version of cmd.exe, you might be able to use forward slashes instead for the directory seperator. That works OK on Windows 7.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://860823]
Approved by AnomalousMonk
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2016-12-11 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (168 votes). Check out past polls.