| | |
How to pass a shell variable to a perl command
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 3
Reputation:
Solved Threads: 0
I have defined a variable in bash
I would like to pass this to perl to perform a simple date conversion.
If I enter the date manually it works.
My attempts to pass the variable MYDATESTAMP into this command it returns nothing.
How can this be done?
Shell Scripting Syntax (Toggle Plain Text)
$ 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)
$ perl -MDate::Parse -le'print str2time("2009-06-25 21:57:18");' $ 1245992238
My attempts to pass the variable MYDATESTAMP into this command it returns nothing.
Shell Scripting Syntax (Toggle Plain Text)
$ perl -MDate::Parse -le'print str2time("$MYDATESTAMP");'
How can this be done?
•
•
Join Date: Jun 2009
Posts: 3
Reputation:
Solved Threads: 0
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
foo.pl (perl)
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)
#echo the datestamp to a text file. echo $MYDATESTAMP > mydatestamp.txt # Set a new variable by having perl read the text file and convert the time format MYDATESTAMP2=$(/usr/bin/perl foo.pl)
foo.pl (perl)
Shell Scripting Syntax (Toggle Plain Text)
#!/usr/bin/perl use Date::Parse 'str2time'; open (MYDATESTAMP, "mydatestamp.txt"); while ($record = <MYDATESTAMP>) { my $date = $record; my $epoch = str2time($date); #print "Epoch: $epoch\n"; #print $record; print "$epoch" } close(MYDATESTAMP);
export the variable and use $ENV:
bash Syntax (Toggle Plain Text)
sk@sk:~$ export MYDATESTAMP="2009-06-25 21:57:18" sk@sk:~$ perl -MDate::Parse -le'print str2time($ENV{"MYDATESTAMP"});' 1245981438
![]() |
Similar Threads
- how to read unix profile variable into Perl (Perl)
- NEED help to pass javascript variable to php (PHP)
- Pass a variable value into a Button value c# (ASP.NET)
- Pass a variable value into a Button value c# (C#)
- pass session variable with window.location.href jsp (JSP)
- Unix shell access from Perl script (Perl)
- How to pass the values of javascript in perl (JavaScript / DHTML / AJAX)
Other Threads in the Shell Scripting Forum
- Previous Thread: Arithmetic Evaluation and Pipes in BASH
- Next Thread: Sending email from a shell script
| Thread Tools | Search this Thread |






