muaddib2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all I am having problems executing my script Basically the script scans for all uids on the system then goes through the entire filesystem to find all files owned by each individual user. The problem is that for some reason my script reports a syntax error and I am not sure why here is my code
#!/usr/bin/perl -w #This line will check the passwd file for any uid greater 1000 system("awk -F:'{if ($3 >1000) print $1}' /etc/passwd > /home/chancock +/users"); open(FILE, 'users'); print <FILE>;#!/usr/bin/perl -w close FILE;
The error that is returned is "Use of uninitialized value at ./testing line 3. ANY ideas.

Replies are listed 'Best First'.
Re: FInd and permission change
by runrig (Abbot) on Jun 05, 2001 at 02:35 UTC
    $1 and $3 are special perl variables. Inside double quotes the '$' would need to be escaped (backslashed) if you want them literally as is. I don't see the point of combining perl and awk in this way, though. I would choose one or the other for this.

    Update:A perl solution is already given below, so just for variety, here's an awk (maybe its nawk or gawk) solution:

    #!/bin/awk -f BEGIN {FS = ":"} $3 > 1000 {print $1}
Re: FInd and permission change
by bikeNomad (Priest) on Jun 05, 2001 at 02:36 UTC
    You get this message because $3 and $1 are uninitialized in line 3.

    Unlike shell programming, in Perl single quotes don't protect anything inside double quotes.

    I wonder why anyone would want to call awk from perl, though; either language would do this job easily by itself.

(jeffa) Re: FInd and permission change
by jeffa (Bishop) on Jun 05, 2001 at 02:33 UTC
    I'll bet that you aren't really opening the file 'users'. Try this:
    open(FILE, 'users') or die "reason: $!";
    Always check for the success of opening a file, Perl won't do it for you. Also, if you are planning on writing to that file handle, you should open like so:
    open(FILE, '>users') or die "reason: $!";
    > means open for writing, this will clob the file, so use >> if you only wish to append.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: FInd and permission change
by VSarkiss (Monsignor) on Jun 05, 2001 at 07:00 UTC
    I too am having trouble understanding your use of awk and Perl in the same program. Plus, this looks really weird:
    print <FILE>;#!/usr/bin/perl -w
    That's just a comment following the print. Is that what you intended? Looks like a formatting error....

    If you're trying to write the login name of any uid greater than 1000 found in the local /etc/passwd to the file /home/chancock/users, here's what you do:

    open I, '/etc/passwd' or die "Can't open passwd: $!\n"; open F, '>/home/chancock/users' or die "Can't open users: $!\n"; my @passwd; while (<I>) { @passwd = split /:/; print F $passwd[0], "\n" if $passwd[2] > 1000; } close I; close F;
    HTH