Archive for the ‘HowTo’s’ Category

Trash with Bash

March 4th, 2009 6 comments

One of the regrettably unavoidable aspects of the Unix shell like environments is the impedance between what a user says and what a user means. While this is present in all computing environments the sheer power of Unix based command lines make a potential mistake catastrophic.

Who amongst us has not at some point done something like:

> rm * .tmp


> rm *>tmp

The former makes the mistake of a space between the dot and the ‘tmp’ part and the latter the accidental holding down of the shift key while pressing the intended full stop.

While searching for an alternative for the I-have-done-this-too-many-times-and-now-its-embarrassing approach of nuking everything with the ‘rm’ command its time i did something about it.

Inserting the following in your ~/.bashrc file will remap the ‘rm’ command to a slightly safer move-to-trash like behavior. Its not comprehensive but it has saved my life on more than one occasion.

# Trash support by Matt Carter /dev/null

Now the command ‘rm’ moves files into ~/.trashcan. You can also use the command ‘rm!’ when you really mean delete immediately and the utility command ’emptytrash’ to clean everything out.

Categories: HowTo's Tags: ,

Zapping processes in Bash

January 24th, 2009 No comments

The following is a handy little script I wrote which politely asks a collection of matching processes to exit. After a second the process is forcibly killed.

This command is designed as a drop-in replacement for the slightly cryptic pgrep and pkill commands.

zap() {
	# Usage: zap [SIGNAL|-NUMERIC|shoot] 
	case "$1" in
		shoot|-0) SIGNAL='SHOOT';;
		term|-15) SIGNAL='TERM';;
		kill|-9) SIGNAL='KILL';;
		hup|-1) SIGNAL='HUP';;
	if [ "$SIGNAL" != '' ]; then
	PROCS=`ps ax -eo pid,comm | fgrep "$1"`
	for DATA in $PROCS; do
		if [ "$MODE" -eq 0 ]; then
			if [ "$SIGNAL" == 'SHOOT' ]; then
				echo -n "Shooting #$PID - $CMD..."
				kill $PID
				sleep 1
				kill -9 $PID
				echo "Shot"
				echo -n "Killing #$PID - $CMD..."
				kill $SIGNAL $PID
				echo "Killed"

To install it simply dump the above text inside your existing ~/.bashrc file.

Usage is quite simple:

To politely kill all processes (then force-killing if it still does not die) containing the string ‘gnome’:

zap gnome

To just kill-forcefully:

zap -9 gnome
Categories: HowTo's Tags:

Autorun as Administrator in Vista

January 18th, 2009 3 comments

When asked how to automate the installation of software the other day I was a little dismayed that no-one seemed to have covered the topic of auto-running a batch file from a USB disk as an administrative user.

Admin level access is obviously required in order to install software without relentless ‘Program X requires administrative access’ UAC prompts.

Like most things in Vista the solution is exceptionally stupid and requires significant trips with Google to rather obscure second-hand Microsoft knowledge base articles.

The overview is something like this:

Autorun as admin process diagram

First create the autorun.inf file:

label=Setup computer from USB disk

So this file simply sets up the autorun.inf file which contains exactly what to list in the Vista ‘Autoplay’ dialog when the USB disk is inserted. You will probaly want to rename the ‘Label’ line and perhaps put a pretty icon on your disk called ‘autorun.ico’.

Now create your actual batch file that you wish to run as admin and call it ‘setup.bat’.

As a small note of caution. For some reason Vista does not change to this files active working directory when executing. I can really think of no reason this behavior exists so you might want to add the following rather cryptic command as the first in the batch file. This changes the active directory into the current batch file (in this case ‘setup.bat’) directory.

cd /d %~dp0

There obvious when you think about it isn’t it. Shame on you for not knowing about the handy and invisible shell variable “~dp0”. I am sure there is a long and interesting story about why this variable is so called and why it doesnt show up with the rest of things when you type ‘set’. This is why I switched to Linux some time ago.

Anyway, now we’ve done that create a shortcut to the ‘Setup.bat’ file and rename it to ‘setup.lnk’ (remember that the ‘.lnk’ bit will always be invisible to you anyway so when you rename in explorer all you will see is the ‘setup’ bit sans extension).

Open the shortcuts properties and hit the ‘Advanced’ button. Tick the ‘Run as Administrator’ box.

Finally, lets join it all together by creating the ‘Invoke.bat’ file which runs the shortcut (which will in turn run the ‘setup.bat’ program after asking via UAC for administrative permission).


Now save everything and remove you disk (safely) and re-insert. You will now be able to run the ‘setup.bat’ file which should contain all your auto-install scripts after a UAC admin prompt. Yey.

Categories: HowTo's Tags: , ,

Pretty URLS in CodeIgniter

January 12th, 2009 No comments

CodeIgniter has to be my favorite framework for PHP. The way it keeps out of your way while working to an MVC standard is something deserving of the highest praise.

For reasons passing understanding though the programmers of this excellent system have desided not to provide pretty URL’s out of the box. Getting infomation on this is a little tricky so here is my take on the situation.

Simply dump the following into a file called ‘.htaccess’ (Note the starting Dot) in the root path (thats the one at the very start of your path tree that should contain the ‘system’ folder).

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$		- [L]

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

As with most mod_rewrite hacks you will need that enabled on an apache server. Its very rare to find a hosting company that doesn’t use this setup so you should be fine.

You will also need to change the following variable to a blank value in the system/application/config/config.php file:

$config['index_page'] = '';

And you’re done. From now on your Urls are addressed in the form: http://SITE/CONTROLLER/METHOD/VALUE1/VALUE2 (e.g.

Categories: HowTo's, PHP Tags: , , ,

Templating in PHP in 10 seconds flat

December 6th, 2008 No comments

One of the rather messy practices that the PHP documentation actually encourages is the practice of using include or require to correctly layout a web page.

The PHP manual suggests that two pages be created: header and footer, and that these are included at the beginning and end of each document.

Thus for a ‘contact us’ page the contact.php file would look something like:

... The contact pages HTML ...

While this does work, there are easier ways to accomplish the same thing and still maintain compatibility with editors such as Dreamweaver which seems to choke on even a small amount of PHP.

The under-documented auto_prepend_value and auto_append_values get a brief mention in the ini directives documentation but I thought it would be nice to show an example of how these can be used in the wild,

Adding the following to either an existing ‘.htaccess’ (note the starting dot) or downloading the below file will do pretty much the same as the above contact.php file example did only without needing to specify the files in the beginning and ends of each separate file.

php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"

And as with all .htaccess directives you can override these on a directory basis. So if you need a different header and footer when inside the ‘invoices’ directory dump another copy of the above into that directory changing it as needed.

And yes this method can be merged with the previous post to work with the URL prettifier.

Categories: HowTo's, PHP Tags: , ,