如何在 bash shell 脚本的 perl 命令调用中使用 shell 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13093709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use shell variables in perl command call in a bash shell script?
提问by user836087
How to use shell variables in perl command call in a bash shell script?
如何在 bash shell 脚本的 perl 命令调用中使用 shell 变量?
I have a perl command in my shell script to evaluate date -1.
我的 shell 脚本中有一个 perl 命令来评估日期 -1。
How can i use $myDate
in perl command call?
我如何$myDate
在 perl 命令调用中使用?
This is the section in my script:
这是我脚本中的部分:
myDate='10/10/2012'
Dt=$(perl -e 'use POSIX;print strftime '%m/%d/%y', localtime time-86400;")
I want use $myDate
in place of %m/%d/%y
.
我想$myDate
代替%m/%d/%y
.
Any help will be appreciated.
任何帮助将不胜感激。
Thank you.
谢谢你。
采纳答案by ikegami
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.)
与将值传递给任何其他程序的方式相同:将其作为 arg 传递。(您可能想生成 Perl 代码,但这是个坏主意。)
Dt=$( perl -MPOSIX -e'print strftime $ARGV[0], localtime time-86400;' -- "$myDate" )
Note that code doesn't always return yesterday's date (since not all days have 86400 seconds). For that, you'd want
请注意,代码并不总是返回昨天的日期(因为并非所有日子都有 86400 秒)。为此,你会想要
Dt=$( perl -MPOSIX -e'my @d = localtime time-86400; --$d[4]; print strftime $ARGV[0], @d;' -- "$myDate" )
or
或者
Dt=$( perl -MDateTime -e'print DateTime->today(time_zone => "local")->subtract(days => 1)->strftime($ARGV[0]);' -- "$myDate" )
or simply
或者干脆
Dt=$( date --date='1 day ago' +"$myDate" )
回答by mob
Variables from the shell are available in Perl's %ENV
hash. With bash
(and some other shells) you need to take the extra step of "exporting" your shell variable so it is visible to subprocesses.
来自 shell 的变量在 Perl 的%ENV
散列中可用。对于bash
(和其他一些 shell),您需要采取额外的步骤“导出”您的 shell 变量,以便它对子进程可见。
mydate=10/10/2012
export mydate
perl -e 'print "my date is $ENV{mydate}\n"'
回答by One Face
Using "
instead of '
also passes shell variables to perl in version 5.24.
在 5.24 版中使用"
而不是'
将 shell 变量传递给 perl。
mydate=22/6/2016
perl -e "print $mydate"
回答by daemon12
Why not something like:
$ENV{'PATH'} = $ENV{'PATH'}.":"."/additional/path";
为什么不是这样的:
$ENV{'PATH'} = $ENV{'PATH'}.":"."/additional/path";
回答by ikegami
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.)
与将值传递给任何其他程序的方式相同:将其作为 arg 传递。(您可能想生成 Perl 代码,但这是个坏主意。)
Dt=$( perl -MPOSIX -e'my($m,$d,$y) = split qr{/}, $ARGV[0]; --$d; print strftime "%m/%d/%y", 0,0,0, $d,$m-1,$y-1900;' -- "$myDate" )
回答by Jason
For me, I need to use $ENV{'VARIABLE'}
to pass VARIABLE
in shell to Perl. I do not know why simply $ENV{VARIABLE}
did not work.
对我来说,我需要使用$ENV{'VARIABLE'}
将VARIABLE
shell传递给 Perl。我不知道为什么根本$ENV{VARIABLE}
没有工作。
For example,
例如,
In Bash:
在 Bash 中:
#!/bin/bash -l
VARIABLE=1
export VARIABLE
perl example.pl
In Perl:
在 Perl 中:
#!/usr/bin/perl -w
print "my number is " . $ENV{'VARIABLE'}