Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

System command with quotes and variables

by mellin (Scribe)
on Apr 16, 2007 at 06:29 UTC ( #610271=perlquestion: print w/replies, xml ) Need Help??
mellin has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, i'm having a problem with my script that uses system command to run Runas.exe with a variable. The idea is just to make Perl ask for the user to run runas.exe with and then execute the command string with System().

Here's the command string i'm trying to make System to run:

runas /user:domain\$user "c:\Program Files\Internet Explorer\iexplore.exe \"c:\""

The only variable that needs to be interpolated is $user but all those quotes and escapes are creating a headache for me.

Replies are listed 'Best First'.
Re: System command with quotes and variables
by Zaxo (Archbishop) on Apr 16, 2007 at 06:41 UTC

    How are you quoting that string? I recommend putting together a list form of the arguments. The bare string '/user:domain\$user' is unlikely to do right. If interpolated, the '$' will be taken as literal because '\' escapes the '$'. I don't know what string is meant there. Try,

    system 'runas', "/user:domain/$user", 'c:\Program Files\Internet Explorer\iexplore.exe', '"c:\"' and die $?;
    The peculiar and logic is because system returns false on success. Most of perl isn't like that. I don't know what 'runas' looks for in its arguments, so you need to use your familiarity with that program to get this right.

    After Compline,

      I finally wrote it just like this:

      my $command = "C:\\Windows\\system32\\runas.exe /user:ctldomain\\$us +er \"c:\\Program Files\\Internet Explorer\\iexplore.exe \"c:\""; system($command) or print "error running command: $!\n" and exit;

      But i have to reconsider using a list as suggested by you and others.

        You can use forward-slash / or back-slash \ as a directory separator on Windows. Forward slash is easier since you don't have to escape it (cmd.exe and Windows Explorer only support \).
        You might also like to consider using qq to avoid escapes.
Re: System command with quotes and variables
by varian (Chaplain) on Apr 16, 2007 at 06:36 UTC
    You can avoid impact from quotes/escapes, if you provide the system function a list instead of a scalar it will not interpolate those args.

    From the perl system function documentation:
    If there is more than one argument in LIST, or if LIST is an array with more than one value, starts the program given by the first element of the list with arguments given by the rest of the list. If there is only one scalar argument, the argument is checked for shell metacharacters

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-06-25 04:04 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (564 votes). Check out past polls.