Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help for exec with command lines with spaces

Reply
Thread Tools

help for exec with command lines with spaces

 
 
PilotMI80
Guest
Posts: n/a
 
      11-26-2004
Hi,

I try to issue the command :
c:\program files\flashget\flashget.exe http:\\url.with.no.white.space
d:\a directory

(same can be tested with any prog that need a directory (potentially
containing whitespaces) as a parameter )

the url parameter does not contain whitespace so it is not a matter


so far, I tried many combinations (to escape baskslashes and quotes)
with the back tick (seems to work better for me although i don't need
the output), and with exec but to no avail :

here are a few :
1.
exec "c:\\program files\\flashget\\flashget.exe url d:\\a directory";
output> 'c:\program' not found (expected)

2.
exec "\"c:\\program files\\flashget\\flashget.exe\" url";
works ok but
exec "\"c:\\program files\\flashget\\flashget.exe\" \"url\"";
outputs 'c:\program' not found

3.
my @exe = ("\"c:\\program files\\flashget\\flashget.exe\" url \"d:\a
directory\"");
exec {$exe[0] } @exe; #as read in the manual
outputs nothing but does nothing because the command issue is wrong
(I assume the program is not the whole line so...)

4.
my @exe = ("\"c:\\program files\\flashget\\flashget.exe\"");
exec {$exe[0]} @exe, url; # or "url" or 'url' or " url" or ' url' ...
compiles but does nothing

I run out of ideas and am still stuck, by now, i do something like
this :
`"\"c:\\program files\\flashget\\flashget.exe\" url d:\\a_directory\"
which works but is quite limited.

any suggestion ?

thanks
 
Reply With Quote
 
 
 
 
PilotMI80
Guest
Posts: n/a
 
      11-28-2004
Finally I managed to do what I wished :
my $ProgramExe = "\"C:\\The path to the\\executable.exe\"";
my $OneArgument = "OneArgumentThatDoesNotContainSpace";
my $SecondArgument = "\"D:\\Another Argt\\that contains spaces\"";

system("start \"\"", $ProgramExe, $OneArgument, $SecondArgument);

Some explanations for the ones who may encounter this concern (and
feel free to correct me if I am wrong or not precise enough) :
- system behaves differently whether it is passed a scalar argument or
a list. I will not repeat the FAQ but it is often useful to force LIST
context to avoid misinterpretation by the shell. (So we meet the form
exec {"FakeName"} @ForcedToBeListCommandLine)

- I use "start" because I'm running on Win32, so it let the system
launch a program as if it was launched from the start menu/Run.
Otherwise, the script waits for the command to end (exec does not work
neither cause it just turns the script process to the one just
launched and there is no point of return)

- \"\" is the FakeName, the name we invoked the program under. (In
this case I don't need to pretend my program was launched under a
particular name so I let it blank)

- $ProgramExe and $SecondArgument were quoted in order to have the
white spaces preserved on the command line issued;
- $OneArgument does not contain any white space so I let it unquoted

- I kept my different arguments in a LIST (and thus separated) not to
have to rebuild my command line for any argument I want to issue but
they could have been grouped together into one argument.

Hope it will help someone
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      11-28-2004

Quoth (PilotMI80):
>
> I try to issue the command :
> c:\program files\flashget\flashget.exe http:\\url.with.no.white.space

^^ //
> d:\a directory
>
> (same can be tested with any prog that need a directory (potentially
> containing whitespaces) as a parameter )


Either use list exec wih the correct (unescaped) path:

my @args = (
'c:/program files/flashget/flashget.exe',
'http://stuff',
'd:\\a directory',
);
exec @args;

(btw, you have read perldoc -f exec, haven't you?), or quote it
correectly for your shell (IIRC this should work for cmd.exe):

exec map s/\s*\n\s*/ /g, q{
"c:\\program files\\flashget\\flashget.exe
http://stuff
"d:\\a directory"
};

I am here assuming that flashget.exe requires paths to be \-separated:
IME, most windows programs do, even though windows itself (and therefore
perl) are quite happy with / paths.

Ben

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
(Kate Rusby)
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-30-2004

Quoth (PilotMI80):
> Finally I managed to do what I wished :
> my $ProgramExe = "\"C:\\The path to the\\executable.exe\"";
> my $OneArgument = "OneArgumentThatDoesNotContainSpace";
> my $SecondArgument = "\"D:\\Another Argt\\that contains spaces\"";
>
> system("start \"\"", $ProgramExe, $OneArgument, $SecondArgument);


You don't need any of this mess. Perl on Win32 knows how to quote
arguments correctly for the standard command-line parsing routines:

my $ProgramExe = 'C:\\The path to the\\executable.exe';
my $OneArgument = 'OneArgument';
my $SecondArgument = 'D:\\Another argt';

system 'start', $ProgramExe, $OneArgument, $SecondArgument;

> Some explanations for the ones who may encounter this concern (and
> feel free to correct me if I am wrong or not precise enough) :
> - system behaves differently whether it is passed a scalar argument or
> a list. I will not repeat the FAQ but it is often useful to force LIST
> context to avoid misinterpretation by the shell. (So we meet the form
> exec {"FakeName"} @ForcedToBeListCommandLine)


Perl will not call the shell for a one-arg system or exec unless the arg
contains shell metachars.

You have got the indirect object and the first arg the wrong way round:
the indirect object is what is actually being executed. In any case,
they should be the same (unless you need them different), so to run a
command with no args but with spaces in the name use

my $cmd = '/path to/command';
system {$cmd} $cmd;

> - I use "start" because I'm running on Win32, so it let the system
> launch a program as if it was launched from the start menu/Run.
> Otherwise, the script waits for the command to end (exec does not work
> neither cause it just turns the script process to the one just
> launched and there is no point of return)


Yup. You could also use Win32:rocess, if you wanted more control over
your 'child'.

> - \"\" is the FakeName, the name we invoked the program under. (In
> this case I don't need to pretend my program was launched under a
> particular name so I let it blank)


No. I am actually rather surprised this worked at all...

You have *not* used the indirect object syntax here. Those ""
are inside the first arg, so what you are asking Perl to do is execute a
program called (literally)

start "".exe

(or some other executable extension). The fact that the windows
doesn't use the C arg-passing convention means that it worked out what
you meant anyway...

> - $ProgramExe and $SecondArgument were quoted in order to have the
> white spaces preserved on the command line issued;


Nope. Perl will deal with this for you if you use system LIST.

> - I kept my different arguments in a LIST (and thus separated) not to
> have to rebuild my command line for any argument I want to issue but
> they could have been grouped together into one argument.


Nope. system LIST is quite different from system SCALAR, as you said to
start with.

Ben

--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
The Levellers, 'Believers'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
Exec/System call with spaces in exec path problem ... Random Task Perl Misc 12 12-04-2005 10:03 AM



Advertisments