使用Perl脚本解压缩tar.gz文件

时间:2014-04-24 05:17:57

标签: linux perl unix

我正在做一个关于使用指定位置提取tar.gz文件的Perl脚本,但是当我运行此代码时。

  use strict;
  use warnings;

  use Archive::Tar;
  use File::Spec::Functions qw(catdir);

  my $input = "sample.tar.gz";
  my $tar   = Archive::Tar->new($input);
  my @files = $tar->list_files;

  my $output_dir = '/var';
  foreach my $file (@files) {

my $extracted_file = catdir($output_dir, $file);
$tar->extract_file($file, $extracted_file);

if ($file =~ /\.rpm\z/ /\.sh\z/ /\.gz\z/) {
    open my $fh, '<', $extracted_file or do { warn "$0: Can't open file $file: $!"; next };
    while (defined(my $line = <$fh>)) {
         do something with $line
                 }
                         close $fh;
                             }
                             }

我的脚本的文件名是&#34; extract.pl&#34;所以当我运行这个脚本。我收到了这个错误

 ./extract.pl: line 1: use: command not found

 ./extract.pl: line 2: use: command not found

 ./extract.pl: line 4: use: command not found

 ./extract.pl: line 5: syntax error near unexpected token `('

 ./extract.pl: line 5: `use File::Spec::Functions qw(catdir);'

任何人都可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

你有两种选择;一)脚本的第一行应该是(首先搜索PATH)#!/usr/bin/env perl或直接搜索perl安装的路径(我的位于#!/usr/bin/perl)。

最后,二)通过perl

调用您的脚本
perl extract.pl

答案 1 :(得分:1)

请使用

在第一行提供perl口译员
#!/usr/bin/perl

或安装perl的任何路径

或者以

执行脚本
perl extract.pl

您的脚本正在default shell内解释,我相信是bash

所以它无法识别use因为bash或任何shell中没有这样的关键字/命令。 因此错误

use command not found

答案 2 :(得分:-1)

缺少一个shebang,告诉你的脚本应该使用哪个翻译。

请添加:

#!/ bin / perl

在第一行。

相关问题