Tuesday, May 24, 2011

Good Tools for Debugging Bash Scripts

# builtin for displaying a list of all settable options

# to see if your process is in a login shell
shopt | grep login

# Print commands and their arguments as they are executed
set -x
# turns off printing of commands
set +x

# standard way to determine linux distribution
lsb_release -a

# current process id

# display cmdline arguments to your script
cat /proc/$$/cmdline

# Not a tool, but something good to know
# $@ vs $*
## set ARGV
set a b c
## dollar star will expand to a single string
for x in "$*"; do echo $x; done
OUTPUT> a b c
## dollar at sign will expand to array of separate elements.
for x in "$@"; do echo $x; done

Friday, May 20, 2011

bash type vs unix which

The real difference between type and which.

Avoid which, the exit code isn't guaranteed to exist and some operating systems change the output.

Thursday, May 19, 2011

echo Bash{Brace Expansion, Buildin Type}

Today a colleague of mine, Elliot Shank (@clonezone on twitter), introduced me to two new bash commands that I think are pretty useful. I've decided to blog about them in the hopes that the commands will stick.

First Brace Expansion. You can read a nice over view of Brace Expansion here. The summary gives a nice example of using it to make mulitple directories. You can also use it to rename a file that is in a deep directory structure, for example like this:

mv /site/web/hello_world/version1.0.0/data/2010/05/17/foo{,.bar}

So rather than typing out the whole directory structure again you just have to type it once! The above expands to:

mv /site/web/hello_world/version1.0.0/data/2010/05/17/foo /site/web/hello_world/version1.0.0/data/2010/05/17/foo.bar

Don't forget the ',' in the braces either. Basically the braces are being expanded and the first element is nothing.

The second command is the bash builtin command type. Type doesn't have a man page but you can get information about it by using the built in help and by typing type:

help type
type type

type is an alternative to which. The big difference is that which is an external command whereas type is a bash builtin. You can read more about it here. With type(and which) you can things like vim `type -p foo` which is kind of nice.

In my time tests these were my results:
using type:
real 0m0.000s
user 0m0.000s
sys 0m0.000s

Using which
real 0m0.003s
user 0m0.001s
sys 0m0.002s

So type is a little faster.

Tuesday, May 17, 2011

Git – setting local repo and pushing to new remote

Initialize local git repository
git init

Add and commit all the files to the local repo
git add *
git commit -m 'nick - git init'

Create your remote repository

Locally add your remote repository
git remote add origin

Locally setup tracking so we can do 'git pull' rather than 'git pull origin master'
git branch --set-upstream master origin/master