Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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

Comment on Output redirection problem in Windows ActivePerl
Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-29 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls