LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 03-25-2008, 03:15 PM   #1
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Rep: Reputation: 0
Perl: Running Command line apps in background and capturing output


Hey there,

I'm trying to write a perl script that will run an app in the background (ie: so it doesn't output any text) and, instead it stores it in a variable so i can parse it and spit it out. The only things I have tried (and know of) are qx/application/; and system("application");. using both of these methods, the program does not withold the data from the screen. Unfortunately, I couldn't find any (linux) help on the net.

Thanks in advance,

s0l1dsnak3123

ps. Here is what I have tried:

Code:
$out = system("gksu -u root \"obexftp -u ttyACX0 -v -l $dir\"");

$out = qx/gksu -u root "obexftp -u ttyACX0 -v -l $dir"/;

Last edited by s0l1dsnak3123; 03-25-2008 at 03:16 PM.
 
Old 03-25-2008, 04:13 PM   #2
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
You have to redirect your standard output to a file instead of the std output,i.e, screen.

Linux has got a file descriptor for standard output, which is 1 (similar to the 0 for standard input file descriptor).

$ ls -al 1> filelisting

This will redirect output to the file instead of screen.

Gentoo

Last edited by prad77; 04-17-2008 at 03:27 AM.
 
Old 03-25-2008, 04:38 PM   #3
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
Use backquotes.
my $output = `ls -l`;
 
Old 03-25-2008, 08:28 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Or use a piped open or IPC::Open2: http://perldoc.perl.org/functions/open.html
 
Old 03-27-2008, 03:23 PM   #5
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Original Poster
Rep: Reputation: 0
OK, thanks alot its working now

Only one problem... after doing alot of regex

Code:
$out =~ s/<\?xml[^>]+?>//ig; #gets rid of <?xml*?>
$out =~ s/<\![^>]+>//ig; #gets rid of <!*>
$out =~ s/<\!\-\-[^>]+\-\->//ig; #gets rid of <!--*-->
$out =~ s/<folder\-listing version="1.0">//ig; #gets rid of <folder-listing version=\"1.0\">
$out =~ s/<folder name="//ig;
$out =~ s/"\/>//ig;
$out =~ s/<\/[^>]+>//ig;
I get what I want plus tonnes of newline characters. The thing that I want is indented also.

Code:


Pictures Sounds Themes Videos Other
I want to get rid of the indentations, I want to remove the new lines above and below the list, and substitute the new lines within the list with a comma like so:

Code:
Pictures,Sounds,Themes,Videos,Other
I have tried the following:
Code:
while ($out =~ m/\t/) {
	$out =~ s/\t//;
}

while ($out =~ m/^+\n/){
	$out =~ s/^+\n//;
}
while ($out =~ m/\n+$/){
	$out =~ s/\n+$//;
}
while ($out =~ m/\n/) {
	$out =~ s/\n/\,/;
}
but I end up with no output :/ I do know, however that
Code:
while ($out =~ m/\t/) {
	$out =~ s/\t//;
}
does work on its own. Any of the Newline regexes just turn the output to nothing on their own.

I have tried googling the problem, but to no avail...

Please help

[edit]

after talking to some guys on IRC, and doing a bit of fiddling, I now have it working. Thanks anyway

Last edited by s0l1dsnak3123; 03-27-2008 at 04:44 PM. Reason: Fixed my own problem
 
Old 03-27-2008, 06:28 PM   #6
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by s0l1dsnak3123 View Post
after talking to some guys on IRC, and doing a bit of fiddling, I now have it working. Thanks anyway
You should post the solution you found since it can be helpful to others.
 
Old 03-27-2008, 09:05 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by s0l1dsnak3123 View Post
after talking to some guys on IRC, and doing a bit of fiddling, I now have it working. Thanks anyway
Did it look anything like the following?
Code:
#!/usr/bin/perl -w
use strict;

my $out = 
"


	Pictures
	Sounds
	Themes
	Videos
	Other

";

$out =~ s/^\n+|\n+$|\t//g;
$out =~ s/\n/,/g;

print "$out\n";
 
Old 03-28-2008, 11:37 AM   #8
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by angrybanana View Post
You should post the solution you found since it can be helpful to others.
here is what I did:

Code:
$out =~ s/^\s+//g; #strip all spaces at the start
$out =~ s/\s+$//g; #strip all spaces at the end
$out =~ s/\s+/,/g; #substitute the rest of the spaces with commas
Quote:
Did it look anything like the following?
Code:

Code:
#!/usr/bin/perl -w
use strict;

my $out = 
"


	Pictures
	Sounds
	Themes
	Videos
	Other

";

$out =~ s/^\n+|\n+$|\t//g;
$out =~ s/\n/,/g;

print "$out\n";
practically :P (just you did it in a better way)
 
Old 03-28-2008, 01:24 PM   #9
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
Separate regexes are usually faster, FWIW
 
  


Reply

Tags
command, line, perl, process


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
change background via command line verbose Linux - General 10 07-19-2012 10:31 AM
expect_out(buffer) is not capturing all output from a Cisco IOS command eentonig Programming 1 01-30-2008 07:29 AM
Changing KDE background from command line goodyboy Linux - Software 1 02-06-2007 04:11 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM
lost taskbar, and apps running in background spooge Linux - General 5 04-12-2002 06:03 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration