LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-21-2007, 09:19 AM   #1
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Rep: Reputation: 15
bash under perl


Hey,

I've just started working with Perl so I'm not very proficient with it. The thing is I want to use Perl to manipulate the output of some bash commands, such as:

Code:
ping -c 5 -i 0.2 -s 1024 IP.IP.IP.IP | grep received | awk '{print $4}'
From the reading I've done I managed to understand the difference between "" and '' in perl, but i can't figure out how to assign the output of bash commands to Perl variables in Perl scripts.

Thanks
 
Old 11-21-2007, 10:45 AM   #2
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
You will want to use the backtick (`) instead of the single quote. You can assign the output to a variable as you would a regular string.

$variable = `command`;
 
Old 11-21-2007, 11:04 AM   #3
mangoo
LQ Newbie
 
Registered: Aug 2005
Posts: 8

Rep: Reputation: 0
And instead of using two commands (grep + awk), you could use one:

ping ... | awk '/received/{print $4}'
 
Old 11-21-2007, 12:17 PM   #4
Braynid
Member
 
Registered: May 2006
Location: Romania
Distribution: CentOS
Posts: 140

Original Poster
Rep: Reputation: 15
Thanks cramer, it worked like a charm.
And thanks mangoo, as I was saying I'm still learning, my scripts are not that efficient right now, but they work!
 
Old 11-22-2007, 04:36 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
A more controlled way is
to open a command as an input stream,
like so:
Code:
#!/usr/bin/perl

open LS, "2>&1 /usr/bin/ls -l |";

while (<LS>) {

    print;
    $size += (split " ")[4];
}
print "totals = $size\n";
 
Old 11-22-2007, 05:20 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
C'mon billy, we're trying to ease a new convert in here mate ...

Slowly, slowly at first ...
 
Old 11-22-2007, 05:46 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
perl -e 'print pack "H*", "736f727279210a"'
 
Old 11-22-2007, 02:03 PM   #8
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
perl can do most things that grep/sed/awk do. So you'll usually use perl to extract/modify strings.

ex.
Code:
$variable = `ping -c 5 -i 0.2 -s 1024 127.0.0.1`;
$rec = $1 if $variable =~ /(\d+) received/;
 
Old 11-23-2007, 03:16 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
also there is in CPAN
Net::Ping

though I have never used it,
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
bash perl elvis Linux - General 9 04-22-2005 05:41 AM
bash vs perl Xris718 Linux - General 9 04-08-2005 03:19 PM
Bash or Perl? philipina Linux - General 3 07-27-2004 06:52 AM
perl/bash sk8guitar Programming 5 07-14-2003 01:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 AM.

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