Dependent Origination

Shell Scripts on Ubuntu

Posted on: October 1, 2010

It has been a long time that I have written about any technical details or new knowledge recently gained on this blog. One reason is that I have started using personal Wiki pages to document the accumulation of my own tech knowledge. Another reason is that I have been so absorbed in working that except working anything else is considered a waste of time and not even time to stop and consider what I have learned time and again.

Luckily this has changed. I spent nearly a full day today to write a shell script that can hook up with my own pre-commit hooks for svn. Along the way, I discovered useful bits on writing a shell script faster.

1. Number one discovery of the day: how to debug shell scripts. Change the shebang line from #!/bin/sh to #!/bin/sh -x will do the trick.

2. On Ubuntu, /bin/sh, by default, links to dash, which is a supposedly POSIX-compliant shell interpreter for bash. Unfortunately dash still has bugs which makes it not exactly POSIX compliant. So if your script starts complaining about ‘unexpected operator’, consider using bash directly instead of the symlinked dash. That is, change the shebang line from #!/bin/sh to #!/bin/bash

3. if [ $transaction == ” ] complains about unitary operator needed. this is because if $transaction is empty string, it will vanish completely and leave the interpreter looking for another input parameter. use “” to wrap around $transaction will fix it.

4. on a similar note, spaces after [ and before ] for a condition are not optional. dash will complain about weird things while bash will tell you there is a missing ].

5. I don’t understand why most tutorials online says ‘if [] then … else … fi’ or ‘ if [] then … elif … else .. fi’ are acceptable if statements. In reality, for the latter case, it is actually ‘if [] then … elif [] then … else … fi’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

October 2010
M T W T F S S
« Sep   Nov »
 123
45678910
11121314151617
18192021222324
25262728293031

Twitter

  • is reflecting on the past three years and reading the article on divided self aptly showing up at the top of my tweeter feed. 2 months ago
  • is having a nice feeling of connecting dots all over the place. they are now illuminating the same point. 3 months ago
  • Style Transfer is fun! TensorFlow rocks! #WTM17 https://t.co/zYP0IFIDfp 5 months ago
  • couldn't get over the jetlag, sleeping during the day from seven to four, for days. 7 months ago
  • is emptying trash and happily discovering the available disk space now ranks at 100G+. 8 months ago

Flickr Photos

%d bloggers like this: