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.

?Download zap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
zap() {
	# Usage: zap [SIGNAL|-NUMERIC|shoot] <fgrep>
	SIGNAL=''
	case "$1" in
		shoot|-0) SIGNAL='SHOOT';;
		term|-15) SIGNAL='TERM';;
		kill|-9) SIGNAL='KILL';;
		hup|-1) SIGNAL='HUP';;
	esac
	if [ "$SIGNAL" != '' ]; then
		shift
	else
		SIGNAL='SHOOT'
	fi
	PROCS=`ps ax -eo pid,comm | fgrep "$1"`
	MODE=0
	for DATA in $PROCS; do
		if [ "$MODE" -eq 0 ]; then
			PID="$DATA"
			MODE=1
		else
			CMD="$DATA"
			MODE=0
			if [ "$SIGNAL" == 'SHOOT' ]; then
				echo -n "Shooting #$PID - $CMD..."
				kill $PID
				sleep 1
				kill -9 $PID
				echo "Shot"
			else
				echo -n "Killing #$PID - $CMD..."
				kill $SIGNAL $PID
				echo "Killed"
			fi
		fi
	done
}

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’:

1
zap gnome

To just kill-forcefully:

1
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:

?Download autorun.inf
1
2
3
4
[autorun] 
open=invoke.bat 
icon=autorun.ico,0
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.

?Download setup.bat
1
2
@ECHO OFF
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).

?Download invoke.bat
1
2
@ECHO OFF
"Setup.lnk"

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).

?Download .htaccess
1
2
3
4
5
6
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:

1
$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. http://mysite.com/users/edit/123123)

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

Missing PHP Functions: getopts()

December 23rd, 2008 11 comments

This function is intended as a complete replacement for the horribly crippled getopt() native PHP function and provide extended functionality.

The use of this library means lots of great command-line-fu becomes readily available without any unnecessary validation tediousness messing up the first half of your script.

To use simply add the (ahem) hash-bang to the top of your PHP script and make the script executable.

An example of a correct setup would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/php -qC
<?php
require('getopts.php');
$opts = getopts(array(
	'a' => array('switch' => 'a','type' => GETOPT_SWITCH),
	'b' => array('switch' => array('b','letterb'),'type' => GETOPT_SWITCH),
	'c' => array('switch' => 'c', 'type' => GETOPT_VAL, 'default' => 'defaultval'),
	'd' => array('switch' => 'd', 'type' => GETOPT_KEYVAL),
	'e' => array('switch' => 'e', 'type' => GETOPT_ACCUMULATE),
	'f' => array('switch' => 'f'),
),$_SERVER['argv']);
print_r($opts);
?>

In the above the script can now be run as:

./getopts_example.php -a -b

With the incoming options (in this case the switches ‘a’ and ‘b’) being set in the $opts variable.

Full documentation as well as extensive examples are included in the source which can be found below.

?Download getopts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/php -qC
<?php
	/**********************************************************************************
	* Coded by Matt Carter (M@ttCarter.net)                                           *
	***********************************************************************************
	* getOpts                                                                       *
	* Extended CLI mode option and switch handling                                    *
	*                                                                                 *
	**********************************************************************************/
/*
GetOpt Readme
+++++++++++++++
 
getOpt is a library to load commandline options in replacement for the horribly inflexible 'getopts' native php function. It can be invoked using the typical 'require', 'include' (or their varients) from any PHP scripts.
 
LEGAL STUFF
===========
This code is covered under the GPL with republishing permissions provided credit is given to the original author Matt Carter (M@ttCarter.com).
 
LATEST VERSIONS
===============
The latest version can be found on the McStuff website currently loadted at http://ttcarter.com/mcstuff or by contacting the author at M@ttCarter.com (yes thats an email address).
 
QUICK EXAMPLE
=============
#!/usr/bin/php -qC
<?php
require('getopts.php');
$opts = getopts(array(
	'a' => array('switch' => 'a','type' => GETOPT_SWITCH),
	'b' => array('switch' => array('b','letterb'),'type' => GETOPT_SWITCH),
	'c' => array('switch' => 'c', 'type' => GETOPT_VAL, 'default' => 'defaultval'),
	'd' => array('switch' => 'd', 'type' => GETOPT_KEYVAL),
	'e' => array('switch' => 'e', 'type' => GETOPT_ACCUMULATE),
	'f' => array('switch' => 'f'),
),$_SERVER['argv']);
?>
 
When used with the commandline:
>./PROGRAM.php -ab -c 15 -d key=val -e 1 --letterb -d key2=val2 -eeeeeee 2 3
 
Gives the $opt variable the following structure:
$opt = Array (
	[cmdline] => Array (
		[0] => 1
		[1] => 2
		[2] => 3
	)
	[a] => 1
	[b] => 1
	[c] => 15
	[d] => Array (
		[key] => val
		[key2] => val2
	)
	[e] => 8
	[f] => 0
)
 
Of course the above is a complex example showing off most of getopts functions all in one.
 
Types and there meanings
========================
 
GETOPT_SWITCH
	This is either 0 or 1. No matter how many times it is specified on the command line.
 
	>PROGRAM -c -c -c -cccc
	Gives:
	$opt['c'] = 1;
 
	>PROGRAM
	Gives:
	$opt['c'] = 0
 
GETOPT_ACCUMULATE
	Each time this switch is used its value increases.
 
	>PROGRAM -vvvv
	Gives:
	$opt['v'] = 4
 
GETOPT_VAL
	This expects a value after its specification.
 
	>PROGRAM -c 32
	Gives:
	$opt['c'] = 32
 
	Multiple times override each precursive invokation so:
 
	>PROGRAM -c 32 -c 10 -c 67
	Gives:
	$opt['c'] = 67
 
GETOPT_MULTIVAL
	The same format as GETOPT_VAL only this allows multiple values. All incomming variables are automatically formatted in an array no matter how few items are present.
 
	>PROGRAM -c 1 -c 2 -c 3
	Gives:
	$opt['c'] = array(1,2,3)
 
	>PROGRAM -c 1
	Gives:
	$opt['c'] = array(1)
 
	>PROGRAM
	Gives:
	$opt['c'] = array()
 
GETOPT_KEYVAL
	Allows for key=value specifications.
 
	>PROGRAM -c key=val -c key2=val2 -c key3=val3 -c key3=val4
	Gives:
	$opt['c'] = array('key1' => 'val2','key2' => 'val2','key3' => array('val3','val4');
 
*/
 
define('GETOPT_NOTSWITCH',0); // Internal use only
define('GETOPT_SWITCH',1);
define('GETOPT_ACCUMULATE',2);
define('GETOPT_VAL',3);
define('GETOPT_MULTIVAL',4);
define('GETOPT_KEYVAL',5);
 
