如何使用在Perl命令调用shell变量在bash shell脚本?

问题描述:

如何使用在Perl命令调用shell变量在bash shell脚本?

How to use shell variables in perl command call in a bash shell script?

我在shell脚本有一个Perl命令对日期-1。

I have a perl command in my shell script to evaluate date -1.

我如何使用Perl中命令调用$数值指明MyDate?

How can i use $myDate in perl command call?

这是我的脚本的部分:

myDate='10/10/2012'

Dt=$(perl -e 'use POSIX;print strftime '%m/%d/%y', localtime time-86400;")

我代替WANA使用$数值指明MyDate'%M /%D /%Y

I wana use $myDate in place of '%m/%d/%y'

任何帮助将appriciated。

Any help will be appriciated.

感谢您。

在值传递给其他任何程序用同样的方法:把它作为一个ARG。 (你也许会产生的Perl code,但是这是一个坏主意。)

The same way you pass values to any other program: Pass it as an arg. (You might be tempted to generate Perl code, but that's a bad idea.)

Dt=$( perl -MPOSIX -e'print strftime $ARGV[0], localtime time-86400;' -- "$myDate" )

请注意,code并不总是返回昨天的日期(因为不是所有的日子都86400秒)。对于这一点,你会想要

Note that code doesn't always return yesterday's date (since not all days have 86400 seconds). For that, you'd want

Dt=$( perl -MPOSIX -e'my @d = localtime time-86400; --$d[4]; print strftime $ARGV[0], @d;' -- "$myDate" )

Dt=$( perl -MDateTime -e'print DateTime->today(time_zone => "local")->subtract(days => 1)->strftime($ARGV[0]);' -- "$myDate" )

或只是

Dt=$( date --date='1 day ago' +"$myDate" )