LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2006, 09:39 PM   #1
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
perl: send output of system() call to variable


I am writing a perl script that uses system("fdisk -l") and I want to send the output to a variable in the script instead of STDOUT. I have a perl book which says you can send the output to a file, will I have to send it to a file then read it into a variable, or is there an easier way to do this?

Thanks for your time!
...drkstr
 
Old 07-23-2006, 09:54 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
You're looking for the "open()" command here, which can be used to open streams to processes as well as normal files. Instead of using "<" or ">" to indicate what direction to open a stream (for input or output), use "-|" to read from a process or "|-" to write to it. In place of the filename, just provide the program to execute. In your case:
Code:
open($fh, "-|", "fdisk -l")
And then just read from the $fh filehandle like you normally would. You can even slurp it all up into a variable if you want.

An easier, but less flexible, alternative is the "readpipe()" command. It takes a single scalar parameter - the program to execute. In scalar context it returns a single, possibly multiline string with all the output of that program. In list context it returns a list of strings, each one line from the program's output. Again, in your example:
Code:
my $output = readpipe("fdisk -l")

Last edited by taylor_venable; 07-23-2006 at 09:59 PM.
 
Old 07-23-2006, 10:07 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
backticks should work also

Code:
$output = `fdisk -l`;
 
Old 07-23-2006, 10:16 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
True, they do; as does the qx quotation method.
Code:
$output = qx(fdisk -l)
But an important distinction is that backticks and qx both do variable interpolation in the command, while using open() or readpipe() leaves that choice more explicitly up to the user.
 
Old 07-23-2006, 10:47 PM   #5
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Quote:
to indicate what direction to open a stream (for input or output), use "-|" to read from a process or "|-" to write to it.
Thanks for the quick reply. That did the trick. I thought I had to use a file descriptor to redirect the output but I couldn't quite figure out how to put it together.

best regards,
...drkstr
 
  


Reply



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 send shell variable data to a text file ginda Programming 7 06-23-2006 05:49 AM
How to call another cgi program to return a variable <db> Programming 3 04-27-2006 03:36 AM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 12:40 PM
is there a system call to send process to run in the background? feetyouwell Programming 4 10-01-2004 04:58 AM
Perl: system() mail losing a variable value m0rl0ck Programming 8 09-09-2003 06:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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