HOw to check for file existence in each home directory of Unix users 
Author Message
 HOw to check for file existence in each home directory of Unix users

Hello

What I need to do is check for the existence of a .forward file in each
of my users homme directories.
Then, if there is no .forward file in the users hoem dir I would get
that users loginid
printed to a file.
Can someone help me out here??..>thanks!!!.Dan



Mon, 10 Jul 2000 03:00:00 GMT  
 HOw to check for file existence in each home directory of Unix users

Re: HOw to check for file existence in each home directory

Dan> Hello What I need to do is check for the existence of a
Dan> .forward file in each of my users homme directories.
Dan> Then, if there is no .forward file in the users hoem
Dan> dir I would get that users loginid printed to a file.
Dan> Can someone help me out here??

The following are likely to be needed:

getpwent()
! -f $forward

"man perlfunc" is the place to start.

hth,
tony



Tue, 11 Jul 2000 03:00:00 GMT  
 HOw to check for file existence in each home directory of Unix users


Quote:
>What I need to do is check for the existence of a .forward file in each
>of my users homme directories.
>Then, if there is no .forward file in the users hoem dir I would get
>that users loginid
>printed to a file.

Something like:

  #!/usr/local/bin/perl -w

  # open the password file
  open PWD, "/etc/passwd" or die "Can't open password file: $!\n";
  while (<PWD>) {
    chomp;

  # split on ":"

  # get the userid and home directory -- 1st and 6th elements of the array

  # if the home directory exists
    if (-d $home_dir) {

  # open the home directory and read it into an array
      opendir HOME, "$home_dir" or die "Can't open $home_dir: $!\n";

  # grep the array for ".forward"

  # if ".foward" is found print "userid has a .forward file"
  # if you want to print this to a file, open a file for writing before
  # the while statement and print to it.
  # Example: print FH "** $userid has a $found[0] file **\n";
      if ($#found >= 0) {
        print "** $userid has a $found[0] file **\n";
      }

  # If the user's home directory wasn't found, print a warning
    } else {
      print "$userid has no home directory.\n";
    }
  }

  # close the password file
  close PWD;

HTH,

--

http://www.panix.com/~clay/



Tue, 11 Jul 2000 03:00:00 GMT  
 HOw to check for file existence in each home directory of Unix users

!
!

!
! >What I need to do is check for the existence of a .forward file in each
! >of my users homme directories.
! >Then, if there is no .forward file in the users hoem dir I would get
! >that users loginid
! >printed to a file.
!
! Something like:
!  
! #!/usr/local/bin/perl -w
!
! # open the password file
! open PWD, "/etc/passwd" or die "Can't open password file: $!\n";
! while (<PWD>) {
!   chomp;
!
! # split on ":"

!
! # get the userid and home directory -- 1st and 6th elements of the array

!
! # if the home directory exists
!   if (-d $home_dir) {

[start-mark]

! # open the home directory and read it into an array
!     opendir HOME, "$home_dir" or die "Can't open $home_dir: $!\n";

!
! # grep the array for ".forward"

!
! # if ".foward" is found print "userid has a .forward file"
! # if you want to print this to a file, open a file for writing before
! # the while statement and print to it.
! # Example: print FH "** $userid has a $found[0] file **\n";
!     if ($#found >= 0) {
!       print "** $userid has a $found[0] file **\n";
!     }

[end-mark]

! # If the user's home directory wasn't found, print a warning
!   } else {
!     print "$userid has no home directory.\n";
!   }
! }
!
! # close the password file
! close PWD;

couldn't you avoid some extra work and replace the code between the
[mark]'s above to just test for the .forward file directly rather
than reading in each users HOME directory and grepping for
.forward files:

if (-f "$home_dir/.forward") {
    print "** $userid has a .forward file **\n";

Quote:
}

regards
andrew


Tue, 11 Jul 2000 03:00:00 GMT  
 HOw to check for file existence in each home directory of Unix users


Quote:

>!
>!

>!
>! >What I need to do is check for the existence of a .forward file in each
>! >of my users homme directories.
>! >Then, if there is no .forward file in the users hoem dir I would get
>! >that users loginid
>! >printed to a file.
>!
>! Something like:
>!  
>! #!/usr/local/bin/perl -w
>!
>! # open the password file
>! open PWD, "/etc/passwd" or die "Can't open password file: $!\n";
>! while (<PWD>) {
>!   chomp;
>!
>! # split on ":"

>!
>! # get the userid and home directory -- 1st and 6th elements of the array

>!
>! # if the home directory exists
>!   if (-d $home_dir) {
>[start-mark]
>! # open the home directory and read it into an array
>!     opendir HOME, "$home_dir" or die "Can't open $home_dir: $!\n";

>!
>! # grep the array for ".forward"

>!
>! # if ".foward" is found print "userid has a .forward file"
>! # if you want to print this to a file, open a file for writing before
>! # the while statement and print to it.
>! # Example: print FH "** $userid has a $found[0] file **\n";
>!     if ($#found >= 0) {
>!       print "** $userid has a $found[0] file **\n";
>!     }
>[end-mark]

>! # If the user's home directory wasn't found, print a warning
>!   } else {
>!     print "$userid has no home directory.\n";
>!   }
>! }
>!
>! # close the password file
>! close PWD;
>couldn't you avoid some extra work and replace the code between the
>[mark]'s above to just test for the .forward file directly rather
>than reading in each users HOME directory and grepping for
>.forward files:
>if (-f "$home_dir/.forward") {
>    print "** $userid has a .forward file **\n";
>}

Absoultely. My brain wasn't quite awake yet...

I could have also used getpwent instead of opening the password file and
parsing it...

It just goes to show -- "There's alway more than one way to do it..."

--

http://www.panix.com/~clay/



Tue, 11 Jul 2000 03:00:00 GMT  
 HOw to check for file existence in each home directory of Unix users

Quote:

> What I need to do is check for the existence of a .forward file in each
> of my users homme directories.

Use the -f filetest. Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



Tue, 11 Jul 2000 03:00:00 GMT  
 HOw to check for file existence in each home directory of Unix users

$name = 'joseph';
$home = (getpwnam $name)[7];
print ".forward exists\n" if -e "$home/.forward";

        -joseph
         http://www.effectiveperl.com

Quote:

> Hello

> What I need to do is check for the existence of a .forward file in each
> of my users homme directories.



Wed, 12 Jul 2000 03:00:00 GMT  
 HOw to check for file existence in each home directory of Unix users

no ... getpwnam or getpwent please!

        -joseph
         http://www.effectiveperl.com

Quote:


> >What I need to do is check for the existence of a .forward file in each
> >of my users homme directories.
> >Then, if there is no .forward file in the users hoem dir I would get
> >that users loginid
> >printed to a file.

> Something like:

>   #!/usr/local/bin/perl -w

>   # open the password file
>   open PWD, "/etc/passwd" or die "Can't open password file: $!\n";



Wed, 12 Jul 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. how to check the existence of ^M in a text file on Unix

2. Checking existence of a user function

3. checking the existence of a directory

4. How to set to logged-on user's home directory

5. How to get user's home directory?

6. ftp file existence check

7. Help : Simple method to check the existence of a file

8. Check existence of file on other server

9. Checking of the existence of a remote file (with rsync)

10. Checking for existence of file

11. Check existence of a remote file!!!

12. checking for the existence of a file

 

 
Powered by phpBB® Forum Software