LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: Running Command line apps in background and capturing output (https://www.linuxquestions.org/questions/programming-9/perl-running-command-line-apps-in-background-and-capturing-output-630582/)

s0l1dsnak3123 03-25-2008 03:15 PM

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"/;


prad77 03-25-2008 04:13 PM

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

exscape 03-25-2008 04:38 PM

Use backquotes.
my $output = `ls -l`;

chrism01 03-25-2008 08:28 PM

Or use a piped open or IPC::Open2: http://perldoc.perl.org/functions/open.html

s0l1dsnak3123 03-27-2008 03:23 PM

OK, thanks alot its working now :D

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 :cry:

[edit]

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

angrybanana 03-27-2008 06:28 PM

Quote:

Originally Posted by s0l1dsnak3123 (Post 3102347)
after talking to some guys on IRC, and doing a bit of fiddling, I now have it working. Thanks anyway :D

You should post the solution you found since it can be helpful to others.

osor 03-27-2008 09:05 PM

Quote:

Originally Posted by s0l1dsnak3123 (Post 3102347)
after talking to some guys on IRC, and doing a bit of fiddling, I now have it working. Thanks anyway :D

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";


s0l1dsnak3123 03-28-2008 11:37 AM

Quote:

Originally Posted by angrybanana (Post 3102511)
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)

exscape 03-28-2008 01:24 PM

Separate regexes are usually faster, FWIW :)


All times are GMT -5. The time now is 03:47 PM.