为什么我得到IO :: Uncompress :: Unzip :: unzip:输入文件名是undef还是null字符串消息?

时间:2016-12-16 06:34:58

标签: linux perl unzip

我有一个包含许多zip文件的文件夹。我需要获取这些zip文件,解压缩并从ziped文件中获取特定文件。我正在使用IO :: Uncompress :: Unzip模块。 我的perl脚本如下:

#!usr/bin/perl
use IO::Uncompress::Unzip qw(unzip $UnzipError);
use strict;
my $propath ="/home/test/prroot/Projects/";
my $proj = "/home/Ras/projectroot/projects.txt";
my @projlist = `cat $proj`;
foreach my $pr(@projlist){
chomp($pr);
my $projname = $pr;
my $projtemp = $pr;
$projname =~ s/ /\_/g;
my $replace1 = "%28"; #escaping special characters from project name
my $replace2 = "%29";
my $replace3 = "%26";
my $replace4 = "%2C";
$projname =~ tr/ /_/;
$projname =~ s/\(/$replace1/g;
$projname =~ s/\)/$replace2/g;
$projname =~ s/\&/$replace3/g;
$projname =~ s/\,/$replace4/g;
chomp($projname);
my $dir = $propath.$projname;
chomp($dir);
my @res = glob "$dir/*.zip";
my @res1 = split '/',$res[$#res];
my $out = chdir $dir;
my $input = "$res1[$#res1]";
my $output = "/home/Ras/projectroot/xmlres/result$projname.xml";
my $status = unzip $input => $output, Name => "data";

}

文件/home/Ras/projectroot/projects.txt包含100多个项目名称。对于每个项目名称,路径/ home / test / prroot / Projects /下都有一个文件夹。在projectname文件夹中有zip文件。我需要从zip文件中读取特定文件命名数据。我能够将13个文件的输出读作/home/Ras/projectroot/xmlres/result$projname.xml。但之后没有结果。请帮帮我。

3 个答案:

答案 0 :(得分:0)

'由于我不知道您的数据是什么样的,我在这里提供了一个略微改进的脚本版本(添加错误处理)。

Compiling Cookbooks...
[2016-12-16T12:47:18+00:00] WARN: Hosts are [#<Chef::Node:0x0000000542fbf8 @chef_server_rest=nil, @name="clj-lc-tels01", @chef_environment="LC", @primary_runlist=#<Chef::RunList:0x0000000542fb08 @run_list_items=[#<Chef::RunList::RunListItem:0x0000000542d1c8 @version=nil, @type=:role, @name="base">, #<Chef::RunList::RunListItem:0x0000000542cfe8 @version=nil, @type=:role, @name="lc_elasticsearch">]>, @override_runlist=#<Chef::RunList:0x0000000542fa18 @run_list_items=[]>, @policy_name=nil, @policy_group=nil, @attributes=#<Chef::Node::Attribute @default={}, @env_default={}, @role_default={}, @force_default={}, @normal={"tags"=>["linux", "test_els"]}, @override={}, @role_override={}, @env_override={}, @force_override={}, @automatic={}, @merged_attributes=nil, @properties=nil>, @run_state={}>, ...
[2016-12-16T12:47:18+00:00] WARN: hostname is:  and has the following tags ["linux",  "test_els"]
[2016-12-16T12:47:18+00:00] WARN: hostname is:  and has the following tags ["linux", "test_els"]
[2016-12-16T12:47:18+00:00] WARN: hostname is:  and has the following tags ["linux", "elasticsearch"]
[2016-12-16T12:47:18+00:00] WARN: hostname is:  and has the following tags ["linux", "elasticsearch"]
[2016-12-16T12:47:18+00:00] WARN: hostname is:  and has the following tags ["linux", "elasticsearch"]

答案 1 :(得分:0)

首先要检查的是脚本是否正在尝试解压缩您期望的所有文件。

然后看看unzip是否在抱怨。

试试这个

   print "About to unzip '$input' to '$output'\n" ;
   my $status = unzip $input => $output, Name => "data"
       or die "unzip from '$input' to '$output' failed: $UnzipError\n";

答案 2 :(得分:0)

解压缩导致数据错误。我在我的脚本中做了一个修改,它有所帮助。

my $status = `unzip -p $input "data" > $output`

添加以上行替换unzip $ input =&gt; $ output,Name =&gt; &#34;数据&#34;帮助解决了这个问题。 无需提取zip文件,它只是读取数据内容,我能够满足要求。谢谢你的帮助