【问题标题】:How to save the output file in a directory and print the name of the output file?如何将输出文件保存在目录中并打印输出文件的名称?
【发布时间】:2013-04-22 04:53:07
【问题描述】:

我正在解析一个文件,然后从中创建多个输出文件。我想将输出文件保存在执行 perl 脚本的同一文件夹中的特殊目录中。如果该目录已经存在,则将文件放在该目录中。如果用户没有 sudo 权限,则在当前工作目录中创建文件。

use strict; use warnings;

sub main {
    my $directory = "pwd/test.pl_output";

    unless(mkdir $directory) {
        return 0; # unable to create directory (it either already exists
                      # or you don't have sudo privileges)  
    } else {
        return $directory
      }
}

if ( my $PATH = main() ){
    open(my $fh, '<', 'input.txt') or die $!;
    open(my $output, '+>', $PATH.'/output.txt') or die $!;
 } else {  # create the output file as usual
    open(my $fh, '<', 'input.txt') or die $!;
    open(my $output, '+>', 'output.txt') or die $!;
 }

 print "All done! Please look in \$output\n";

在脚本的最后,在我解析并处理完文件后,我想在 shell 提示符下打印出以下内容:

All done! Please look in 'output.txt' for the output.

如果输出文件在新目录中,我想打印以下内容:

All done! Please look in 'output.txt' in the directory '~/path/to/output.txt' for the output.

我的代码不起作用。

【问题讨论】:

  • 我不习惯 perl,但我在这里有一个问题,'$PATH/output.txt' 在这个 $PATH 中,它会被 ' 中的值替换吗?
  • abasu 这是另一个很好的收获。单引号不插入。我怀疑我们没有看到实际的复制粘贴代码。
  • 我知道create a directory in perl 是可能的。但我不知道我正在尝试做的其余部分是否可行。
  • 有可能。但是您描述的逻辑比您显示的代码更复杂。换句话说,您的条件并没有处理您提到的所有条件。而且您对'pwd'的使用不正确。只需使用 FindBin 模块,可执行文件的路径将在$FindBin::Bin

标签: perl bash unix sudo file-handling


【解决方案1】:

您的现有代码存在几个严重问题。一个是您在双引号字符串中使用pwd。那是行不通的。您可以在反引号内使用 pwd 并捕获输出,但不能在双引号文字内使用。

另一个问题是您的代码逻辑没有达到描述您希望如何优雅降级目标的复杂性。

以下代码 sn-p 将首先在可执行文件的目录中查找名为“special”的目录。如果它不存在,它将尝试创建它。如果创建失败(可能是由于权限问题),它接下来会在用户当前的工作目录中寻找一个名为“special”的目录。如果它不存在,它将尝试创建它。如果失败,它将以描述性消息结束。

如果它超过了这一点,那么“特殊”目录要么是预先存在的,要么是沿着允许的路径之一创建的。接下来,打开文件。如果打开输出文件失败,我们就死了。否则,继续并可能写入文件。然后关闭输入和输出文件。最后,打印输出文件所在的路径。

use strict;
use warnings;

use FindBin;
use File::Path qw( make_path );

my $special_dir = 'special';
my $filename    = 'my_file.txt';


my $bin = $FindBin::Bin;

my $path;


if( not defined( $path = get_path( "$bin/$special_dir", "./$special_dir" ) ) ) {
  die "Unable to find or create a suitable directory for output file.";
}

my $output_filename = "$path/$filename";


open my $in_fh, '<', 'input.txt' or die $!;
open my $out_fh, '+>', $output_filename or die $!;

# Do whatever it is you want to do with $in_fh and $out_fh....

close $out_fh or die  $!;
close $in_fh  or warn $!;

print "All done! Please look in $output_filename for the output.\n";



sub get_path {
  my @tries = @_;
  my $good_path;
  for my $try_path ( @tries ) {
    if( -e $try_path and -d _ and -w _ ) {          # Path exists. Done.
      $good_path = $try_path;
      last;
    }
    elsif( eval { make_path( $try_path ); 1; } ) {  # Try to create it.
      $good_path = $try_path;                       # Success, we're done.
      last;
    }
  }
                                                    # Failure; fall through to
                                                    # next iteration.  If no
                                                    # more options, loop ends
                                                    # with $path undefined.
  return $good_path;
}

我正在使用 Perl 模块 FindBin 来定位可执行文件。并且 File::Path 被用来创建目录。

【讨论】:

    【解决方案2】:

    过去有好几次,我发现最好将 perl 脚本与 Shell 脚本一起用于此目的。用于解析的 Perl 脚本和用于文件夹处理的 shell 脚本。并轻松地将 perl 脚本包含在 shell 脚本中。它会更容易和方便。

    【讨论】:

      猜你喜欢
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2011-12-19
      • 2014-02-11
      • 2012-09-30
      • 1970-01-01
      相关资源
      最近更新 更多