Perl 脚本不喜欢日期扩展

Perl Script Not Liking Date Extension

为什么我会收到抱怨括号的错误?

sh: 第 1 行的语法错误:`)' 意外

将此日期扩展添加到新文件时 -- mv abc abc$(date +%Y%m%d%H%M%S) 因为它似乎不喜欢最后一个括号

#!/usr/bin/perl

# =========================================== #
# Script to watch POEDIACK file size
#
#                - Comments -
#
# script will check the file size of the POEDIACK file in
# $LAWDIR/$PLINE/edi/in.
# If it's > 1 gig, it will send notification via email
#
#
# =========================================== #

use strict;

use POSIX qw(strftime);

# get env vars from system
my $LAWDIR = @ENV{'LAWDIR'};
my $PLINE = @ENV{'PLINE'};
#my $email_file = "/lsf10/monitors/poediack.email";

my $curr_date = strftime('%m%d%Y', localtime);

my $ack_file = "$LAWDIR" . "/$PLINE" . "/edi/in/POEDIACK";
my $ack_location = "$LAWDIR" . "/$PLINE" . "/edi/in/";
my $mv_location = "$LAWDIR" . "/$PLINE" . "/edi/in/Z_files";

my $ack_file_limit = 10;
#my $ack_file_limit = 1000000000;
my $ack_file_size;

if( -e $ack_file)
{
        $ack_file_size = -s $ack_file;
        if ( $ack_file_size > $ack_file_limit )
        {
                `compress -vf $ack_file`;
                `mv $mv_location$ack_file.Z $mv_location$ack_file.Z.$(date +%Y%m%d%H%M%S)`;
        }
}
else
{
        print "POEDIACK File not found: $ack_file\n";
}

### end perl script ###

$( 被解释为一个变量。是the group ID of the process。你需要逃避它。

而且你可能不应该逃脱$ack_file

  `mv $mv_location$ack_file.Z $mv_location$ack_file.Z.$(date +%Y%m%d%H%M%S)`;

避免使用复杂的 shell 命令并使用 rename 更安全、更快速。

use autodie;

my $timestamp = strftime('%Y%m%d%H%M%S', localtime);
rename "$mv_location$ack_file.Z", "$mv_location$ack_file.Z.$timestamp";

或使用 existing log rotator.