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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Mar 17, 2009, 2:38 PM

Post #1 of 3 (836 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.


@list ='ls *.hdf';

foreach (@list) {

$/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";


Mar 20, 2009, 8:53 AM

Post #2 of 3 (825 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:


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 !";



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

Veteran / Moderator

Mar 20, 2009, 12:32 PM

Post #3 of 3 (809 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