Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute an MSDOS cmd in perl?

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
I worte a piece of perl code to run an MSDOS cmd:

Code:
my $pp1 = `which powermt`;
chomp($pp1);
my $ret1 = &issue_cmd($pp1, '1st try');

my $pp2 = $pp1;
$pp2 =~ s/\s+/\*/g;
my $ret2 = &issue_cmd($pp2, '2nd try');

my $pp3 = $pp2;
$pp3 =~ s/\\/\//g;
my $ret3 = &issue_cmd($pp3, '3rd try');

my $pp4 = $pp2;
$pp4 =~ s/\\/\/\\/g;
my $ret4 = &issue_cmd($pp4, '4th try');

my $pp5 = $pp2;
$pp5 =~ s/\\/\\\//g;
my $ret5 = &issue_cmd($pp5, '5th try');

sub issue_cmd {
	my $cmd = $_[0];
	my $flag = $_[1];
	print "=============In issue_cmd(), $flag==================\n";
	my $ret = `$cmd 2>&1`;
	print "\$cmd = $cmd\n";
	print "$ret";
	print "====================================================\n\n";
	return $ret;
}

The output:
=========
Code:
=============In issue_cmd(), 1st try==================
syntax error: got (, expecting Newline
$cmd = C:\Program Files (x86)\EMC\PowerPath\powermt.exe
====================================================

=============In issue_cmd(), 2nd try==================
$cmd = C:\Program*Files*(x86)\EMC\PowerPath\powermt.exe
C:Program*Files*(x86)EMCPowerPathpowermt.exe: not found
====================================================

=============In issue_cmd(), 3rd try==================
$cmd = C:/Program*Files*(x86)/EMC/PowerPath/powermt.exe
C:/Program*Files*(x86)/EMC/PowerPath/powermt.exe: not found
====================================================

=============In issue_cmd(), 4th try==================
$cmd = C:/\Program*Files*(x86)/\EMC/\PowerPath/\powermt.exe
C:/Program*Files*(x86)/EMC/PowerPath/powermt.exe: not found
====================================================

=============In issue_cmd(), 5th try==================
$cmd = C:\/Program*Files*(x86)\/EMC\/PowerPath\/powermt.exe
C:/Program*Files*(x86)/EMC/PowerPath/powermt.exe: not found
====================================================

The above code is run on a windows server which has MKStool kit installed and
the partial system info is listed below:

Code:
OS Name:            Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition
OS Version:         5.2.3790 Service Pack 2 Build 3790
OS Manufacturer:    Microsoft Corporation
OS Configuration:   Standalone Server
OS Build Type:      Multiprocessor Free
System Type:        x64-based PC

Could someone please tell me what the correct implementation is?

Many thanks.
 
try this for now:

Code:
if ( chdir('C:/Program Files (x86)/EMC/PowerPath') ) {
   print "We are in the directory\n";
   print qx{dir '.'};
}
else {
   print "Failed : $!\n";
}

report back.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you, Kevin and sorry not getting back to you earlier.

The output of your code is:

We are in the directory
dir: not found

============================

However, I do have a few different ways to run a MSDOS cmd in perl. More to follow.
 
Below is a piece of my demo code.

Code:
use File::Basename;
use Cwd;

# 1st
# The command 'which' only works when
# 1) an MKStool kit or something similar is installed;
# 2) $cmd is set in PATH env
my $cmd = "powermt";
my $origDir = cwd;
my $ret = &_getResultsOnWin($cmd, $origDir);

# 2nd 
# The extension 'exe' has to be included!!
$cmd = "$ENV{HOMEDRIVE}/Program Files (x86)/EMC/PowerPath/powermt.exe";
if(-x $cmd) {
	$cmd = "$ENV{HOMEDRIVE}/Program*x86*/EMC/PowerPath/powermt.exe";
	$ret = &issue_cmd($cmd, '2nd try');
}

# 3rd
# The extension 'exe' does not have to be included!!
# And it does not require an MKStool kit or something similar
$cmd = "\"$ENV{HOMEDRIVE}\\Program Files (x86)\\EMC\\PowerPath\\powermt\"";
$ret = &issue_cmd($cmd, '3rd try');

exit;

sub _getResultsOnWin {
	my ($cmd, $origDir) = @_;
	my $pp = `which $cmd`; 
	print "\$pp = $pp\n";
	# $pp == C:\Program Files (x86)\EMC\PowerPath\powermt.exe
	chomp($pp);
	my $ppPath = dirname($pp);
	chdir($ppPath);
	my $ret = &issue_cmd($cmd, '1st try');
	chdir($origDir);
	return $ret;
}

sub issue_cmd {
	my $cmd = $_[0];
	my $flag = $_[1];
	my $ret = `$cmd 2>&1`;
	print "=================== Demo Only $flag ===========================\n";
	print "$ret";
	print "======================= End of Demo =============================\n\n";
	return $ret;
}

I like the first way better. Mike, do you have any comments/suggestions? Thank you again, Mike.
 
The output of your code is:

We are in the directory
dir: not found

That seems very strange. When you chdir into a directory it should be accessible with the . character.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top