More bash :-)
grep -Ir --exclude='.svn' dbsettings !(dbsettings)
This is the line i used couple of days ago when searching through pydra's code. -I and -r are what i'm using on daily basis, first one tells grep to ignore binary files second one to search the directory recursively. What i don't use often is the --exclude parameter, it tells grep to ignore the files that have .svn anywhere in their path. Since svn stores it's metadata in the .svn dirs that is very useful. "dbsettings" is simply a string i want to search for.
Now to the interesting part "!(dbsettings)". This is called extended globing and allows you to use regex like pattern inside shell. Some of the available operators are '?', '+', '*' and '@' (the later does nothing, only allows you to use other features). Suppose we want to get the files start_master.sh and start_node.sh:
shopt -s extglob; ls start_@(master|node).sh
bash: syntax error near unexpected token `('
First of all a word about shopt -s extglob, extended globing is off by default and with this you turn it on. That apart what the hell just happened? You see bash parses input line by line and glob expansion is one of the things that attempts to do before it actually got to executing the shopt thingy.
$ shopt -s extglob $ ls start_@(master|node).sh start_master.sh start_node.sh
This per-se is not really useful, the cool feature about extglob is that they are space proof. Looping through it is OK (as opposed to `ls | grep`). Here you can read up on simple as well as extended globing.
You should use "#!/usr/bin/env bash" as the shebang line in your script, it increases the portability (/usr/bin/env is required by POSIX to be there, BSD's tend to have bash in /usr/local/bin). What really happens here is that kernel invokes that binary passing it the file you are executing as an argument. As a side note most kernels don't perform argument splitting (one exception i can think of is freebsd). Due to that "#!/usr/bin/env bash -u" will not work on most kernels, as kernels tells the env to execute "bash -u" which it can't find and errors :-).
Here is the tip for the script writers and vim users
function Modchange()
if getline(1) =~ "^#!.*/bin"
silent !chmod a+x
endif
endfunction
au BufWritePost * call Modchange()
Having that in your .vimrc will make the file executable on save automatically if it thinks it's a script. NOTE: i probably stole this from the internets many moons ago.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=13dfc3bd-a9f1-4bb9-90bc-e4e229a79bc6)