CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
shell command in perl script.

 



bgokaraju
New User

Mar 17, 2009, 2:38 PM

Post #1 of 3 (835 views)
shell command in perl script. Can't Post

I'm new to perl script and i want to execute the shell command 'hdp' from /usr/local/hdf/bin. could anybosy please help me resolve this.



#!/usr/bin/perl

@list ='ls *.hdf';

foreach (@list) {
chop;
/^S(\S*)\.(\S*)\.x.hdf$/;

$/usr/local/hdf/bin hdp dumpsds -n chlor_a -o "gri/general/balu/test/S$1.$2.chl_a.bin" -b "gri/general/balu/test/S$1.$2.x.hdf" || die "can't convert S$1.$2 !\n";
}
;#_


1arryb
User

Mar 20, 2009, 8:53 AM

Post #2 of 3 (824 views)
Re: [bgokaraju] shell command in perl script. [In reply to] Can't Post

Hi bgokarju,

Use backticks (``) or the system command to run a shell command. Use the glob command to get a list of files:

Code
#!/usr/bin/perl 

use strict;
use warnings;

# foreach would work here, also.
for my $file (glob("*.hdf")) {
# chop() not necessary when using glob().
$file =~ /^S(\S*)\.(\S*)\.x.hdf$/;

# You should check to see if the regex worked.
die "Can't parse $file" unless ($1 and $2);

# System commands go through an extra interpolation pass
# (once for perl, and another time for the shell), so you need
# to escape the quotes inside your command string

# BTW, '/usr/local/hdf/bin<space>hdp<space>dumpsds' looks fishy to me.
# You might want to try this command from a shell before coding it into your perl script.
`/usr/local/hdf/bin hdp dumpsds -n chlor_a -o \"gri/general/balu/test/S$1.$2.chl_a.bin\" -b \"gri/general/balu/test/S$1.$2.x.hdf\"`;

# Always check the return value of system commands.
if ( $? != 0 ) {
# Newline not necessary with die().
die "can't convert S$1.$2 !";
}
}


Cheers,

Larry


(This post was edited by 1arryb on Mar 20, 2009, 11:23 AM)


FishMonger
Veteran / Moderator

Mar 20, 2009, 12:32 PM

Post #3 of 3 (808 views)
Re: [1arryb] shell command in perl script. [In reply to] Can't Post

perldoc -q "What's wrong with using backticks in a void context"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives