2014-04-24 251 views
3

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

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; 
          } 
          } 

我脚本的文件名是“extract.pl”,所以当我运行这个脚本时。我得到这个错误

./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

感谢您的编辑.. :) – user3534255

回答

2

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

最后,二)经与perl

perl extract.pl 
-1

调用脚本一个家当北京时间失踪,告诉其解释应该由你的脚本中使用。

请加:

#/斌/ perl的

在第一线。

1

使用

#!/usr/bin/perl 

或在Perl是安装

或者执行的脚本

perl extract.pl 

你的脚本是被解释内部的任何路径请给在第一线的Perl解释器的default shell我相信是bash

,所以它不能识别use,因为在bash或shell中没有这样的关键字/命令。 因此错误

use command not found 
+0

我试着加入认领,但仍有错误。剩下的错误是./extract.pl:第5行:'使用File :: Spec :: Functions qw(catdir);'。 – user3534255

+0

@ user3534255尝试之前和之后放置空格() – PradyJord

+0

我遵循你所说的,但没有任何更改.. – user3534255