Should be a simple subdirectory check


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Should be a simple subdirectory check
# 1  
Old 03-03-2012
Should be a simple subdirectory check

Ok. Just getting back into PERL and probably (or most definitely) making a mountain out of a mole hill.

I'm trying to see if a subdirectory exists, and if not, print the slightly modified path of the missing sub to a file. Sounds simple enough. Well here is my elaborate code. Save the snickering. I know it's nuts. There must be a simpler way.

Directory structure is:

/nas/240/234/2311/VMAIL/1

I need to check that the directory /1 exists. If not, print the 2402342311 to a file.

Any help and tolerance is greatly appreciated
Code:
use strict;

my $nas = "/nas";

opendir (NAS, $nas) or die "Can't open $nas: $!";
open OUTFILE, ">bad_accounts" or die $!;

while(my $npa = readdir(NAS)) {
    if ($npa =~ /\d{3}/) {
        my $path = $nas . "/" . $npa;
        print "PATH = '$path' \n";
        opendir (NPA, $path) or die "Can't open $path: $!";
        while(my $nxx = readdir(NPA)) {
           if ($nxx =~ /\d{3}/) {
               my $path = $path . "/" . $nxx;
               print "PATH = '$path' \n";
               opendir (NXX, $path) or die "Can't open $path: $!";
               while(my $line = readdir(NXX)) {
                   if ($line =~ /\d{4}/) {
                       my $path = $path . "/" . $line;
                       print "PATH = '$path' \n";
                       opendir (LINE, $path) or die "Can't open $path: $!";
                       while(my $vmail = readdir(LINE)) {

                           if ($vmail == "VMAIL") {
                               my $path = $path . "/VMAIL";
                               opendir (VMAIL, $path) or die "Can't open $path: $!";
                               my $count = grep { ! /^\.{1,2}/ } readdir VMAIL;
                               if ($count > 2) {
                                  print "COUNT = $count \n";
                               }
                               else {
                                  print OUTFILE "BAD ACCT = " . $npa . $nxx . $line . "\n";
                                  print OUTFILE "COUNT = $count \n";
                               }
                           }
                       }
                   }
               }
           }
        }
    }
}
closedir LINE;
closedir NXX;
closedir NPA;


I do get the numbers in a file, but the each number gets printed 3 times Smilie

Last edited by Franklin52; 03-05-2012 at 03:16 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-04-2012
expecting this .. ??!!
Code:
dir_path=/nas/240/234/2311/VMAIL/1
[ -d "$dir_path" ] && echo "Exists" || echo "$dir_path" | awk -F/ '{print $3$4$5}' > outfile

# 3  
Old 03-04-2012
Yes, that's why I'm checking for count<2 because . And .. Always exist. I failed to mention this is a rather large directory tree, so the subdirectort names will need to be variable.

My script works even though its probably overkill. Just can't figure out why it's printing each match 3 times to OUTFILE

---------- Post updated at 07:38 AM ---------- Previous update was at 07:36 AM ----------

Meant count>2

---------- Post updated at 09:05 AM ---------- Previous update was at 07:38 AM ----------

Found it. I was checking $vmail == "VMAIL" when == is for numerical conditions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to move to the last subdirectory one by one?

Hi, How can i traverse to the last subfolder in all the directories. eg: i have the below folders structure f1/sf1/r1 f2/sf2/r2 f3/sf3/r3/r4 i need to move to the last directory in each directory. Can anyone tell me a solution for this? I saw an example that does that. find . -type... (2 Replies)
Discussion started by: Little
2 Replies

2. Shell Programming and Scripting

Drilling down to the last subdirectory

Within a BASH environment, I need to search through a filesystem looking for the last subdirectory. Once the last subdirectory of the filesystem is found, I need to create another directory within it: Basic example: /u01/data1/project_1/proj_data1/score... (9 Replies)
Discussion started by: leepet
9 Replies

3. Shell Programming and Scripting

Simple check login script

Hello, I just started with shell and i am doin simple script to check if certain user is logged in. Script takes 3 args. name sleep time and quit value if there is q for quit then date and user name is printed to file, not to screen. so far i have got this... login() { while :... (3 Replies)
Discussion started by: shelllo
3 Replies

4. Shell Programming and Scripting

simple while loop script to check a connection

Hi, I am completely new to shell scripting. Basically I am wanting to create a simple while loop script to check if a network connection is available from 'netstat -a' and if its present then output a character to the serial port. I think i am nearly there.. but need some assistance.. this is... (9 Replies)
Discussion started by: zippyzip
9 Replies

5. Shell Programming and Scripting

making subdirectory within makefile

hi, I am trying to create subdirectories under my ../objs directory within a make file. my ../src directory has the following subdirectories: source-a source.1.1 so, I want to label the subdirectories under my ../objs directory with the same names as those in the ../src directory, so that... (0 Replies)
Discussion started by: bacpp
0 Replies

6. Shell Programming and Scripting

Simple Script to Check running Process

#!/bin/sh CHECK='ps aux | grep start.jar | grep -v grep | wc -l' if then /usr/local/jre-1.7.0/bin/java - jar start.jar & else fi Could anybody advise whats up with this code im trying to put this in as a cron job to check that solr search engine is running every 10secs and if... (10 Replies)
Discussion started by: will_123
10 Replies

7. Shell Programming and Scripting

Pick one file from each subdirectory

Hi, I have a problem I am trying to solve with bash. I need to search in a file system (data base) with hundreds of directories and thousands of subdirectories and millions of files. The files have a specific format with a header that gives the properties. Directories are organized so... (1 Reply)
Discussion started by: majest
1 Replies

8. UNIX for Dummies Questions & Answers

Any simple method to check date format (YYDDD)?

Hi Guru's, Are there any simple method to check the date format which is in "YYDDD" (ex: 08002 for 02-Jan-2008)? Eventhough this can be implemented in several way's, I need the simpler one. Any idea would be much appreciated. Thanks in advance, Mysore Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

9. Shell Programming and Scripting

simple check to see if a folder exists

Hi, I have cobbled together a simple script to create a Windows folder in a bunch of home folders on a mac server using the following code. for i in /Volumes/student_data/studenthomefolders/* do u=`echo $i | cut -d/ -f5` //if //then //echo "Folder already exists for "$u" Skipping" //else... (4 Replies)
Discussion started by: psyman
4 Replies

10. UNIX for Dummies Questions & Answers

subdirectory password

Hi! I am a user of a workstation. I have got my own previlages on my system. Still I would like to have a security for my data. Can I set password or write a script that the entry to a particular subdirectiry is restricted?:confused: (1 Reply)
Discussion started by: sskb
1 Replies
Login or Register to Ask a Question