How to pass a shell variable to a perl command

Thread Solved
Reply

Join Date: Jun 2009
Posts: 3
Reputation: lanepds is an unknown quantity at this point 
Solved Threads: 0
lanepds lanepds is offline Offline
Newbie Poster

How to pass a shell variable to a perl command

 
0
  #1
Jun 26th, 2009
I have defined a variable in bash

Shell Scripting Syntax (Toggle Plain Text)
  1. $ MYDATESTAMP="2009-06-25 21:57:18"

I would like to pass this to perl to perform a simple date conversion.

If I enter the date manually it works.
Shell Scripting Syntax (Toggle Plain Text)
  1. $ perl -MDate::Parse -le'print str2time("2009-06-25 21:57:18");'
  2. $ 1245992238

My attempts to pass the variable MYDATESTAMP into this command it returns nothing.
Shell Scripting Syntax (Toggle Plain Text)
  1. $ perl -MDate::Parse -le'print str2time("$MYDATESTAMP");'

How can this be done?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: lanepds is an unknown quantity at this point 
Solved Threads: 0
lanepds lanepds is offline Offline
Newbie Poster

Re: How to pass a shell variable to a perl command

 
0
  #2
Jun 26th, 2009
Ok, I figured out a way to do this. It's not elegant, but it managed to get the job done.

What I did was echo the $MYDATESTAMP variable from bash into a file named mydatestamp.txt and had a tiny perl script which I named "foo.pl" read that file and then perform the date format conversion.

bash
Shell Scripting Syntax (Toggle Plain Text)
  1. #echo the datestamp to a text file.
  2. echo $MYDATESTAMP > mydatestamp.txt
  3. # Set a new variable by having perl read the text file and convert the time format
  4. MYDATESTAMP2=$(/usr/bin/perl foo.pl)

foo.pl (perl)
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2. use Date::Parse 'str2time';
  3. open (MYDATESTAMP, "mydatestamp.txt");
  4. while ($record = <MYDATESTAMP>) {
  5. my $date = $record;
  6. my $epoch = str2time($date);
  7. #print "Epoch: $epoch\n";
  8. #print $record;
  9. print "$epoch"
  10. }
  11. close(MYDATESTAMP);
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,141
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 555
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: How to pass a shell variable to a perl command

 
0
  #3
Jun 26th, 2009
export the variable and use $ENV:

  1. sk@sk:~$ export MYDATESTAMP="2009-06-25 21:57:18"
  2. sk@sk:~$ perl -MDate::Parse -le'print str2time($ENV{"MYDATESTAMP"});'
  3. 1245981438
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: lanepds is an unknown quantity at this point 
Solved Threads: 0
lanepds lanepds is offline Offline
Newbie Poster

Re: How to pass a shell variable to a perl command

 
0
  #4
Jun 26th, 2009
Thanks! That's much better than my caveman solution.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC