running egrep in perl script ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting running egrep in perl script ?
# 1  
Old 09-24-2009
running egrep in perl script ?

Hi there

if i run this from the BASH command line, i get a good result

Code:
# FS="my-box23/account"
# zfs list -t filesystem -H | cut -f1 |egrep "^ZP[01]pool1/$FS$"
ZP0pool1/my-box23/account

which is great, however if I try to run in a perl script populating an array with the result/s, i get problems

Code:
#!/bin/perl -w
my $FS="my-box23/account";
my @zfs_get = `zfs list -t filesystem -H | cut -f1 | egrep "^ZP[01]pool1/$FS$"`;
print "zfs get equals $zfs_get[0]\n";


# ./test.pl 
Use of uninitialized value in concatenation (.) or string at ./test.pl line 4.
zfs get equals
#


It seems as though it is having problems with my egrep/regex and quotes because if i run a similar statement using standard grep within a perl script, it returns just fine



Code:
#!/bin/perl -w
my $FS="my-box23/account";
my @zfs_get = `zfs list -t filesystem -H | grep $FS | cut -f1 `;
print "zfs get equals $zfs_get[0]\n";


# ./test.pl 
Use of uninitialized value in concatenation (.) or string at ./test.pl line 4.
zfs get equals ZP0pool1/my-box23/account
#

Ive tried running this command within perl's system() function as well to no avail

Is there something i can do to stop perl trying to interpret the egrep/regex quotes and just run the command as it is? i take it `` and system() are finicky about what is put inside them ?

any help would be great

Last edited by rethink; 09-24-2009 at 07:25 AM..
# 2  
Old 09-24-2009
How about
Code:
my @zfs_get = grep { |^ZP[01]pool1/$FS$| } map { my @a=split /\s/; $a[0] } `zfs list -t filesystem -H`;

Why have the system start processes, when all the functionality needed is already there? Smilie
# 3  
Old 09-24-2009
thanks pludi but i seem to be having some problems with that.. maybe its to do with my perl version (which is 5.8.4) ??


Code:
#!/bin/perl -w
my $FS = "my-box23/account";
my @zfs_get = grep { |^ZP[01]pool1/$FS$| } map { my @a=split /\s/; $a[0] } `zfs list -t filesystem -H`;
print "zfs get equals $zfs_get[0]\n";


Code:
# ./test2.pl
Bareword found where operator expected at ./test2.pl line 3, near "]pool1"
        (Missing operator before pool1?)
Scalar found where operator expected at ./test2.pl line 3, near "$FS$|"
        (Missing operator before $|?)
syntax error at ./test2.pl line 3, near "{ |"
Execution of ./test2.pl aborted due to compilation errors.

# 4  
Old 09-24-2009
Sorry, must have mixed up something. Replace
Code:
grep { |^ZP[01]pool1/$FS$| }

with
Code:
grep { /^ZP[01]pool1\/$FS$/ }

# 5  
Old 09-24-2009
that works great, thanks pludi

now to read up on map() and figure out exactly whats going on with that command of yours Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script running multiple commands at once

Hi All, I have put a perl script together to go and collect some information from multiple nodes/endpoints. The script works absolutly fine however I want to make it quicker. You will see in the below that my script calls an expect script called ssh_run_cmd2.exp followed by the IP of... (7 Replies)
Discussion started by: mutley2202
7 Replies

2. Windows & DOS: Issues & Discussions

PERL: Running script from Notepad++

I'm not sure if this forum covers PERL issues but here I go: I'm trying to run a PERL script from Notepadd++. I've entered the path of the script from the run command but it is only opening a blank window. Is there anything else that needs to be done when using the 'RUN' feature of Notepad++? (2 Replies)
Discussion started by: millsy5
2 Replies

3. Shell Programming and Scripting

Running one Perl script from another

Hi, I have one small question - what is the best way to run one perl script from another? Thanks in advance. (1 Reply)
Discussion started by: xqwzts
1 Replies

4. UNIX for Advanced & Expert Users

What script is running by Perl?

Dear fellow unixoids! I need a little help (just a link would be fine) how can I investigate what specific perl script is eating 100% of cpu of my ubuntu server: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND ... (2 Replies)
Discussion started by: ulrith
2 Replies

5. Shell Programming and Scripting

Running unix commands in a perl script

Executing two unix commads via perl script one after another e.g: make clean bsub -i -q short make have tried using exec but the second command doesnt executes (1 Reply)
Discussion started by: rajroshan
1 Replies

6. Shell Programming and Scripting

running shell command in Perl script

Does not work. #!/usr/bin/perl $etcdir = 'ls -l /etc'; print $etcdir; #END ------------result-------- #perl -w abc123.pl ls -l /etc # This method works. #!/usr/bin/perl $etcdir = system("ls -l /etc"); print $etcdir; #END (2 Replies)
Discussion started by: dplinux
2 Replies

7. Shell Programming and Scripting

running perl script problem

While executing perl scriptit gives some compling issue, please help out $inputFilename="c:\allways.pl"; open (FILEH,$inputFilename) or die "Could not open log file"; Error : Could not open log file at c:\allways.pl line 4 learner in Perl (1 Reply)
Discussion started by: allways4u21
1 Replies

8. Shell Programming and Scripting

Problems in running a Perl script via cronjob

I have a very basic perl script that attempts to find if a process is running. If the process is not running then the script is supposed to start the process. If I execute the script from command line it works fine as expected. However if the script is executed via cronjob, the script cannot find... (1 Reply)
Discussion started by: ypant
1 Replies

9. UNIX for Dummies Questions & Answers

running a Perl script on HPUX platform

Hi, I wish to execute a simple perl script to pass unix commands on a HPUX platform, retrieve the result and filter through the text to determine outcomes x,y and z. I am developing the code on my windows system. I initially wrote the code to issue UNIX commands line by line, however i soon... (1 Reply)
Discussion started by: mmetcalfe
1 Replies

10. Shell Programming and Scripting

Running a remote Server through perl script

Hello people, I am want to run a server on remote machine through perl scripting using telnet api. Now when I try to do so, the server gets started perfectly, but as soon as I close the telnet connection in the script, the server started on the remote machine suddenly goes down. I also... (0 Replies)
Discussion started by: chandrak
0 Replies
Login or Register to Ask a Question