Win2k CMD shell variables and backtick execution

B

buildmorelines

I want to do backtick execution (to capture STDOUT to a variable for
matching/error handling) with a shell variable in it. If I type it
into a C prompt

C:\>"%PROGRAMFILES%\Windows Media Player\mplayer2.exe" #works fine

When I do

C:\>%PROGRAMFILES%\Windows Media Player\mplayer2.exe

I get

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

Now if I do it in Perl as (heres a list of everything I tried)

system('%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe');
system("%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe");
system("\%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe");
system'"%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe"';
`"%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe"`;
`\"%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe\"`;
qx/"%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe"/;
qx!"%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe"!;
qx#"%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe"#;
qx("%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe");

$run = '"%PROGRAMFILES%\Windows Media Player\mplayer2.exe"';
`$run`;

$run = '%PROGRAMFILES%\Windows Media Player\mplayer2.exe';
`$run`;

I get

'C:\Program' is not recognized as an internal or external command,
operable program or batch file

I've spent 3 hours trying to do this.
The only code I found that does it (written by myself), is this
totally crazy, and there has to be a better, more efficent way. It is
below.

#!/usr/bin/perl
$data = `echo %PROGRAMFILES%\\Windows Media Player\\mplayer2.exe`;
chop $data;
$data = '"' . $data . '"';
print "\n data is \n\n $data \n\n";
`$data`;

Idealy I would like this to work.

$run_result = `%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe`;

Also I know that I need to manually close Windows Media Player for all
of these to work but thats not the point and I have a solution for
that (cmd /c or start).
 
M

mothra

Idealy I would like this to work.

$run_result = `%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe`;
Try this:

#!/app/perl5.8.0/bin/perl
use strict;
use warnings;

my $cmd = "\"$ENV{ProgramFiles}\""."\\". "\"Windows Media
Player\"\\mplayer2.exe";
system ("$cmd");

Should do what you want

Mothra
 
M

mothra

mothra said:
system ("$cmd");
ops!!! shame on me for not checking the return status of a system call
should be

system ("$cmd") == 0 || die "Error in system call: $?\n";

Mothra
 
A

A. Sinan Unur

(e-mail address removed) (buildmorelines) wrote in
I want to do backtick execution (to capture STDOUT to a variable for
matching/error handling) with a shell variable in it. If I type it
into a C prompt

C:\>"%PROGRAMFILES%\Windows Media Player\mplayer2.exe" #works fine

When I do

C:\>%PROGRAMFILES%\Windows Media Player\mplayer2.exe

I get

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

Now if I do it in Perl as (heres a list of everything I tried)

system('%PROGRAMFILES%\\Windows Media Player\\mplayer2.exe');

It would help you to actually print each string to see what you are
trying to execute.

Here's what works on my system:

#! perl

use strict;
use warnings;

use File::Spec::Functions;
my $prog = catfile($ENV{ProgramFiles},
'Windows Media Player', 'wmplayer.exe');
print "[ $prog ]\n";

system($prog) == 0
or die "System failed: $?";
__END__
I've spent 3 hours trying to do this.
The only code I found that does it (written by myself), is this
totally crazy, and there has to be a better, more efficent way. It is
below.

#!/usr/bin/perl
$data = `echo %PROGRAMFILES%\\Windows Media Player\\mplayer2.exe`;
chop $data;
$data = '"' . $data . '"';
print "\n data is \n\n $data \n\n";
`$data`;

Idealy I would like this to work.

But it can't. Perl is not your shell. So, how do you expect it to know
what to do with the % signs? On the other hand, you do have acess to
%ENV. Use it.

Sinan.
 
B

buildmorelines

Thanks, I never knew perl had a special variable to get into shell variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top