Writng console output to a file

R

rg.iitk

Hi,
I need a way to write the output on the console to a file,
for ex:
if I need to list dir I use the command
system("ls");
the list of dirs and files are printed on the console, now I need to
store this in a file. How to do this? I need to store output that come
from using system() commands.
Plz help.

TIA

Rohit
 
S

Stephen O'D

I am pretty sure

my $var = `ls ...`; will do that.

The backticks execute a command, like system - however the system
command returns you only a status (generally 0 indicating success and
non zero for failure - but you need to check the man page of the
command you are running).

Backticks gather up all the output that would normally goto the shell
and put it into $var, so you can then manipulate it in you script.

Or you could do

my $status = system("ls -al ... >logfile.log");

If you dont need the output in Perl.
 
J

Jürgen Exner

I need a way to write the output on the console to a file,
for ex:
if I need to list dir I use the command
system("ls");
the list of dirs and files are printed on the console, now I need to
store this in a file. How to do this?

1: use the redirect to file command of your command shell, typically a ">"
(this has nothing to do with Perl)
2: capture the output of the external command (see "perldoc -f system",
third paragraph, third sentence; you did read the documentation for the
functions that you are using, didn't you?), then open() a file and print()
to it.

jue
 
J

Jürgen Exner

I need a way to write the output on the console to a file,
for ex:
if I need to list dir I use the command
system("ls");
the list of dirs and files are printed on the console, now I need to
store this in a file. How to do this?

1: use the redirect to file command of your command shell, typically a ">"
(this has nothing to do with Perl)
2: capture the output of the external command (see "perldoc -f system",
third paragraph, third sentence; you did read the documentation for the
functions that you are using, didn't you?), then open() a file and print()
to it.

jue
 
S

Sherm Pendley

In UNIX
@file = qx# ls -l /some/dir#;

works for me.

Works for what? Please quote some context when posting a followup
message. It's long-standing usenet tradition, and it's requested in
both this group's guidelines and Google Groups' guidelines.

sherm--
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top