I am invoking a perl program from the shell script. the shell script is named as sam.sh

#!/usr/bin/sh
perl check1.pl cr_incr08292005 n n //cr_incr08292005 is the directory under a directory where sam.sh,slp.pl and check1.pl are stored.

The check1.pl is as below. The check1.pl inturn call another perl program slp.pl.

#!/usr/local/bin/perl
$runAutomatic=$ARGV[1];
$skipCaseClean=$ARGV[2];
if( -d $ARGV[0])
{
print("Directory is $ARGV[0]");
}

if ($runAutomatic eq "n"){ print "No run automatic";
}
if ($skipCaseClean eq "n") { print "No skipping bad cases";
$i=1;
}

print "calling";
system "perl slp.pl" || die "Cannot open"

But when i run the main program using the command

sh sam.sh

The output is as below.

This is from slp.pl

Directory is cr_incr08292005

No run automatic

No skipping bad cases

calling


So the problem is when ever the external perl program is invoked "the system command is called first and then rest of the statements in th program is executed". Please provide me the solution for this problem.

Recommended Answers

All 4 Replies

You need to be clear about where your output begins and ends. Your program prints out "This is from slp.pl\n\n"?

I am invoking a perl program from the shell script. the shell script is named as sam.sh

#!/usr/bin/sh
perl check1.pl cr_incr08292005 n n //cr_incr08292005 is the directory under a directory where sam.sh,slp.pl and check1.pl are stored.

The check1.pl is as below. The check1.pl inturn call another perl program slp.pl.

#!/usr/local/bin/perl
$runAutomatic=$ARGV[1];
$skipCaseClean=$ARGV[2];
if( -d $ARGV[0])
{
print("Directory is $ARGV[0]\n\n");
}

if ($runAutomatic eq "n"){ print "No run automatic\n\n";
}
if ($skipCaseClean eq "n") { print "No skipping bad cases\n\n";
$i=1;
}

print "calling\n\n";
system "perl slp.pl" || die "Cannot open"

Ths program slp.pl is as below.
#!/usr/local/bin/perl
print "This is from slp.pl\n\n";

But when i run the main program using the command

sh sam.sh

The output is as below.

This is from slp.pl

Directory is cr_incr08292005

No run automatic

No skipping bad cases

calling


So the problem is when ever the external perl program is invoked "the system command is called first and then rest of the statements in th program is executed". Please provide me the solution for this problem.

Ok, try this code fixup:

#!/usr/local/bin/perl -w

############### check1.pl ###############

$runAutomatic = $ARGV[1];
$skipCaseClean = $ARGV[2];

if ( -d $ARGV[0] ) { print "Directory is $ARGV[0] \n\n"; }
if ( $runAutomatic eq 'n' ) { print "No run automatic. \n\n"; }
if ( $skipCaseClean eq 'n' ) { print "No skipping bad cases. \n\n"; }

system("perl slp.pl") or die("Cannot open.");

Here's what I've done:
1. I cleaned up the alignment and formatting of your syntax.
2. I took out the seemingly useless $i = 1; assignment from the third "if" conditional.
3. I literalized the system() command, and threw in some parenthesis for sanity's sake.
4. Most importantly I added a semi-colon after the die() command.

I hope that fixes things up for you. :)

EDIT: Keep in mind, Perl's syntax is ugly as is, so - and this goes to everyone who writes in Perl - try and make your code as visually appealing and as un-cryptic as possible; one command/small conditional/assignment a line.

An even cleaner one:

#!/usr/local/bin/perl

use strict;
use warnings;

@ARGV == 3 or die "usage: check1.pl DIR AUTOMATIC SKIP";
my ( $dir, $run_automatic, $skip_case_clean ) = @ARGV;

-d $dir                 and print "Directory is $dir\n\n";
$run_automatic eq 'n'   and print "No run automatic.\n\n";
$skip_case_clean eq 'n' and print "No skipping bad cases.\n\n";

system( 'perl', 'slp.pl' ) == 0
    or die "System call failed: $?";

Note that system returns the exit code, which normally is 0, and hence system ... or die as in the previous fixup always dies...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.