-1

我有这个 Perl 脚本,它用于根据包版本对包进行排序:

#!/usr/bin/perl -w
#
# Compare versions of all *.rpm files against the
# latest packages installed (if installed)
#
# Usage:
# rpmver.pl
# This script looks for all *.rpm files.
#
use strict;
use RPM2;
my $rpm_db = RPM2->open_rpm_db();
for my $filename (<*.rpm>) {
    my $h = RPM2->open_package( $filename );
    # Ensure we compare against the newest
    # package of the given name.
    my ($installed) =
      sort { $b <=> $a } $rpm_db->find_by_name($h->name);
    if (not $installed) {
        printf "Package %s not installed.\n", $h->as_nvre;
    } else {
        my ($result) = ($h <=> $installed);
        if ($result < 0) {
            printf "Installed package %s newer than file %s\n",
            $installed->as_nvre,
            $h->as_nvre;
        } else {
            printf "File %s newer than installed package %s\n",
            $h->as_nvre,
            $installed->as_nvre;
        }
    }
 } 

我有一个带有 SRPM 的 Linux 存储库。我想将最新的软件包移动到不同的目录中;例如latest_packages。必须如何修改脚本?

4

1 回答 1

1

首先,将所有最新的包名称存储在名为%latest_package;的哈希中。然后在检查所有包之后遍历散列的键并移动那些最新的包。

} else {
    printf "File %s newer than installed package %s\n", ...
    $latest_package{$h->name} = $installed->as_nvre;
    ...
}

使用File::Copy模块的move()方法来移动文件。不要忘记将其视为任何其他 IO 系统调用(die如果它出错,包括$!错误文本变量的内容;或使用autodie)。

foreach my $name (sort keys %latest_package) {
    File::Copy::move($latest_package{$name}, $LATEST_PACKAGE_DIR) 
        || die "Could not move $latest_package{$name} to $LATEST_PACKAGE_DIR: $!);
}

如果需要,使用mkdir()函数创建目录。

于 2012-04-08T01:35:25.330 回答