Question

Value in my input file (list_muni_nav.txt). Each line is a path for a file, here 'test' and 'test2' are the filename.

/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

My perl script(as below) should read each line in input file and execute them. But, except last line rest of the lines are not being located by the script. Please see the result

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

RESULT:

        *****************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
        **********************************************************

Please help. Thank you!!

Was it helpful?

Solution

I think that @stark is right, your problem is with newline characters. chomp will remove any trailing string corresponding to the special variable $/, which is different on Windows and Linux. You can get round this problem using a regex instead:

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

The s/\s+$// regex removes all whitespace characters from the end of the line.

As a bonus, I changed your open so that it uses the three argument version and I also changed your while loop to avoid code repetition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top