943,625 Members | Top Members by Rank

Ad:
Jun 26th, 2009
0

How to pass a shell variable to a perl command

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanepds is offline Offline
3 posts
since Jun 2009
Jun 26th, 2009
0

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

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);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanepds is offline Offline
3 posts
since Jun 2009
Jun 26th, 2009
0

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

export the variable and use $ENV:

bash Syntax (Toggle Plain Text)
  1. sk@sk:~$ export MYDATESTAMP="2009-06-25 21:57:18"
  2. sk@sk:~$ perl -MDate::Parse -le'print str2time($ENV{"MYDATESTAMP"});'
  3. 1245981438
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 26th, 2009
0

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

Thanks! That's much better than my caveman solution.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lanepds is offline Offline
3 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Arithmetic Evaluation and Pipes in BASH
Next Thread in Shell Scripting Forum Timeline: Sending email from a shell script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC