http://www.perlmonks.org?node_id=117230

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

Further to this thread, in which I try to write a utility script to make my newly uploaded files executable, I've now got my ISP to let me run scripts suEXEC. And, bearing in mind the helpful comments on my recently posted other code I have pared my script down to the script you see at the end of this node.(if sibling pixel should read this - note that I have taken to heart your advice about how to use CGI.pm, for which again thanks).

So that's fine. It works. It toggles the file permissions of files in a given directory. BUT... does it toggle them from 644 to 755? It does not! It toggles them from 204 to 363. It consistently chmods files to 204 when I want 644 and 363 when I want 755. And it does the same thing if I try 0755 and 0644. I'm puzzled.

Is this something wrong with my script? If so, I'd be very grateful to know what. Or is it some obvious thing outside the script I should check - again, any advice gratefully received.

Here's the code:
#!/usr/bin/perl -w use strict; use CGI qw(:standard); print header, start_html( -title=>'CHMOD UTILITY' ); &do_chmod if param; &send_form; sub do_chmod { for (param('Dirs')) { my @files; push @files, $_ while <$_*.p*>; if (chmod param('Action'), @files) { print "Changed permissions on " . join(" ; ",@files) . " t +o " . param('Action') . "<BR>"; } else { print "Could not change permissions on " . join(" ; ",@fil +es) . " to " . param('Action') . "<BR>"; } } print "DONE"; } sub send_form { my @dirs; push @dirs, $_ while <../*/>; print startform( -name=>'mainform', -method=>'POST', -action=>'chmod.pl', ), checkbox_group( 'Dirs', [@dirs], [], 1, ), submit( -name=>'Action', -value=>'0755', ), submit( -name=>'Action', -value=>'0644', ), endform, end_html; }
Thanks to all,

George Sherston

Replies are listed 'Best First'.
Re: Ongoing chmod lack of success
by tommyw (Hermit) on Oct 06, 2001 at 21:26 UTC
    644 (dec)=1204 (oct)
    755 (dec)=1363 (oct)
Final Update: Re: Ongoing chmod lack of success
by George_Sherston (Vicar) on Oct 06, 2001 at 21:52 UTC
    So I learnt that '0755' has a numerical value of decimal 755, whereas 0755 has a numerical value of octal 755. Worth knowing. And now I fixed my script and it does exactly what I want and I'm very happy. Thanks to eveyrone.
    #!/usr/bin/perl -w use strict; use CGI qw(:standard); print header, start_html( -title=>'CHMOD UTILITY' ); &do_chmod if param; &send_form; sub do_chmod { my $Perm; if (param('Action') eq 'Not Executable') { $Perm = 0644; } else { $Perm = 0755; } for (param('Dirs')) { my @files; push @files, $_ while <$_*.p*>; if (chmod $Perm, @files) { print "Changed permissions on " . join(" ; ",@files) . " t +o " . $Perm . "<BR>"; } else { print "Could not change perms on " . join(" ; ",@files) . +" to " . $Perm . "<BR>"; } } print "DONE"; } sub send_form { my @dirs; push @dirs, $_ while <../*/>; print startform( -name=>'mainform', -method=>'POST', -action=>'chmod.pl', ), checkbox_group( 'Dirs', [@dirs], [], 1, ), submit( -name=>'Action', -value=>'Executable', ), submit( -name=>'Action', -value=>'Not Executable', ), endform, end_html; }


    George Sherston
      Or, there's the oct function...
Re: Ongoing chmod lack of success
by lemming (Priest) on Oct 06, 2001 at 21:27 UTC
    Check out the umask function.