perl - 如何在 PERL 中使用 Getopt::Long 删除特定目录

标签 perl getopt

我正在尝试使用 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")};

错误日志-

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)

谢谢!

最佳答案

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 允许缩短参数,只要它们保持明确。 (但我不会——我宁愿让自己输入那个字符串,并在这个过程中重新思考即将发生的事情。)

关于perl - 如何在 PERL 中使用 Getopt::Long 删除特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66442670/

相关文章:

我可以使用 2 种语言制作 1 个程序吗?

arrays - Perl:从子程序返回哈希

linux - 在 bash 脚本中解析参数/选项/标志

java - 将参数从 Java 传递到 Python 时出错

perl - 为什么我的 -p one-liner 打印出我想跳过的行?

perl - 如何使用 Perl 获取 DOS 工具的命令行输出?

c++ - `getopt` 的段错误

c - getopt 不适用于我的参数之一

c - C 中使用 getopt() 进行变长参数解析

linux - find.pm 错误 "encountered a second time"