Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Backslashes in command arguements

by tygur (Initiate)
on Feb 26, 2007 at 16:50 UTC ( [id://602166] : perlquestion . print w/replies, xml ) Need Help??

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

Hello, I'm trying to get my script to pass an argument to the PStools command psinfo, which requires the remote computer name in the \\hostname format. However whenever I run it, it defaults to the local machine. I think this is happening because Perl isn't parsing the backslashes correctly. How can this be done? This is running on Windows XP. The lines involved in the script looks like this:
$hostName = $ARGV[0]; my $temp = `c:\\"Program Files"\\pstools\\psinfo.exe -s -c $hostName` +|| "no dice";

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: Backslashes in command arguements
by holli (Abbot) on Feb 26, 2007 at 17:15 UTC
    Any special reason why you want to shell out to psinfo? There's the Win32::Process family of modules on CPAN.


    holli, /regexed monk/
      I'm using PStools only because I have to get this done quickly for the New DST changes, and am not yet familiar with the Win32 modules. Will learn them though eventually. Thank you everyone for you help. It's working properly now!
Re: Backslashes in command arguements
by Joost (Canon) on Feb 26, 2007 at 17:07 UTC
Re: Backslashes in command arguements
by smithers (Friar) on Feb 27, 2007 at 05:17 UTC
    tygu: you can often also use unix-style slashes instead of the Windows-style backslash escaped with another backslash. For example, on my XP laptop any of below work fine with glob:
    my @filelist = glob( "c:\\temp\\*" ); my @filelist = glob( "c:/temp/*" ); my @filelist = glob( "//foobar/CDShare/*" ); my @filelist = glob( "//foobar/c\$/temp/*" );
    Note that the Windows Admin share (C$) is shown with the $ escaped (C\$).
Re: Backslashes in command arguements
by kyle (Abbot) on Feb 26, 2007 at 17:06 UTC

    It would probably be useful to print out $hostName before you use it. Your double backslash on the command line ("foo.pl \\hostname") may be converted to a single backslash when Perl gets it, but I'm guessing that it's getting converted in the backticks. You can escape them before it gets there like so:

    $hostName =~ s/\\/\\\\/g;

    Update: Thanks to ikegami for pointing out that my speculation was off the mark. DOS does not mangle backslashes on the command line. I thought it might, so I suggested printing out the variable to be sure. I'd have checked this myself, but I don't have Perl on Windows to try.

      Your double backslash on the command line ("foo.pl \\hostname") may be converted to a single backslash when Perl gets it

      No.

      >perl -e "print $ARGV[0]" \\hostname \\hostname

      If there's a slash missing, it was removed *before* Perl got it.