Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Find Scripts and Make Executable

by The Mad Hatter (Priest)
on Apr 22, 2003 at 04:16 UTC ( [id://252193]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info /msg [The Mad Hatter]
Description: The other day I had a large number of Perl and shell scripts that were not marked executable. They needed to be, but there was a great deal of them scattered all over the place (a source tree directory, to be precise) and I wasn't about to find and chmod them myself. This is the result.

It will check the first line of a file to see if it starts with a shebang (#!), and if it does, will make it executable for the user who owns it. I shell out to the system chmod because it was simpler and quicker to use the symbolic permissions notation (u+x) instead of stat-ing the file, adding the correct value to the octal permissions, and then using Perl's chmod with that modified value.

File names to check are specified as arguments. I used the script in combination with find: find . -type f -exec isscript \{\} \;

#!/usr/bin/perl -w

foreach (@ARGV) {
    open FILE, $_ or die $!;
    system "chmod", "-v", "u+x", $_ if <FILE> =~ /^#!/;
}
Replies are listed 'Best First'.
•Re: Find Scripts and Make Executable
by merlyn (Sage) on Apr 22, 2003 at 04:23 UTC
      Thank you for pointing out File::chmod. I didn't bother to search CPAN at the time because it was just a quick script.
        I didn't bother to search CPAN at the time because it was just a quick script.
        One of the keys to writing a quick script quicker is to write less of it. Get familiar with interesting CPAN modules, and have them installed.

        Most of my columns are merely wrappers around good CPAN modules. So, I write a dozen lines of code, and call hundreds of lines of previously tested code. That's good leverage. (I even wrote a column about that. {grin})

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Find Scripts and Make Executable
by artist (Parson) on Apr 22, 2003 at 15:35 UTC
    I have found that sometime scripts are purposely made not executable may be to avoid accidental running as they are meant to be run from cron or some other ways. So it is done not as a security mesaure but as a precautionary measure.

    artist

      I agree that sometimes that is true and is a good precaution, but in my case, the scripts weren't marked executable because they were copied over from a Windows machine which doesn't preserve the permission bits.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found