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
  • 13:29 (UTC +10:00)
View GitHub Profile
@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 / foo
Last active November 8, 2022 16:49
There's no excuse for using #!/usr/bin/env ...
test
@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 / 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).
@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 / xr
Last active November 25, 2021 03:45
Script using "xrandr" to configure multi-head display
#!/bin/bash
die() { echo "$*" >&2 ; exit 1 ; }
declare -a R=( normal right inverted left ) # rotation descriptions
declare -A S
declare -A DX
declare -a EX
@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 / 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;