Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: File reading with hashes

by Bindo (Acolyte)
on Aug 08, 2013 at 08:30 UTC ( [id://1048512]=note: print w/replies, xml ) Need Help??


in reply to Re: File reading with hashes
in thread File reading with hashes

Thank you very much for triying to help me out. However I now get a permission error. "could not open /x02/bee/MY_FILE Permission denied at ./base_ports.pl line 9, <FH2> line 3103. but when I tried to open the file before going on to the regex section it just works, As in it opens up the files on the screen

my %users = %{user_paths()}; foreach my $user (keys %users) { if (-e "$users{$user}/MY_FILE") { open (FH2, "<$users{$user}/MY_FILE") or die "could not open $u +sers{$user}/MY_FILE $!"; while (my $line = <FH2>) { print $line; } } }

Also I tried commented out the die function and tried to riun and then I get that same "readline" error. Any thoughts?

Replies are listed 'Best First'.
Re^3: File reading with hashes
by Random_Walk (Prior) on Aug 08, 2013 at 09:32 UTC

    can you open /x02/bee/MY_FILE from the command line with something like cat? What are the perms if you ls -l /x02/bee/MY_FILE? Does this match your user/group settings?

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Thanks. Yes there are no problems with the file permissions sir. Each and every one of those files have read permission.

        there are no problems with the file permissions

        If you are getting a permission denied error I am afraid that there is a problem with permissions, however probably not an obvious one.

        Can you add the following code between the if -e and the open FH2 line and tell us the results.

        my @file_stat = stat "$users{$user}/MY_FILE"; my @dir_stat = stat "$users{$user}"; printf("%d $users{$user} -> 0%o %d %d\n", $>, $dir_sta +t[2] & 07777, $dir_stat[4], $dir_stat[5]); printf("%d $users{$user}/MY_FILE -> 0%o %d %d\n", $>, $file_st +at[2] & 07777, $file_stat[4], $file_stat[5]);

        Update: tidied up code no functionality changed.

Re^3: File reading with hashes
by Random_Walk (Prior) on Aug 08, 2013 at 11:22 UTC

    Can you try this version and see if any files are opened?

    #!/usr/bin/perl use strict; use warnings; # my $fname = '.profile'; my $fname = 'MY_FILE'; my %users = %{user_paths()}; for my $user (keys %users) { my $fh; if (open $fh, '<', "$users{$user}/$fname") { while (my $line = <$fh>) { if ($line =~ /BASE_PORT=(\d+)/) { print "$user --> $1"; } } } else { warn "couldn't open $users{$user}/MY_FILE $!"; } } sub user_paths { my %users; open my $fh, '<', "/etc/passwd" || die "Can't Open : $!"; while (<$fh>) { my @user = split (/:/); if ( $user[2] > 500 ) { $users{$user[0]} = "$user[5]"; } } return \%users; }

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Thank you very much R, It worked. :) I owe a big apology to people who tried to help me out because I found out that the string I was looking for in the "MY_FILE" file was "SREG_PORT" not "BASE_PORT". I have wasted your time and mine over some silly thing that I should have easily realized. Sorry gentlemen

      Mr R, with right key word being searched the script I initially mentioned in my question also works but I do get that readline error message printed only once on the screen. I tried tweaking the scrit a bit but no luck. Im gonna use your script as it is very clean and solid, but Id still like to realize what I have done wrong to get that error.

      following is the output when I run my version of script

      supun --> 40000 sam --> 25000 readline() on closed filehandle $fh at ./base_ports.pl line 11. binasha --> 10000 bundy --> 37000 binjiya --> 5000

      Following is my version of script

      my $fname = 'MY_FILE'; my %users = %{user_paths()}; foreach my $user (keys %users) { my $fh; if (-e "$users{$user}/$fname") { open ($fh, '<', "$users{$user}/$fname"); while (my $line = <$fh>) { if ($line =~ /S_REG_PORT_NO=(\d+)/) { print " $user --> $1\n"; } } } } sub user_paths { my %users; open my $fh, '<', "/etc/passwd" || die "Can't Open : $!"; while (<$fh>) { my @user = split (/:/); if ( $user[2] > 500 ) { $users{$user[0]} = "$user[5]"; } } return \%users; }

      Only different thing Im doing here is that Im checking the existence of the file first with the "-e". Please help me figure this out. I just like to learn is all :) thanks again for helping out gentlemen.

        Is it possible a user has a folder called MY_FILE ? . If so, this code should find it .
        foreach my $user (keys %users) { my $fh; my $file = "$users{$user}/$fname"; if (-d $file){ print "$file is directory for user $user\n"; } elsif (-e $file ) { open ($fh, '<', $file) or warn "Could not open $file for user $use +r :$!" ; while (my $line = <$fh>) { if ($line =~ /S_REG_PORT_NO=(\d+)/) { print " $user --> $1\n"; } } } else { print "$file does not exist for user $user\n"; } }
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1048512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-25 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found