LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-05-2010, 10:26 PM   #1
deci007
Member
 
Registered: Sep 2007
Posts: 44

Rep: Reputation: 15
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

Last edited by deci007; 02-05-2010 at 10:28 PM. Reason: edit
 
Old 02-05-2010, 10:35 PM   #2
bmxcess
Member
 
Registered: Jan 2009
Location: Brisbane, Australia
Distribution: @work:RHEL 5.4/Fedora 13, @home:slack64-current,ubuntu lynx studio
Posts: 65

Rep: Reputation: 19
You can open a pipe:

Code:
open( PIPE, "ls -l /etc|" )   or die "Couldn't read pipe: $!";
while ( <PIPE> ) {  
    printf "output:",$_;
}
close PIPE;
 
Old 02-06-2010, 02:31 AM   #3
nodopro
Member
 
Registered: Oct 2008
Posts: 48

Rep: Reputation: 16
How about this:
Code:
#!/usr/bin/perl
my $etcdir = `ls -l /etc`;
print $etcdir;
#END
 
Old 02-06-2010, 07:36 AM   #4
ricstirato
Member
 
Registered: Jan 2004
Location: Gießen, Germany
Distribution: Xubuntu 12.04, Mythbuntu, Ubuntu Server 12.04
Posts: 174

Rep: Reputation: 24
Quote:
Originally Posted by nodopro View Post
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.
 
Old 02-06-2010, 11:10 PM   #5
deci007
Member
 
Registered: Sep 2007
Posts: 44

Original Poster
Rep: Reputation: 15
nope

this doesn't work either;

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

Last edited by deci007; 02-06-2010 at 11:11 PM. Reason: edit
 
Old 02-07-2010, 06:12 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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";
 
  


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
running shell script: command not found tripat Red Hat 3 04-14-2010 01:21 PM
Running a shell script with Perl ziphem Linux - Server 1 06-27-2009 06:56 PM
Running shell script through perl tucs_123 Programming 8 03-19-2009 07:43 PM
Error running command via shell script lowlifeish Linux - Software 2 08-31-2007 10:38 PM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 02:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:13 PM.

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