如何使用在Perl命令调用shell变量在bash shell脚本? [英] How to use shell variables in perl command call in a bash shell script?

查看:558
本文介绍了如何使用在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" )

这篇关于如何使用在Perl命令调用shell变量在bash shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