Skip to content

Instantly share code, notes, and snippets.

@nottsknight
nottsknight / mycgi.py
Last active December 15, 2015 14:28
Python module providing a series of functions to generate various forms of HTML element, for use in CGI scripts. Will be expanded as and when I need more elements!
# UTILITY FUNCTIONS
def preamble():
"""Prints the mandatory opening lines of any CGI script."""
print "Content-Type: text/html"
print
def parseCss(css):
"""Convert a CSS class or id name into a string of the form class=var or id=var, with quotes around var.
String css -> css tag to convert
@nottsknight
nottsknight / escape-spaces.sh
Created February 2, 2013 17:08
BASH function to escape all ' ' characters in a string, e.g. for formatting a file name for use over scp
#!/usr/bin/env bash
function escapeSpaces () {
if [[ -n $1 ]]; then
string=`echo $1 | sed -r 's/[ ]+/\\\\\\ /g'`
echo "$string"
fi
}
@nottsknight
nottsknight / install-java.sh
Last active April 27, 2017 20:54
BASH script to install the Java Development Kit. User must download the JDK themselves, and then give the path to the downloaded .tar.gz archive as the only command-line argument.At the end of the script, a series of menus appear, presenting all the different places where the system can find the 'java' (or javac, etc.) command in a numbered list…
#!/usr/bin/env bash
# Abort if not super user
if [[ ! `whoami` = "root" ]]; then
echo "You must have administrative privileges to run this script"
echo "Try 'sudo ./install-java.sh'"
exit 1
fi
# Check that the file is a JDK archive
@nottsknight
nottsknight / FormatCurrency.java
Created December 28, 2012 11:51
Right-aligned currency formatting for Java
/**
* Method to format a BigDecimal for printing as an amount of currency. Only works for locales
* where the currency symbol is placed before the amount.
* @param n the value to format
* @returns the formatted String
*/
public String formatCurrency(BigDecimal n) {
String symbol = Currency.getInstance(Locale.getDefault()).getSymbol();
String value = new DecimalFormat("#,###.00").format(n);
@nottsknight
nottsknight / sshfs-mount.sh
Last active December 10, 2015 02:19
Short script for UNIX users to automatically connect to a remote file system via the sshfs program. Needs to be configured with the address of the server (line 4), your preferred mount point (line 5), a directory you can guarantee will be present IFF the file system is mounted (line 10), and the path to your home directory on the remote server (…
#!/usr/bin/env bash
# Script to mount or unmount a remote filesystem via sshfs
SERVERNAME="user@host.name"
MOUNTPOINT="$HOME/cs-home"
if [[ ! -d "$MOUNTPOINT" ]]; then
mkdir $MOUNTPOINT
fi
if [[ -d "$MOUNTPOINT/Private" ]]; then