While decrufting my homedir I found some scripts that might be of public* interest ­— either because of their amusement value, or their utility ;-)
Some are so tiny that you’d actually be better off just defining them as functions in your ~/.{ba,z}shrc .

*) For unixoid values of ‘public’.

groe

Open a file (argument 2) in joe (works with vim, too) with the cursor on the first line matching the regex in argument 1.

#!/bin/bash -eu
THEL=$(grep -m 1 -n ${1} ${2} | cut -d ':' -f 1)
[ -n "$THEL" ] && joe +$THEL $2

paserv

Set pulseaudio server (in X11, for display in $DISPLAY env var) to argument 1. Useful if you use several pulseaudio network audio servers. Without arguments, resets to default.

#!/bin/bash
if [ -n "${1}" ]; then pax11publish -e -S ${1}
else pax11publish -e -r; fi

gmrun

Wrapper launcher for the excellent “gmrun” program launcher. (queue “yo dawg” meme).
Raises and/or pulls any existing gmrun windows to your current desktop.

#!/bin/bash
/usr/bin/wmctrl -x -b add,raise -R "gmrun.Gmrun" || PATH="/home/boer/bin:$PATH" /usr/bin/gmrun

getx

Get an X11 authorization cookie for a display on some other host, over SSH.

#!/bin/bash
if [ -z ${1} ]; then
	echo "Usage: $(basename ${0}) sshstanza"
	echo ""
	exit 2
fi
 
SSH=${1}
XHO=${1##*@}
DIS=${2-":0"}
 
ssh ${SSH} "xauth list ${XHO}${DIS} | sort -u" | while read line; do xauth add ${line}; done

gaap

Ghetto power management. We don’t need no stinkin’ gnome-power-manager, we’ll just grep our keyboard/trackpad interrupt counter thankyouverymuch (I got that idea from reading the xscreensaver man page).
Comes in two parts.
is_user_alive:

#!/bin/bash -eu
#Succeeds if keystrokes or touchpad input has taken place since last time this was run, or if there is no record of the last run.
 
RECORD=${1:-/dev/shm/i8042_intcnt}
 
RETVAL="1"
 
upd_intcnt() {
  grep i8042 /proc/interrupts > ${RECORD}
}
 
is_alive() {
  grep i8042 /proc/interrupts | diff - ${RECORD} > /dev/null 2>&1 || RETVAL=0
}
 
[ ! -f ${RECORD} ] && upd_intcnt && exit 0
is_alive
upd_intcnt
exit $RETVAL

Run this one (gaap.sh) from cron:

#!/bin/bash -eu
 
misschien_slapen() {
  test -f /tmp/koffie || on_ac_power || echt_slapen
}
 
echt_slapen() {
  /usr/local/sbin/is_user_alive /dev/shm/gaap_alive || true
  echo -e "Hibernating in 30 secs, unless you prove you're alive by pressing\na key or touching the touchpad. Physically. On ${HOSTNAME}." | wall
  for sec in `seq 0 3 99`; do echo $sec; sleep 1; done | DISPLAY=":0" XAUTHORITY="/home/boer/.Xauthority" sudo -u boer zenity --progress --auto-close --text "hibernate?" || exit 0
  /usr/local/sbin/is_user_alive /dev/shm/gaap_alive || /usr/sbin/pm-hibernate
}
 
/usr/local/sbin/is_user_alive /dev/shm/gaap_alive || misschien_slapen

You might want to install zenity, and you’ll have to adjust the username. As you can see my laptop is a single-user system. The sudo is therefore a bit silly, but hey, it’s good practice to not present privilege escalation attack surfaces to yourself ;-)
I still need to find a good way of iterating over all X11 displays and finding the user who started the associated X server. Does anyone know of a not-too-hackish approach?

Aphorism slideshow


A friend of mine got married this summer and I got the newlyweds one of those digital photo frames. I preloaded it with a bunch (5.5K) of images generated from my unix fortune database. So it’s kind of a modern incarnation of those tiles that older Dutch generations used to cement onto their walls (Wikipedia entry [Dutch]). Terrible as they are, they’re making a (campy) comeback. Personally I can’t wait for the day that camp culture itself becomes camp (queue another “yo dawg” meme).


Anyway, I made myself this sed script, “str2cu.sed”:

/^%$/d
s/"\([^"]*\)"/“\1”/g

and then did this. Which, the better part of a year later, proves quite hard to reverse-engineer:

for CATG in art definitions education fightclub food hitchhiker humorists kids literature love medicine men-women news paradoxum pets platitudes politics science smac strangelove wisdom work; \
do mkdir /home/boer/tmp/fortunes/$CATG; cd /home/boer/tmp/fortunes/$CATG; \
csplit -q -z /usr/share/fortune/$CATG '/%/' '{*}' ; for f in /home/boer/tmp/fortunes/$CATG/*; \
do cat $f | sed -f /home/boer/tmp/fortunes/str2cu.sed | fmt -w 2500 | tr -s '\t' | expand -t 2 > /tmp/fortune; \
mv /tmp/fortune $f; done; find /home/boer/tmp/fortunes/$CATG/ -type f -size +200c -exec rm '{}' \; ; \
for f in /home/boer/tmp/fortunes/$CATG/*; do convert -background pink -fill white -size 480x234 \
-stroke black -gravity Center -font Essays1743-Bold caption:@$f $f.jpg; done; done

GNU textutils are fun… but holy cow. What an obtuse kludge.
A simple python script could have done all the work up until the image generation (which is done with imagemagick, it has a nice auto text scaling feature, did you know?).


Tags: ,