Operating System - HP-UX
1752493 Members
5145 Online
108788 Solutions
New Discussion юеВ

Re: Using a perl command within a sh script

 
SOLVED
Go to solution
Belinda Dermody
Super Advisor

Using a perl command within a sh script

I would like to use the following perl command within a sh script is it possible.

$process_date = $date->prev;
7 REPLIES 7
Mark Greene_1
Honored Contributor

Re: Using a perl command within a sh script

perl -e [commands]

will run the commands give from then command line or from within a shell script.

mark
the future will be a lot like now, only later
A. Clay Stephenson
Acclaimed Contributor

Re: Using a perl command within a sh script

If I understand your question then
PROCESS_DATE=$(perl -e 'print scalar localtime(time() - 86400)')
echo "Process Date = ${PROCESS_DATE)"

Plan B.

PROCESS_DATE=$(caljd.sh $(caljd.sh -p 1))
echo "Process Date = ${PROCESS_DATE)"

Search Forums for caljd.sh
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Using a perl command within a sh script

Ooops, that should be:
echo "Process Date = ${PROCESS_DATE}"
rather than
echo "Process Date = ${PROCESS_DATE)"

If it ain't broke, I can fix that.
Belinda Dermody
Super Advisor

Re: Using a perl command within a sh script

Thanks Clay, I downloaded your script and once I figure it out I will add it to my program. I need the date format in yyyymmdd, the process runs at 4AM in the morning and copies a few hundred files with a datestamp of the previous day to different machines.
Ralph Grothe
Honored Contributor

Re: Using a perl command within a sh script

The easiest way to do your date manipulation would be to use the solution posted by ACS, calling localtime() in scalar context.
Then you have to stick with common date format (same as date prints).
Otherwise you need to do (very little) postprocessing of the list localtime() returns in list context
(see "perldoc -f localtime")

However, your intended Perl command looks to me as if you were using some Date manipulation module (like Date::Manip or Date::Calc, or whatever) because you seem to invoke a method call on an object reference.
For this OO style to work you would need to load the module (if it's not core Perl, what I doubt), and get an object reference first.

Since I don't know what module you are using I'd use this hypothetical module Date::Magic

e.g.

PROCESS_DATE=$(perl -MDate::Magic -e 'print Date::Magic->new('now')->prev')

Of course you have to check what module you were using in your script, what the name of the constructor is, and what parameters it takes for object initialization.
grep your script for lines that have
lines with 'use' or 'require'.

So I'd think you better stick with localtime()
Madness, thy name is system administration
H.Merijn Brand (procura
Honored Contributor

Re: Using a perl command within a sh script

That module already exists, and is part of my distributions:

lt09:/home/merijn 105 > perl -MDate::Manip -le'print ParseDate("yesterday")'
2004111117:21:05
lt09:/home/merijn 106 > perl -MDate::Manip -le'print ParseDate("now")'
2004111217:21:19
lt09:/home/merijn 107 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Using a perl command within a sh script

Okay, here's the way you do that.

#!/usr/bin/sh

PROCESS_DATE=$(perl -e '($mday,$mon,$year) = (localtime(time() - 86400)) [3,4,5]; printf("%04d%02d%02d",$year + 1900,$mon + 1,$mday)')
echo "Process date = ${PROCESS_DATE}"

If I got all that typed in correctly; it should work. Note that you must add 1900 to the year and 1 to the months as localtime() returns months in the range 0-11 (as does its C counterpart); simularly years are years since 1900 CE.

Plan B:
#!/usr/bin/sh
PROCESS_DATE=$(caljd.sh -y -s $(caljd.sh -p 1))
echo "Process date = ${PROCESS_DATE}"

Invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.