LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   running shell command in Perl script (https://www.linuxquestions.org/questions/linux-newbie-8/running-shell-command-in-perl-script-787339/)

deci007 02-05-2010 10:26 PM

running shell command in Perl script
 
Does not work.
Code:

#!/usr/bin/perl
$etcdir = 'ls -l /etc';
print $etcdir;
#END
------------result--------
#perl -w abc123.pl
ls -l /etc[root@localhost perl]
#

This method works.
Code:

#!/usr/bin/perl
$etcdir = system("ls -l /etc");
print $etcdir;
#END


how do i run commands directly without using system or exec ?

perl version is 5.8.8 built for i386-linux-thread-multi

bmxcess 02-05-2010 10:35 PM

You can open a pipe:

Code:

open( PIPE, "ls -l /etc|" )  or die "Couldn't read pipe: $!";
while ( <PIPE> ) { 
    printf "output:",$_;
}
close PIPE;


nodopro 02-06-2010 02:31 AM

How about this:
Code:

#!/usr/bin/perl
my $etcdir = `ls -l /etc`;
print $etcdir;
#END


ricstirato 02-06-2010 07:36 AM

Quote:

Originally Posted by nodopro (Post 3854548)
How about this:
Code:

my $etcdir = `ls -l /etc`;

I think that was the point in the original post - he used normal single ticks, but you must use backticks.

deci007 02-06-2010 11:10 PM

nope
 
this doesn't work either;

Code:

#!/usr/bin/perl
use strict;
my $etcdir = `ls -l /etc`;
print $etcdir;
#END


chrism01 02-07-2010 06:12 PM

The point about backticks (`) not single quotes (') is correct.
When you say it doesn't work, show how you ran it and what you got.
Incidentally, I'd change the code
Code:

#!/usr/bin/perl -w
use strict;
my $etcdir = `ls -l /etc`;
print "$etcdir\n";



All times are GMT -5. The time now is 04:58 PM.