How to debug and/or profile shell scripts
2005-07-15
I’ve been following the git and cognito projects since almost the beginning. (Yes, only 3 months, but it’s true!) Both projects use shell scripts for at least some of their functionality and the mailing lists have a decent amount of discussion about efficiency. I didn’t know how to go about looking for bottlenecks so I just let others worry about it. However, it was recently pointed out that there is an easy way to get a qualitative feeling for what script commands take the most time.
Simply use ‘sh -x’ before the name of a shell script and each line of
the script will be printed just before it is executed. That means that
fast commands will fly by and slow commands will pause so you can see
what they are. It’s a great way to get a feeling for your code and
hopefully allow you to make it faster. It could also be useful to find
what line is causing a crash while debugging.
I just tried this with ‘sh -x cg-update’ and watched as cogito updated
my repository.
By the way, my last post about git and cogito described what I had
to do to get them to compile and run. Many of those issues have been
worked out now, so all you do is download and unpack the tarball,
‘make’, ‘make install’, and add ‘~/bin’ to your path. Much easier.