Passing variable from bash to perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing variable from bash to perl script
# 1  
Old 02-02-2016
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 :
Code:
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:

Code:
echo -n "Enter the Country Code - "
read COUNTRYCD
 
echo -n "Enter Start date in YYYY-MM-DD format - "
read STARTDATE[/FONT][/SIZE]
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 :
Code:
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'}')";


Last edited by Don Cragun; 02-03-2016 at 06:38 AM.. Reason: Remove SIZE and FONT tags; add CODE tags.
# 2  
Old 02-02-2016
You must run the perl script from the bash script, then it inherits the environment variables.

Last edited by MadeInGermany; 02-03-2016 at 09:30 AM.. Reason: typo
# 3  
Old 02-02-2016
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..
# 4  
Old 02-02-2016
Perl can access environment variables through the %ENV hash
for example try the following
Code:
perl -e 'print "$ENV{USER}\n"'

# 5  
Old 02-02-2016
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.

Code:
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:

Code:
#!/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')";


Last edited by Aia; 02-03-2016 at 11:24 PM.. Reason: correct wrong word
This User Gave Thanks to Aia For This Post:
# 6  
Old 02-03-2016
Quote:
Originally Posted by NileshJ
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..
Did you really do this?
Code:
...
export STARTDATE
export ENDDATE 
export VENDNUM
# now start perl from here, so it gets the just defined environment variables
perl ...

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 02-03-2016
Hi Thanks for the help it workedn now:-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Passing variable from PHP to bash script

I am totally new to PHP and I am trying to create a script that will as a user for a hostname and then use the "hostname" variable to generate a report using REST API. I am able to create the html script and php script to GET the "hostname" but I am having trouble passing the hostname variable... (10 Replies)
Discussion started by: kieranfoley
10 Replies

2. Shell Programming and Scripting

How to change Linux Terminal environment variable in a perl or bash script?

Hi, I meet an problem that it cannot change Terminal environment variable in a perl or bash script. This change can only exist and become effective in script lifetime. But I want to make this change take effect in current opened Terminal. In our view, the thought seems to be impossible, As... (9 Replies)
Discussion started by: weichanghe2000
9 Replies

3. Shell Programming and Scripting

Passing string as variable(s) in bash

I'm trying to write a basic bash script that takes input you give (what directory, if any, what name, if any ....) and passes the information to find. I'm trying to just create a string with all variables and then pass it to find. So far I have this extremely simple: #!/bin/bash -f ... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

4. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

5. Shell Programming and Scripting

Passing perl variable to shell command

Can we pass perl variable to shell commands. If yes, please give some example. (2 Replies)
Discussion started by: Anjan1
2 Replies

6. Shell Programming and Scripting

Passing awk variable in perl -e call

Hi. I am on a Solaris box and have an awk script which calls perl via the command line: timeTester="'"`perl -e 'use Time::Local;my $time = timelocal(10,10,10,10,10,2011 );print $time'`"'" But I want to pass awk variables into this call. These are the example awk variables: secondField = 10... (0 Replies)
Discussion started by: pedro6994
0 Replies

7. Shell Programming and Scripting

PERL script -- calling 'sed' by passing 'variable value'.

Hi Friends, I'm calling 'sed' command inside one perl script, which is to list directory names which are having some date value as their names (in the form YYYYMMDD) with in the range (start and end date). #!/usr/bin/perl -w use strict; use warnings; my $DATA = "/export/home/ganapa"; my... (5 Replies)
Discussion started by: ganapati
5 Replies

8. Shell Programming and Scripting

Passing Bash variable to javascript

How do I pass a bash variable to a javascript? I've tried #!/bin/bash echo "Content-type: text/html" echo "" echo "<html>" echo "<head>" counter=0 echo '<script> window.parent.document.forms.counter.value = "$counter"; </script>' I have an iframe script which I am trying to pass a... (3 Replies)
Discussion started by: numele
3 Replies

9. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

10. Shell Programming and Scripting

Passing variable to perl

I need a non-perl (bash) way to strip the path from a list of "find" results. Below is the perl version which I could use, if I could figure out how to call the script with a variable (like in sh, $1 is the variable passed in ./script variable) $file = "/path/to/file.txt"; # How do I... (2 Replies)
Discussion started by: TheCrunge
2 Replies
Login or Register to Ask a Question