Automatically Make Shell Scripts Executable

I strongly believe that a script should be created to handle any repetitive process in the terminal.

By default, when you create a new script, it does not have executable permissions.  So for each script that you create, you then have to chmod the file to be executable.  However, its kind of hypocrital to say that a script should be created to replace any repetitive process and then not figure out a way to automate the ‘chmod’ command when creating a script.

Well, as it turns out, a simple addition to your vimrc file will automatically make any newly created script an executable (so long as you have the ‘#!’ in the file).

" automatically give executable permissions if file begins with #! 
"and contains '/bin/' in the path

au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod a+x  | endif | endif
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/usr/bin/" | silent !chmod a+x  | endif | endif

So just copy the above code into your .vimrc file (each uncommented line ends in 2 endif commands) and now you can create scripts that are executable by default.

Enhanced by Zemanta

5 thoughts on “Automatically Make Shell Scripts Executable

  1. […] doing any type of programming or scripting, I do my best to streamline everything (see this post and this post).  Basically anything I do in the Terminal that can be automated, I try to […]

  2. I think this is one of the most vital info for me. And i am glad reading your article. But wanna remark on few general things, The site style is ideal, the articles is really great : D. Good job, cheers

  3. Rashmi Patel dental license

    Automatically make scripts executable | Tech N Comp

  4. Plans are secured for the development of a
    swimming center and a much larger building to provide for meetings.
    People again turned to astrology during the late 19th h and 20th centuries when spiritualism made
    a comeback. Multiple big and small fish populate the waters near
    the island, as well as sea turtles and sharks.

    my blog: news
    news recently posted..newsMy Profile

  5. What’s up to every body, it’s my first go to see

    of this weblog; this webpage includes remarkable and genuinely excellent data in favor of readers.
    online date recently posted..online dateMy Profile

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.