LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-24-2013, 07:41 PM   #1
hcbj44
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
Perl 5.10 on RHEL 6.2: The "for" loop code is not executing within my Perl Script


Simply, I wrote a little Perl script that will have an outer loop (the "for" loop) to process all of the directories with a similiar name of "/home/customer*/input" and an inner loop (the "foreach" loop) that will process all files found within each directory of "/home/customer*/input".

When I execute the script, it does not fail but I do not receive my print command output (a checking mechanism). When I modify the script to remove the outer loop (the "for" loop) everything works correctly.

Here is the code:

#!/usr/bin/perl

use strict;
use warnings;
use English;
use File::Basename;
use File::Copy;

# VARIABLES

my $AIWBASE = "/aiw/aiw1";
my $totcus = `ls -d /home/customer* | wc -l`;
my $customer = 1;

# MAINLINE

for (my $customer=1; $customer > $totcus; $customer++) {
my $SaaS_Customer_Input_Data = "/home/customer$customer/input";
my $SaaS_Customer_Output_Data = "/home/customer$customer/output";
opendir(INPUTDIR, $SaaS_Customer_Input_Data) or die "Couldn't open '$SaaS_Customer_Input_Data': $!";
opendir(OUTPUTDIR, $SaaS_Customer_Output_Data) or die "Couldn't open '$SaaS_Customer_Output_Data': $!";
my @customer_files = grep {!/^\./ && -f "$SaaS_Customer_Input_Data/$_"} readdir(INPUTDIR);
foreach my $customer_file (@customer_files) {
print "total customers: $totcus\n";
print "Customer Number: $customer\n";
print "customer file: $customer_file\n";
}
closedir(INPUTDIR);
closedir(OUTPUTDIR);
}
exit 0;


Perhaps someone can point-out my failure here!
 
Old 01-24-2013, 07:50 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Would you mind adding code tags? .. it's a bit hard to read, thanks

Are you sure this is correct?:
Code:
for (my $customer=1; $customer > $totcus; $customer++) {

Last edited by kbp; 01-24-2013 at 07:54 PM.
 
Old 01-24-2013, 08:06 PM   #3
hcbj44
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
What are code tags, I'm sorry? Well, I think that the "for" loop code is correct but it is why I have posted the question for assistance.
 
Old 01-24-2013, 08:29 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
He is talking about putting code tags around your listing to make it easier to read.

I don't think readdir does what you think it does.

It opens a directory and gives a list of file names. It does not do subdirectory processing.

If a directory listing looks like
Code:
$ ls -r xyz
xyz/a
xyz/a/level
xyz/a/another
xyz/b
xyz
Then
Code:
opendir(INPUTDIR,"xyz");   # I'll assume it always opens
@list = readdir(INPUTDIR)
Will give you the array list with:
Code:
.
..
a
b
Your grep line appears to be looking for "xyz/a", which won't be there.

The result should be an empty list (@customer_files will have nothing in it), and the script then exits.

No output.

Last edited by jpollard; 01-24-2013 at 08:30 PM.
 
Old 01-24-2013, 08:36 PM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
For loops are usually in the format
Code:
for (<initialise>; <test>; <increment>)
.. where the test needs to return true to execute the body.
Yours initialises a value to 1, tests whether the value is greater than $totcus and then skips executing the loop contents because it isn't.

I believe what you want is:
Code:
for (my $customer=1; $customer <= $totcus; $customer++) {
 
1 members found this post helpful.
Old 01-24-2013, 11:26 PM   #6
hcbj44
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Great post sir or madam! It worked beautifully! It wasn't so intuitive at the time regarding the second parameter of the "for" loop command but now it is so! Thanks a trillion!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
#In this perl code, im getting the input as "ip address" from the user and pinging it dawood munavar Linux - Newbie 1 04-07-2011 09:42 PM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 10:08 PM
"Not a CODE" reference error perl v5.10.0, Deprecated assignment operation. skuletm Programming 3 03-12-2009 12:21 AM
please help me with this perl code getting error "use of uninitialized value" haxpak Programming 10 03-01-2009 10:55 PM
Compiling KDE 3.2.0 - "Can't open perl script "/dcopidlng/kalyptus" linuxgamer09483 Linux - Software 3 04-09-2004 08:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration