脚本只读取输入文件的最后一行

时间:2014-04-22 12:30:34

标签: perl scripting

输入文件中的值(list_muni_nav.txt)。每一行都是文件的路径,这里的'test'和'test2'是文件名。

/iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test
/iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test2

我的perl脚本(如下所示)应读取输入文件中的每一行并执行它们。但是,除了最后一行之外,脚本没有找到其余的行。请查看结果

    my $content_list = '/home/p12/scripts/list_muni_nav.txt';

print "\t*****************START***************************\n";
print "\tOpening file $content_list\n";
if (open FILE,"$content_list") {
        my $line = <FILE>;
        chomp($line);
        while ($line ne "") {
                print "\t Applying EAs to file $line\n";

                my $setEAVersion= "$getAttrib -s TeamSite/Assocation/Version=1 \"$line\"";
                                 `$setEAVersion`;
                my $setEADCRType= "$getAttrib -s TeamSite/Templating/DCR/Type=muni_site/nav \"$line\"";
                                 `$setEADCRType`;
                my $setEAFormValid= "$getAttrib -s iw_form_valid=true \"$line\"";
                                 `$setEAFormValid`;

                print "\t EAs applied successfully to $line\n";
                print "\t**********************************************************\n";

                $line = <FILE>;
                chomp($line);
        }

结果:

        *****************START***************************
        Opening file /home/pbiswa01/scripts/list_muni_security.txt
         Applying EAs to file /iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test
ERROR:02005: Error locating /iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test
ERROR:02005: Error locating /iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test
ERROR:02005: Error locating /iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test
         EAs applied successfully to /iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test
        **********************************************************
         Applying EAs to file /iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test2
         EAs applied successfully to /iwmnt/default/main/Content/Munic/WORKAREA/work/templatedata/muni_site/security/data/test2
        **********************************************************

请帮忙。谢谢!!

1 个答案:

答案 0 :(得分:1)

我认为@stark是对的,你的问题在于换行符。 chomp将删除对应于特殊变量$/的任何尾随字符串,这在Windows和Linux上是不同的。您可以使用正则表达式解决此问题:

my $content_list = '/home/p12/scripts/list_muni_nav.txt';

print "\t*****************START***************************\n";
print "\tOpening file $content_list\n";
if (open my $fh, "<", "$content_list") {
    while (my $line = <$fh>) {
        $line =~ s/\s+$//;
        print "\t Applying EAs to file $line\n";

        my $setEAVersion= "$getAttrib -s TeamSite/Assocation/Version=1 \"$line\"";
        `$setEAVersion`;
        my $setEADCRType= "$getAttrib -s TeamSite/Templating/DCR/Type=muni_site/nav \"$line\"";
        `$setEADCRType`;
        my $setEAFormValid= "$getAttrib -s iw_form_valid=true \"$line\"";
        `$setEAFormValid`;

        print "\t EAs applied successfully to $line\n";
        print "\t**********************************************************\n";
    }

s/\s+$//正则表达式从行尾删除所有空格字符。

作为奖励,我更改了您的open,以便它使用三个参数版本,并且我还更改了您的while循环以避免代码重复。