如何将 Shell 参数传递给 Perl 脚本? [英] How do I pass Shell parameters to Perl script?

查看:74
本文介绍了如何将 Shell 参数传递给 Perl 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以某种格式查找日期,我之前用 perl(strftime) 做过,但那次我提到了静态时间,这次我需要一个变量 $CURRENT_DAY在这里.

下面是问题,当使用 DAYHOUR=86400 和整数将 CURRENT_DAY 等同时,变量给出了正确的时间,但是一旦我把 $CURRENT_DAY 变量在语句中,日期不会减少.

$ DAYHOUR=86400$天=1$ CURRENT_DAY=`回声 $(($DAYHOUR*$DAY))`$ DD=`perl -e '使用POSIX;打印 strftime "%d", 当地时间 - $CURRENT_DAY;'`$回声$DD20$ DAY=`回声$(($DAY+1))`$ CURRENT_DAY=`回声 $(($DAYHOUR*$DAY))`$ DD=`perl -e '使用POSIX;打印 strftime "%d", 当地时间 - $CURRENT_DAY;'`$回声$DD20$ DAY=`回声$(($DAY+1))`$回声$DAY3$ CURRENT_DAY=`回声 $(($DAYHOUR*$DAY))`$回声 $CURRENT_DAY259200$ echo `perl -e '使用POSIX;打印 strftime "%d",本地时间 - 259200;'`17

解决方案

您的主要问题是 $CURRENT_DAY 是 Perl 脚本变量.通过将您的 Perl 脚本括在单引号中,shell 的同名变量是不可见的.如果您启用了警告(例如使用 -w),您就会看到这一点.

有几种方法可以规避您的问题.一种是使用双引号来封装您的 Perl,从而允许 shell 在运行 Perl 之前首先扩展其变量的值:

CURRENT_DAY=3perl -MPOSIX -wle "print strftime qq(%d),localtime time-(86400*$CURRENT_DAY);print $CURRENT_DAY"17

另一个是:

出口CURRENT_DAY=3perl -MPOSIX -wle 'print strftime qq(%d),localtime time-(86400*$ENV{CURRENT_DAY})'

请注意,从昨天或明天计算的时间中添加或减去 24 小时不会正确考虑夏令时的变化.请参阅此常见问题解答

I am trying to find dates with in a certain format, I have done it before with perl(strftime), but that time I mentioned a static time, this time i need a variable $CURRENT_DAY here.

Below is the issue, when equate the CURRENT_DAY by using DAYHOUR=86400 and an integer, the variable is giving the right time, but once I put $CURRENT_DAY variable in the statement, the date would not decrease.

$ DAYHOUR=86400
$ DAY=1
$ CURRENT_DAY=`echo $(($DAYHOUR*$DAY))`
$ DD=`perl -e 'use POSIX; print strftime "%d", localtime time - $CURRENT_DAY;'`
$ echo $DD
20
$ DAY=`echo $(($DAY+1))`
$ CURRENT_DAY=`echo $(($DAYHOUR*$DAY))`
$ DD=`perl -e 'use POSIX; print strftime "%d", localtime time - $CURRENT_DAY;'`
$ echo $DD
20
$ DAY=`echo $(($DAY+1))`
$ echo $DAY
3
$ CURRENT_DAY=`echo $(($DAYHOUR*$DAY))`
$ echo $CURRENT_DAY
259200
$ echo `perl -e 'use POSIX; print strftime "%d", localtime time - 259200;'`
17

解决方案

Your principal problem is that $CURRENT_DAY is a Perl script variable. By enclosing your Perl script in single quotes, there is no visibility to the shell's variable of the same name. Had you enabled warnings (e.g. with -w) you would have seen this.

There are a couple of ways to circumvent your problem. One is to use double quotes to encapsulate your Perl thus allowing the shell to first expand its variable's value before the Perl is run:

CURRENT_DAY=3
perl -MPOSIX -wle "print strftime qq(%d),localtime time-(86400*$CURRENT_DAY);print $CURRENT_DAY" 
17

Another is:

export CURRENT_DAY=3
perl -MPOSIX -wle 'print strftime qq(%d),localtime time-(86400*$ENV{CURRENT_DAY})' 

Be advised that adding or subtracting 24-hours from a time to calculate yesterday or tomorrow will not correctly account for daylight saving changes. See this faq

这篇关于如何将 Shell 参数传递给 Perl 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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