Perl inside shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl inside shell
# 1  
Old 08-01-2013
Perl inside shell

I am trying to find out the difference between two dates, for which I am using perl inside SHELL. Below are my codes.

Code:
perl -MDate -e 'Date::Calc qw(Delta_DHMS);'

perl -e '($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)'

But I am getting the error:

Code:
[edwpapp9a:u210502] /home/u210502 $ ksh perltest
Can't locate Date.pm in @INC (@INC contains: /u001/edwcommon/ul/perl5/lib/5.8.0/aix-64all /u001/edwcommon/ul/perl5/lib/5.8.0 /u001/edwcommon/ul/perl5/lib/site_perl/5.8.0/aix-64all /u001/edwcommon/ul/perl5/lib/site_perl/5.8.0 /u001/edwcommon/ul/perl5/lib/site_perl .).
BEGIN failed--compilation aborted.
Undefined subroutine &main::Delta_DHMS called at -e line 1.


Can Someone help me.

Thanks In advance.

Last edited by Don Cragun; 08-01-2013 at 04:36 AM.. Reason: Change COLOR to CODE tags
# 2  
Old 08-01-2013
Make sure you have installed the Date::Calc Module

Code:
perl -MDate::Calc -e 'use Date::Calc qw(Delta_DHMS); ($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)';

This User Gave Thanks to MR.bean For This Post:
# 3  
Old 08-02-2013
little simpler to avoid use statement
Code:
perl -MDate::Calc=Delta_DHMS -e '($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)'

This User Gave Thanks to rajamadhavan For This Post:
# 4  
Old 08-02-2013
My program is below

Code:
year1=2011
month1=01
day1=07
hour1=06
min1=05
sec1=16
year2=2013
month2=12
day2=12
hour2=09
min2=18
sec2=23

perl -MDate::Calc=Delta_DHMS -e '($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)'
perl -e '$elapsed = "$Dh:$Dm:$Ds"'
echo $elapsed


I am getting the error below.

Date::Calc::Delta_DHMS(): not a valid date at -e line 1.
# 5  
Old 08-02-2013
This is because the perl one liner doesn't expand the shell variables $year1,$month1....$sec2 within the single quotes and consider them as undefined and therefore invalid date.
Also the second perl statement will not work since it will not be executed on the same context as the previous one and $Dh, $Dm will be undefined on this line.

You should be writing a perl script than shell script for this.
# 6  
Old 08-02-2013
So Can I interchange pass values between a perl program and shell program?
if yes how ?

Thanks
Saurav
# 7  
Old 08-02-2013
Double quotes works in some cases, but not all. You can use shell env variables in perl using -MEnv option

Code:
$ export year1=2011
$ perl -MEnv -e 'print $year1'
2011

This User Gave Thanks to rajamadhavan For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Use of sed inside perl

$string="test.binary.value"; $toggle="test.binary.value=true"; host=test.server.com my $returnCode = system ( "ssh $host 'cp -p /tmp/testfile /tmp/testfile.bkup; sed -i 's/\$string.*/\$toggle/g' /tmp/testfile'" ); cat testfile test.binary.value=false Scenario: I am trying to... (3 Replies)
Discussion started by: Tuxidow
3 Replies

3. Shell Programming and Scripting

Search inside a file, PERL

HEllo!! I need a script in perl that see inside a file(file.txt) that has only one row. The row contains only this text: logs_yearly = 20120628113345 I want that the script does this: "Search in the file, if the date 20120628 is actual date" Thank you!! ---------- Post... (4 Replies)
Discussion started by: super_sun
4 Replies

4. Shell Programming and Scripting

perl inside shell script?

Hi UNIX Pros! Newbie here, I would like to ask if it is possible to use perl commands inside a shell script? i created a simple script which connects to oracle and run queries which the script saves to a separate .txt file for each query issued. What i want to do is to create a presentable and... (7 Replies)
Discussion started by: 4dirk1
7 Replies

5. Shell Programming and Scripting

PERL: an OR inside an IF .. im stuck

Hi all, I basically have an if statement that says if you have more than 5 changes you need to have the force flag set to 1 to proceed. like this if ((($Changes > 5) && ($force == 1)) || (($nChanges > 0) && ($Changes < 6))) { I now need to allow the force flag to be either 1 or 2 to... (2 Replies)
Discussion started by: rethink
2 Replies

6. Shell Programming and Scripting

Shell script inside a perl script

How to write a shell script inside a perl script code? It is possible or not. Actually i have a perl script code and i need some values from the shell script code and vice-versa i.e. some values of shell script use by the perl scipt and some values of perl script use by the shell script. So how can... (1 Reply)
Discussion started by: anupdas
1 Replies

7. Shell Programming and Scripting

Using Perl Inside KSH

Hi there, I am new to Perl and KSH. The system I am using picks up the KSH scripts and uses them in a batch job control system. I am trying to set a variable from a perl command #!/bin/ksh -eaxp #******************************************************************************* # Testing... (5 Replies)
Discussion started by: SaadLive
5 Replies

8. Shell Programming and Scripting

Call a perl script inside a shell script

Hi all, I have the following snippet of code.. #!/bin/sh echo "run perl script............" #Run the verification script perl bill_ver echo " perl script completed....." echo "rename files......" #Remove from all file in the directories test, test1, test2, test3 for f in... (3 Replies)
Discussion started by: chriss_58
3 Replies

9. Shell Programming and Scripting

doing grep inside perl file

Hi have one Perl file inside that i am defining at an array file. @temp_vmdk_files = `grep vmdk '$guest_vmx'` where my $guest_vmx=/vmfs/volumes/47e40fec-9c8bb7f7-d076-001422159f8a/BES Exchange/BES-Exchange.vmx and i am just want to do grep of "vmdk" files from the above path but when... (5 Replies)
Discussion started by: bp_vardhaman
5 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question