perl: looping through the output of a 'system' command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: looping through the output of a 'system' command
# 1  
Old 04-05-2010
perl: looping through the output of a 'system' command

Hi there

could anybody point me in the right direction when it comes to looping through the output of a system command in perl (i.e. df -k) doing a test against each line to see if it matches?

for example if i have a df -k output like this and I wanted to grab the lines that matched "sda" or "udev" in the first column

Code:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda3             78412744   4670368  69759224   7% /
udev                   1915252       304   1914948   1% /dev
none                   1915252       540   1914712   1% /dev/shm
none                   1915252       112   1915140   1% /var/run
none                   1915252         0   1915252   0% /var/lock
none                   1915252         0   1915252   0% /lib/init/rw
none                  78412744   4670368  69759224   7% /var/lib/ureadahead/debugfs
/dev/sda2            232571420  75736032 156835388  33% /media/Other HD

any pointers would be great
# 2  
Old 04-05-2010
From perldoc -f open:
Quote:
If the filename begins with '|' , the filename is interpreted as a command to which output is to be piped, and if the filename ends with a '|' , the filename is interpreted as a command which pipes output to us. See "Using open() for IPC" in perlipc for more examples of this. (You are not allowed to open to a command that pipes both in and out, but see IPC::Open2, IPC::Open3, and "Bidirectional Communication with Another Process" in perlipc for alternatives.)

For three or more arguments if MODE is '|-' , the filename is interpreted as a command to which output is to be piped, and if MODE is '-|' , the filename is interpreted as a command which pipes output to us. In the 2-arguments (and 1-argument) form one should replace dash ('-' ) with the command. See "Using open() for IPC" in perlipc for more examples of this. (You are not allowed to open to a command that pipes both in and out, but see IPC::Open2, IPC::Open3, and "Bidirectional Communication" in perlipc for alternatives.)
So what you need would be something like (untested, needs error checking)
Code:
open my $cmd, '-|', 'df -k';
while ($line = <$cmd>) {
}
close $cmd;

# 3  
Old 04-05-2010
thanks Pludi, im not entirely sure i understand exactly what is going on here. but it seems to work as you expected. Specifically, i dont understand the comma separated open/variable declaration bit...... this technique is very new to me

I assumed perl would have a syntactically simple and intuitive way of performing an action such as this. i guess not Smilie
# 4  
Old 04-06-2010
The open could also be written as
Code:
open my $file 'df -k|';

, but I find the 3 parameter form (variable, mode, "file") to be more readable. And the variable declaration is advisable here, unless you're reusing an older variable (you are using use strict;, aren't you?)

As for more intuitive, do you mean something like
Code:
print foreach `df -k`

If so, it's shorter, but much more unmaintainable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command output format in email is not same as on the system

Hi I have script to collect file system usage statistics from few remote unix hosts and email . On the UNIX system the column spacing is fine but the email output is not aligned properly. Any tips to fix this ? (1 Reply)
Discussion started by: new2prog
1 Replies

2. Shell Programming and Scripting

Redirect system output to null in perl

Hi Guys, Please help me.. it is urgent. I am writing a perl script to capture command output and redirect it to a logfile.At the same i want to check the return code of the command and log it if the command is not succesful in my logfile.. Here is my code, it is working but system command inside... (2 Replies)
Discussion started by: sriramperumalla
2 Replies

3. Shell Programming and Scripting

How to call the System command twice in the same perl script...

Hello experts, I have a perl script which looks for the ARGV and then loads the data as per it. Example. #Checking the server to connect if ($ARGV eq 'QA') { $ENV{"ORACLE_HOME"} = "/oracle/product/11.2.0"; $ENV{"PATH"} = "$ENV{'PATH'}:/oracle/product/11.2.0/bin"; ... (1 Reply)
Discussion started by: msrahman
1 Replies

4. Shell Programming and Scripting

file handling in perl without using system command

Hi , Is there any way to achieve following using perl program (i.e without using system command). 1.system ("echo 'test' > /usr/spool/ship.csv"); 2.system ("cat /usr/ajay_test* >> /usr/spool/RAM/work/patil.csv"); 3.system("> /usr/spool/ajay.txt"); e.g for system("rm -f... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

5. Shell Programming and Scripting

exit ststus 9 from perl system command

HI all, can anyone tell me what does exit status 9 from perl's system function meant. I am using system fuction to execute a shell script as : my $s=system ('script.sh' ,arg1 ,arg2); print $s; the output is 9. Thanks in advance. !!:confused: (1 Reply)
Discussion started by: glamo_2312
1 Replies

6. Shell Programming and Scripting

Run system command in perl cgi

Hi guys, got a problem with a perl cgi script over here. I need it to run a system command to get the status of a process. Unfortunately the process is owned by a specific user and only this user can get its status. So i tried running the command from the perl cgi with "su", but then i get the... (12 Replies)
Discussion started by: polki
12 Replies

7. Shell Programming and Scripting

Perl System command calls to variable

I am new to scripting in Perl so I have a dumb question. I know I can call system commands using system("date"); But I am not able to: 1. set its output to a variable 2. run in quiet mode(no output to the screen) The examples i have #!/usr/bin/perl print `date +\%y\%m\%d.\%H\%M`;... (5 Replies)
Discussion started by: 4scriptmoni
5 Replies

8. Shell Programming and Scripting

Need Help w/PERL system command

Hi, I'm wanting to run an nslookup, dig or whatever to check for the existence of a printer. The PERL script will display the results on the screen, but I can't figure out how to capture the result & test the value. Any ideas will be greatly appreciated!!! Thank You (1 Reply)
Discussion started by: lorik
1 Replies

9. Shell Programming and Scripting

perl - system command

Can this be done without using te system command? I have a directory with a large number of files in it, but I am interested in only the 8 most recent. The directory looks like -rw-rw-rw- 1 adsm adsm 13412 Sep 22 08:31 events_dump_09222005.csv.gz -rw-rw-rw- 1 adsm adsm ... (5 Replies)
Discussion started by: reggiej
5 Replies

10. Shell Programming and Scripting

Perl run system command

Can perl execute a system command similar to the C function System()? Thanks. Gregg (1 Reply)
Discussion started by: gdboling
1 Replies
Login or Register to Ask a Question