【问题标题】:How to delete specific directories using Getopt::Long in PERL如何在 PERL 中使用 Getopt::Long 删除特定目录
【发布时间】:2021-03-02 15:53:34
【问题描述】:

我正在尝试使用 Getopt::Long 删除一些目录。 我想要做的是在一个目录中运行脚本并只删除我指定的目录(和所有文件)(target_1、target_2、target_3)。 我想这样执行它-

cleanup_script.pl -target target_1,target_2,target_3

这是我到目前为止写的 -

#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;

#Logs
open(STDOUT, '>', 'results.log') or die "Can't open log";
open(STDERR, '>', 'errors.log') or die "Can't open log";

#Input
use Getopt::Long;
my $target = "target";
(my $target_1, my $target_2, my $target_3) = split ',', $target;
GetOptions ("target=s" => \$target);

#Check and delete
if ($target_1 =~ '/target_1'){
    system("rm -rf $target_1")};
if ($target_2 =~ '/target_2'){
    system("rm -rf $target_2")};
if ($target_3 =~ '/target_3'){
    system("rm -rf $target_3")};

errors.log -

Use of uninitialized value $target_1 in pattern match (m//) at
        cleanup_script.pl line 22 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.
    
    To help you figure out what was undefined, perl will try to tell you the
    name of the variable (if any) that was undefined. In some cases it cannot
    do this, so it also tells you what operation you used the undefined value
    in.  Note, however, that perl optimizes your program and the operation
    displayed in the warning may not necessarily appear literally in your
    program.  For example, "that $foo" is usually optimized into "that "
    . $foo, and the warning will refer to the concatenation (.) operator,
    even though there is no . in your program.
    
Use of uninitialized value $target_3 in pattern match (m//) at
        cleanup_script.pl line 26 (#1)

谢谢!

【问题讨论】:

  • 请永远不要说你有错误,然后就这样。剪切并粘贴确切的错误,以便我们可以看到它所说的内容。如果我们看不到错误,我们就无法判断问题所在。这就像把你的车开到修理工那里,然后说“这辆车发出了噪音”,但没有说出噪音是什么。
  • 另外,不清楚您的代码的意图是什么。你能用英语解释一下你希望你的 Perl 代码做什么吗?
  • 你说得对,安迪,编辑了所有信息

标签: perl getopt


【解决方案1】:

Getopt::Long 的明确目的是解析程序的参数并将它们读入程序和变量中。 (它可以也执行任意代码,但将其用于除参数处理需求之外的任何事情都是非常糟糕的做法。)

因此,一旦使用该库读取 dir1,dir2,... 之类的字符串,这是您选择的名称为 target 的参数的值,然后您拆分该字符串并获得所需的清单。然后程序可以继续处理(在这种情况下删除提交的目录)

use Getopt::Long;
use File::Path qw(remove_tree);   # better use this than system's 'rm'

... 

my $target;  # only need to declare

# - I strongly advise an argument with a more informative name, like
#   GetOptions ("dirs-to-remove=s" => \$target);
# - Don't want to proceed with a program that deletes stuff if there is an error!
#   So exit with a brief usage message (or call a function with a nicer message)
GetOptions ("target=s" => \$target)
    or die "Usage: $0 [arguments...]\n";  # or call a sub for usage message

# Check these directories if you have any way to do so      
my @dirs_to_remove = split /,/, $target;

# Now iterate over all those `@dirs_to_remove`
foreach my $dir_to_del (@dirs_to_remove) {
    #
    # Check as best as you can. A whole hierarchy is getting blown away
    #
    if ( -d $dir_to_del and ... ) {   # check somehow
        my $num_removed = remove_tree(
            $dir_to_del, { error => \my $err, safe => 1 }
        );
        if ($err and @$err) {
            # See module docs for how to work with $err
            die "Errors while removing $dir_to_del ..."; 
        }
        # print, check ...; see docs for options
    }
}

请参阅文档。特别是,remove_tree 可以采用一些非常有用的选项。我发现 error 选项特别好,因为我宁愿在删除条目的程序中出现任何错误时立即退出。

该库可以在多个目标上调用,所以你可以说

my $num_removed = remove_tree( @dirs_to_remove, ... );

在这种情况下,您不需要循环,只需这一条语句。然而,没有办法检查每个目录,因为它即将被删除,或者在它已经被删除之后——它们都已经消失了。

我建议始终使用完善的 Perl 库,而不是使用系统和使用外部工具,如果有库可以完成这项工作。在这种情况下,肯定有,比如File::Path


一旦这个程序被调用,一堆目录就会消失,可能没有办法恢复它们的内容。想到这我就很紧张。

那么为什么不用一些非常明显的东西来命名命令行参数,比如

program_name --dirs-to-remove dir1,dir2...

(即使程序本身的当前名称是信息丰富且具体的!)

然后,您可以更简单地调用它,甚至可以使用program_name -d dir1,dir2...,因为Getopt::Long 允许缩短参数,只要它们保持明确。 (但我不会——我宁愿自己输入那个字符串,并在此过程中重新考虑即将发生的事情。)

【讨论】:

    【解决方案2】:

    您的问题让人不清楚。 (例如,当人们认为它们应该匹配时,正则表达式模式在示例中将不匹配。)您提供了错误和损坏的代码,但不是代码试图实现的目标。

    因此,我将从程序的基本形式开始。

    use Getopt::Long;
    
    GetOptions("target=s" => \$target)
       or die("usage");
    
    my @targets = split /,/, $target // "target";
    
    for my $target (@targets) {
       system("rm", "-rf", "--", $target);
    }
    

    你可以添加验证。

    my %valid_targets = map { $_ => 1 } qw( target1 target2 target3 );
    
    die("...") if !$valid_targets{$target};
    system("rm", "-rf", "--", $target);
    

    您甚至可以将关键字映射到目录。

    my %targets = (
       target_1 => ".../foo",
       target_2 => ".../bar",
       target_3 => ".../baz",
    );
    
    my $dir = $targets{$target}
       or die("...");
    
    system("rm", "-rf", "--", $dir);
    

    【讨论】:

      【解决方案3】:

      有一点看起来很奇怪:

      (my $target_1, my $target_2, my $target_3) = split ',', $target;
      GetOptions ("target=s" => \$target);
      

      您希望 $target 成为用户从命令行输入的选项,以便您可以从中获取目标,但您在调用 GetOptions 之前执行 split 以读取-target 参数。这对我来说听起来不对。

      另外,你不应该给 $target 一个初始值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多