Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-28-2008, 02:11 AM   #1
LQ Newbie
Registered: Mar 2008
Posts: 3

Rep: Reputation: 0
How to redirect the output of exec command output in perl?

In my code i want to redirect the o/p of exec to one file is it possible?

else {

if ($pid = fork) {


elsif (defined $pid) {

chdir $chdir if $chdir;

eval $extra if $extra;

exec $cmd or die "Unable to exec $cmd: $!\n";

} else {

die "Unable to fork: $!\n";



here $cmd am passing one processor cmd line i want to preserve the o/p of that processor in to one file.

Can anyone help me?

Old 03-28-2008, 09:29 AM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This small example should explain:
#! /bin/perl -w
use strict;

    # open a command with command's output piped back to us
    open( DIR, "ls|" ) || die "No ls"; 
    while( <DIR> ){      # read command's output from the pipe
        print $_;        # do something with what we read back from the proces
    close DIR;         # be done
You can use the same technique to pipe standard input into the command (but not both input & output).
No explicit fork() necessary. See also, readpipe.

--- rod.

Last edited by theNbomr; 03-28-2008 at 09:30 AM.
Old 03-28-2008, 04:35 PM   #3
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
The output of a command is directed to STDOUT. we can redirect this to a file for our use.

To read both a command's STDOUT and its STDERR separately, it's easiest and
safest to redirect them separately to files, and then read from those files
when the program is done: system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr");


Last edited by prad77; 04-17-2008 at 03:29 AM.
Old 08-28-2009, 03:35 PM   #4
LQ Newbie
Registered: Jan 2009
Posts: 3

Rep: Reputation: 0
This is probably far too late to be helpful, but I bumped into this thread just now. You could add the desired redirection to your 'exec' command, e.g.:

exec "$cmd 1>stdout.text 2>stderr.txt" or die "Unable to exec $cmd: $!\n";

Hope this helps,



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect the SCP command output to text file. gokuls Linux - General 10 02-22-2012 02:12 AM
find with -exec: no command output David the H. Linux - Software 5 01-03-2008 01:53 AM
how to redirect the output of find command to input of egrep babu198649 Linux - Newbie 2 12-27-2007 03:50 AM
php: output the result of exec( $command ) zovres Programming 6 08-27-2004 06:41 PM
How to redirect standard output of piped command to log file andrewstr Linux - General 10 02-04-2004 02:07 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration