Skip to content

Instantly share code, notes, and snippets.

View kurahaupo's full-sized avatar
🏠
Working from home

Martin Kealey kurahaupo

🏠
Working from home
  • Brisbane, Australia
  • 06:42 (UTC +10:00)
View GitHub Profile
@kurahaupo
kurahaupo / example of bash aliases inside functions
Last active August 29, 2015 14:06
bash aliases inside functions
$ alias randomly='if ((RANDOM%2)) ; then'
$ dorand() { randomly print Hi ; fi ; }
$ type dorand
dorand is a function
dorand ()
{
if ((RANDOM%2)); then
print Hi;
fi
}
@kurahaupo
kurahaupo / twelve-days-of-christmas.bash
Last active August 29, 2015 14:07
twelve days of christmas, in bash
#!/path/to/your/bash
dox=( [1]='a partridge in a pair tree'
'2 turtle doves, and'
'3 french hens'
'4 calling birds'
$'5 gold rings\n ...'
'6 geese a-laying'
'7 swans a-swimming'
'8 maids a-milking'
@kurahaupo
kurahaupo / chresolvconf
Created January 14, 2015 02:00
Manually change /etc/resolv.conf but protect it from meddling by NetworkManager et al
#!/bin/bash
# Update /etc/resolv.conf but keep it unwritable, even by root-owned processes.
# Useful to stop WiFi and/or DHCP from messing with it, and quite essential when
# there are multiple managers for different interface types.
#
# Because symlinks don't work, instead we bind-mount an appropriate file onto
# /etc/resolv.conf, chosen from those in ~/.resolvconf.d/.
#
# Since BIND is running locally, almost all the time that will work, so it's the
@kurahaupo
kurahaupo / readkey
Last active December 28, 2015 10:59
How to read ANSI keyboard sequences in Bash
readkey() {
local char
IFS= \
read -rs -d '' -N1 key || return $? # use -n1 on older versions
while
case "$key" in
('') key=$'\n' ; break ;; # compensate for bug
( $'\e[M'??? ) break ;; # mouse report
( $'\e' | $'\e[' | $'\e['[?O] | $'\e['*[\;0-9] | $'\e[M'* | $'\eO' ) ;;
( [$'\xc0'-\$'xfe'] \
@kurahaupo
kurahaupo / siginfo
Last active December 29, 2015 12:19
Show signal names from Sig* lines in Linux's /proc/$PID/status
#!/bin/bash
pid=${1:-$PPID}
signals="$(trap -l)"
declare -A SIG
for s in ${signals//') '/=}
do
i=${s%%=*}
@kurahaupo
kurahaupo / wideascii
Created January 3, 2014 00:07
Convert normal ascii to equivalent double-width unicode characters
#!/usr/bin/perl
use utf8;
binmode STDOUT, ":utf8";
while (<>) {
s/ / /g;
s/\t/\t\t/g;
s/[!-~]/chr(ord($&)+0xff00-0x20)/xge;
print $_;
@kurahaupo
kurahaupo / xterm-colourtest
Created January 10, 2016 07:29
Xterm Colour test pattern
#!/usr/bin/perl
use 5.008;
use strict;
use warnings;
use Getopt::Long qw(:config bundling);
use POSIX qw( floor );
use utf8;
#!/bin/bash
shopt -s globstar
sdir=$HOME/Documents/website
tdir=$HOME/tmp/transcoded-website
scode=WINDOWS-1252
tcode=UTF-8
mkdir -vp "$tdir"
#define BUfSIZ*pick a number
, add #includes, and ADD ERROR HANDLING */
int main(){
char b[N]="/tmp/.spongeXXXXXX";
char*t=mktemp(b);
int f=open(t,O_RDWR|O_CREAT|O_EXCL,0);
unlink(t);
ssize_t n;
while((n=read(0,b,N))>0)
@kurahaupo
kurahaupo / shorten-symlinks
Created January 7, 2014 23:24
Convert absolute symbolic links (those whose target starts with /) to be relative (to their containing directory). Written for Linux, but will work anywhere that has `readlink -m` and `ln -T` (the GNU versions do).