Passing variable from bash to perl script

Hi All,
I need to pass a variable from bash script to perl script and in the perl script i am using those variables in the sql query but its giving
error :

Use of uninitialized value $ENV{"COUNTRYCD"} in concatenation (.) or string at /GIS_ROOT/custom/tables/DBread_vendor.pl line 50.

Can you please help me on thsi as i am new to perl script and tried some options from google but its not working.
bash script logic:

echo -n "Enter the Country Code - "
read COUNTRYCD
 
echo -n "Enter Start date in YYYY-MM-DD format - "
read STARTDATE
echo -n "Enter End date in YYYY-MM-DD format - "
read ENDDATE
 
echo -n "Enter the Vendor Number-"
read VENDNUM
 
export STARTDATE
export ENDDATE
export VENDNUM

Perl script logic :

my $DRSQL = "SELECT dest_batch_name FROM Document WHERE dest_batch_name LIKE '$ENV{'COUNTRYCD'}' AND LEFT(entity_ref_id,6) = '$ENV{'VENDNUM'}'[
AND (batch_create_ts BETWEEN '$ENV{'STARTDATE'}' AND '$ENV{'ENDDATE'}')";

You must run the perl script from the bash script, then it inherits the environment variables.

Hi,
yes i am running the perl script from shell script only..
after environment variables i am running the perl script.. that only its giving error..

Perl can access environment variables through the %ENV hash
for example try the following

perl -e 'print "$ENV{USER}\n"'

With the information you have posted it is not possible to give you more than a guess.
You appear to be exporting the necessary variables in your bash script, but Perl does not see those variables and you are not showing where or how you are calling the Perl script.

Adding the following to your Perl script will display the environment variables seen by the script.

use Data::Dumper;
print Dumper \%ENV;

As a suggestion, why do you not invest the time to do the work from Perl alone?

Here's something to get you started:

#!/usr/bin/perl

use strict;
use warnings;

print "Enter the Country Code - ";
my $countrycd = <stdin>;
chomp $countrycd;
#always validate user input

print "Enter Start date in YYYY-MM-DD format - ";
my $startday = <stdin>;
chomp $startday;
#always validate user input
$startday =~ /\d{4}-\d{2}-\d{2}/ || die "The format for start date is YYYY-MM-DD: $startday was entered\n";

print "Enter End date in YYYY-MM-DD format - ";
my $endday = <stdin>;
chomp $endday;
#always validate user input
$endday =~ /\d{4}-\d{2}-\d{2}/ || die "The format for end date is YYYY-MM-DD: $endday was entered\n";

print "Enter the Vendor Number -";
my $vendnum = <stdin>; # read from stdin user input
chomp $vendnum; # remove the ENTER pressed by user
#always validate user input

my $drsql = "SELECT dest_batch_name FROM Document WHERE dest_batch_name LIKE '$countrycd' " .
            "AND LEFT(entity_ref_id,6) = '$vendnum' " .
            "AND (batch_create_ts BETWEEN '$startday' AND '$endday')";
1 Like

Did you really do this?

...
export STARTDATE
export ENDDATE 
export VENDNUM
# now start perl from here, so it gets the just defined environment variables
perl ...
1 Like

Hi Thanks for the help it workedn now:-)