My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://www.mattdturner.com/wordpress
and update your bookmarks.

AdSense

Pages

Wednesday, April 20, 2011

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 <afile> | endif | endif
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/usr/bin/" | silent !chmod a+x <afile> | 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.
 

0 comments:

Post a Comment