/**
* @param array $options The getOpts specification. See the documentation for more details
* @param string|array $fromarr Either a command line of switches or the array structure to take options from. If omitted $_SERVER['argv'] is used
* @return array Processed array of return values
*/
function getopts($options,$fromarr = null) {
	if ($fromarr === null)
		$fromarr = $_SERVER['argv'];
	elseif (!is_array($fromarr))
		$fromarr = explode(' ',$fromarr); // Split it into an array if someone passes anything other than an array
	$opts = array('cmdline' => array()); // Output options
	$optionslookup = array(); // Reverse lookup table mapping each possible option to its real $options key
	foreach ($options as $optitem => $props) { // Default all options
		if (!isset($props['type'])) { // User didnt specify type...
				$options[$optitem]['type'] = GETOPT_SWITCH; // Default to switch
				$props['type'] = GETOPT_SWITCH; // And again because we're not using pointers here
		}
		switch ($props['type']) {
				case GETOPT_VAL:
					if (isset($props['default'])) {
						$opts[$optitem] = $props['default'];
						break;
					} // else fallthough...
				case GETOPT_ACCUMULATE:
				case GETOPT_SWITCH:
					$opts[$optitem] = 0;
					break;
				case GETOPT_MULTIVAL:
				case GETOPT_KEYVAL:
					$opts[$optitem] = array();
		}
		if (is_array($props['switch'])) { // Create the $optionslookup var from an array of aliases
				foreach ($props['switch'] as $switchalias)
					$optionslookup[$switchalias] = $optitem;
		} else { // Create the $optionslookup ref as a simple pointer to the hash
			$optionslookup[$props['switch']] = $optitem;
		}
	}
 
	$inswitch = GETOPT_NOTSWITCH;
	for ($i = 1; $i < count($fromarr); $i++) {
		switch ($inswitch) {
			case GETOPT_MULTIVAL:
			case GETOPT_VAL:
				if (substr($fromarr[$i],0,1) == '-') // Throw error if the user tries to simply set another switch while the last one is still 'open'
					die("The option '{$fromarr[$i]}' needs a value.\n");
				GETOPT_setval($opts,$options,$inswitch_key,$fromarr[$i]);
				$inswitch = GETOPT_NOTSWITCH; // Reset the reader to carry on reading normal stuff
				break;
			case GETOPT_KEYVAL: // Yes, the awkward one.
				if (substr($fromarr[$i],0,1) == '-') // Throw error if the user tries to simply set another switch while the last one is still 'open'
					die("The option '{$fromarr[$i]}' needs a value.\n");
				$fromarr[$i] = strtr($fromarr[$i],':','='); // Replace all ':' with '=' (keeping things simple and fast.
				if (strpos($fromarr[$i],'=') === false)
					die("The option '$inswitch_userkey' needs a key-value pair. E.g. '-$inswitch_userkey option=value'");
				GETOPT_setval($opts,$options,$inswitch_key,explode('=',$fromarr[$i]));
				$inswitch = GETOPT_NOTSWITCH; // Reset the reader to carry on reading normal stuff
				break;
			case GETOPT_NOTSWITCH: // General invokation of no previously complex cmdline options (i.e. i have no idea what to expect next)
				if (substr($fromarr[$i],0,1) == '-') {
					// Probably the start of a switch
					if ((strlen($fromarr[$i]) == 2) || (substr($fromarr[$i],0,2) == '--')) { // Single switch OR long opt (might be a weird thing like VAL, MULTIVAL etc.)
							$userkey = ltrim($fromarr[$i],'-');
							if (!isset($optionslookup[$userkey]))
									die("Unknown option '-$userkey'\n");
								$hashkey = $optionslookup[$userkey]; // Replace with the REAL key
							if (($options[$hashkey]['type'] == GETOPT_SWITCH) || ($options[$hashkey]['type'] == GETOPT_ACCUMULATE)) {
								GETOPT_setval($opts,$options,$hashkey,1); // Simple enough - Single option specified in switch that needs no params.
							} else { // OK the option needs a value. This is where the fun begins
								$inswitch = $options[$hashkey]['type']; // Set so the next process cycle will pick it up
								$inswitch_key = $hashkey;
								$inswitch_userkey = $userkey;
							}
					} else {
						// Multiple letters. Probably a bundling
						for ($o = 1; $o < strlen($fromarr[$i]); $o++) {
							$hashkey = substr($fromarr[$i],$o,1);
							if (!isset($optionslookup[$hashkey]))
									die("Unknown option '-$hashkey'\n");
							if (($options[$optionslookup[$hashkey]]['type'] != GETOPT_SWITCH) && ($options[$optionslookup[$hashkey]]['type'] != GETOPT_ACCUMULATE))
								die("Option '-$hashkey' requires a value.\n");
							GETOPT_setval($opts,$options,$optionslookup[$hashkey],1);
						}
					}
				} else {
					$opts['cmdline'][] = $fromarr[$i]; // Just detritus on the cmdline
				}
				break;
		}
	}
	return $opts;	
}
 
function GETOPT_setval(&$opts,&$options,$key,$value) {
	switch ($options[$key]['type']) {
		case GETOPT_VAL:
		case GETOPT_SWITCH:
			$opts[$key] = $value;
				break;
		case GETOPT_ACCUMULATE:
			$opts[$key]++;
				break;
		case GETOPT_MULTIVAL:
			$opts[$key][] = $value;
			break;
		case GETOPT_KEYVAL:
			$opts[$key][$value[0]] = $value[1];
	}
}
?>

If you do use this script, please take a moment to add any comments or suggestions below.

Categories: PHP Tags: ,

Missing PHP Functions: array_totree()

December 13th, 2008 1 comment
array_totree( $array, $seperator = "/" )

Splits a given array into a tree formed hash array.

This is useful when a large array of strings needs splitting into a hierarchical array structure.

For example in incoming array structure

1
2
3
4
5
6
7
$a = array(
	'a/a/a',
	'a/b/b',
	'a/b/c',
	'a/c/c',
	'c/a/a'
);

Is returned as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
array(
	"a" => array(
		"a" => array(
			"a" => array()
		),
	),
	"b" => array(
		"b" => array(),
		"c" => array()
	),
	"c" => array(
		"c" => array()
	),
	"c" => array(
		"a" => array(
			"a" => array()
		)
	)
);

The function:

1
2
3
4
5
6
7
8
9
10
11
12
13
function array_totree($in, $splitchar = '/') {
        $out = array();
        foreach ($in as $item) {
                $bits = explode($splitchar,$item);
                $outref =& $out;
                foreach ($bits as $bit) {
                        if (!isset($outref[$bit]))
                                $outref[$bit] = array();
                        $outref =& $outref[$bit];
                }
        }
        return $out;
}
Categories: PHP Tags: