且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 bash shell 脚本的 perl 命令调用中使用 shell 变量?

更新时间:2023-09-01 15:32:10

与将值传递给任何其他程序的方式相同:将其作为 arg 传递.(您可能想生成 Perl 代码,但这是个坏主意.)

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" )

请注意,代码并不总是返回昨天的日期(因为并非所有日子都有 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" )