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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

system command in NT

Status
Not open for further replies.

sureshp

Programmer
Aug 28, 2000
79
0
0
Hi all,

I am trying to run a simple system command in NT using perl. I am having the code as

$cmd="dir > output.txt"; # or just $cmd=dir;
system $cmd;

but I am not getting any output. Will it be a problem with the version of PERL I am
having. If I have a perl.exe, How can I find the version of PERL that I have.


Thanks in advance,

Suresh
 

What do you mean by "not getting any output"?

When you use the system command output would go to STDOUT, but you are redirecting STDOUT to a file, so nothing should come to the screen. Personally I prefer to treat anything that might be a function as a function so I would write

system($cmd);

That probably doesn't really matter, but you can try it.


You can see your version of perl by issuing the command
perl -v
on the command line.
perl -V
gives you information about your libraries and build data.

But the system() command has worked for years and years.

Have fun!
Kai.
 
Hi kai,
Thanks for your reply.
I mean I am not getting the file output.txt after doing the command
$cmd="dir > output.txt";
system ($cmd);
Do you have any more solutions???

Thanks

Suresh
 
Have you checked the return code from the system call to make sure it is working? To do it, try this:
Code:
$rc = system($cmd);
$rc = $rc / 256; # divide by 256 to get the real return code
print "The system call returned $rc\n";
Also, the perlfunc manpage says: "exec and system to not flush your output buffer, so you may have to set $| to avoid lost output." So try adding this line before the system call:
Code:
$| = 1; # flush buffer after every print
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi tracy,
I tried your option also, I am getting a return code of 65536(without dividing 256). But I am not getting the result even I clear the buffer.

Thanks anyway

Suresh
 
That means you're getting a return code of 256, which since it's non-zero I would think indicates that for some reason your system command is not working. I just ran the same thing thru Komodo on my Win98 machine and got a correct output file. Maybe it's a permissions thing on the directory you are in. Do you get an empty output.txt file, or none at all?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi Tracy,
I tried both the way. Even If I have the output.txt file,
it's not getting updated after the system command.

Thanks,
suresh
 
your perl script probly doesn't have the permissions to create or alter files at the location you're trying this. try just a system("dir"); to make sure that part is working (which really shouldn't be having any problems). if you get errors here, that means something is very strange and wrong and scary. next, try doing exactly what you want the script to do on the command line to see if you yourself (or whatever user will be executing the script) have the ability to do it (that is, type dir > output.txt in a dos window and see what happens). if you get errors here, that means it has to do with the permission settings of the directory the script is executing in, and you'll need to set them to be writable by the user that the script runs as. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Alternatively, you could create the output.txt file in a directory that you KNOW can be written to. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi stillflame,
I have just tried with two simple lines of code like

$var=system("dir"); ### file name test.pl
print $var;

From DOS prompt I am running like
D:\Suresh\test>D:\Product\perl.exe test.pl

I am getting a return value of 65536(actual code 256). I don't know why it's failing.Really I'm scarred about this part.Any more suggestions.

Thanks,

Suresh
 
Try adding "$!" to the print, to find out the error.

print "$var, $!";


 
Hi,

I tested this also. I am just not getting anything for the $! variable, As you said I tried
print ("$!,$var");

I am just getting the O/P as
,65536

Thanks

Suresh.
 
Hi everyone,
I forget to mention the version of PERL. It's PERL for NT Revision 4.0.1.0 Build 3 patch level 36.
Is the problem due to Perl version?. Once I know the problem is due to the version,then I can update it to 5.0.
But without checking I don't want to update the version at production.

Thanks

Suresh
 
Suresh -- go to and upgrade to the current stable version. I doubt you'll regret it. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top