Tim's blag

To content | To menu | To search

Sunday 28 October 2012

ZSH/Bash string manipulation

Both ZSH and Bash have some built-in string manipulations that can be useful at times.


Given a test string:

word='hello - world - how - are - you'

Take elements (letters) 5 through 10

$word[5,10] == 'o - wo' # for zsh
${word:5:5} == '- wo'   # for bash

N.B.: in bash the first character is at index 0, in zsh this is at index 1!

Substring matching

To match a string with a substring, we can use:

  • ${str#substr} Deletes shortest match of $substr from front $str
  • ${str##substr} Deletes longest match of $substr from front $str

for example: take part before 'how*', or o*

${word%how*} == 'hello - world - '
${word%o*} == 'hello - world - how - are - y'
${word%%o*} == 'hell'

Or take the part after '* - '

${word#* - } = 'world - how - are - you'
${word##* - } = 'you'

Explode strings

To explode or split strings into array, we can use several zsh functions:

To split by ' - ', take 3rd element

${word[(ws: - :)3]} == 'how' # only zsh

Split by ' - ', take second element, capitalize

${(C)${word[(ws: - :)2]}} == 'World' # only zsh

Split by word, take first word

$word[(w)1] == 'hello' # only zsh

File rename examples

Get the file extension:

${file##*\.} == jpg
echo ${file##*\.}

Get the file base name:

${file%\.*} == helloworld.multiple.dots
echo ${file%\.*}


Sunday 14 October 2012

Utrecht University Solis VPN on Mac OS X Lion 10.7.*

Utrecht University offers a VPN service called Solis-VPN. This works fine under Windows using the Cisco Client, but this client is not available for newer (10.6+) version of Mac OS X that run a 64-bit kernel by default.

Update: Cisco released the 'Cisco Anyconnect' VPN client available that works under OS X 10.7.* (Lion) as well. It is a Java client which Solis-VPN users can download at https://solisvpn.uu.nl. Unfortunately it seems, this client only routes traffic over the VPN that is on the target network, there is no option to route all traffic over the VPN. Therefore, you cannot route your web traffic over the VPN to download papers from journals that only allow access from a university network.

Although OS X Lion has a built-in VPN client that should support the Cisco protocol, this did not work in my case with the UU Solis VPN for some reason. I finally got it to work using vpnc. The following steps should help you set up the VPN client as well:

  1. Download and install Xcode and the Developer Tools. The latter can be installed at the Downloads section of Xcode's preferences.
  2. Install Macports, a package manager for OS X
  3. Install vpnc
  4. Configure tuntaposx
  5. Configure vpnc
  6. Connect and disconnect the VPN

This worked for me on Mac OS X 10.7.5 with vpnc version 0.5.3 and tuntaposx version 20111101.

Step 1 and 2 are documented in abundant details elsewhere, so I will skip these here.

Install vpnc (step 3)

Install vpnc with the hybrid_cert option in case you might need it. For UU Solis VPN this is not required.

sudo port install vpnc +hybrid_cert

Configure tuntaposx (step 4)

To set up the network connection, vpnc uses tuntaposx. This is automatically installed once you install vpnc, but it's not configured properly for some reason. To fix this, run

sudo rsync -av /opt/local/Library/Extensions/* /Library/Extensions/
sudo rsync -av /opt/local/Library/StartupItems/* /Library/StartupItems/

which will copy the Kernel extension bundles to the proper locations and load them at startup.

Configure vpnc (step 5)

To connect to your favourite VPN server, create a configuration file in /opt/local/etc/vpnc/. For Solis VPN, you need these settings:

IPSec gateway
IPSec ID everyone
IPSec secret rlakluxiupoeqlurlaqo
IKE Authmode psk
Xauth username <SOLISID>
Xauth password <PASSWD>
Enable Single DES
DPD idle timeout (our side) 0
Local Port 0

Note that you can write your password here such that vpnc will connect automatically, but I left it empty (just delete the Xauth password line) such that vpnc asks for my password at startup.

Also, IPSec secret is a group password that Cisco VPN uses to authenticate the user to some group, or something. This is the decoded version, obtained with the Cisco vpnclient password decoder. For Solis VPN, the encoded password is:


Connect and disconnect (step 6)

To run vpnc, simply type

sudo vpnc --natt-mode cisco-udp solis-vpn

and vpnc will connect to the VPN. To disconnect, run

sudo vpnc-disconnect

and you are again on your own.


Monday 20 August 2012

OpenProj export as PNG/SVG

OpenProj is one of the few project management software programs available on the Mac platform, intending to replace Microsoft Project. Unfortunately it is impossible to easily export schemes as PDF for use in publications. OpenProj was forked to ProjectLibre which promises to release a new and improved version in August 2012.

Until that time, there are a few people who wrote patches for OpenProj so you can fix it yourself. openprojext adds an 'export to PNG' feature for OpenProj, and Eduardo Suarez-Santana extended this patch to also allow 'export as SVG', which subsequently allows conversion to PDF. The latter patch worked fine for me so I put up a patched version of OpenProj 1.4 online. It is patched exactly according to the instruction on Eduardo's website, using Batik version 1.7 and XML Commons External version 1.3.05. To start it, run

 java -jar openproj.jar

There is also an even more extended patch for OpenProj called Angelfalls, but I did not get this running easily and export to SVG was all I needed anyway.

Thursday 17 November 2011

OSX launchd and launchctl issues

I recently re-installed Lion on my MBP and ran into the problem that by default ssh-agent remembers my private key indefinitely. I wanted to reduce this to something sane and found the file /System/Library/LaunchAgents/org.openbsd.ssh-agent.plist which controls ssh-agent's behaviour. I started tweaking stuff with launchctl, first broke my ssh-agent altogether and after a while figured out how to tweak it to my needs.

After initially breaking my ssh-agent because I copy/pasted commands that I didn't really understand, I found the following apt quote:

A good rule for rocket experimenters to follow is this: always assume that it will explodeAstronautics, issue 38, October 1937 (source)

Fortunately I managed to fix it and get some basic understandign of launcd on OSX. This information seemed to be a bit fragmented, so here are my findings.

Continue reading...

Monday 7 November 2011

Analyse written text

Earlier I wrote a simple script that checks a text for structural integrity, making sure there are no words close together etc. I converted this script to Python, dubbed it metacheck.py and I present the new and improved version here!

Continue reading...

Python introduction 101

I recently gave a introductory presentation to Python to some colleagues. The topics covered include the basic language syntax of Python, available data types, some words about memory management and some sightly more advanced Python features like list comprehension and array slicing. Click further to read more details.

Continue reading...

Tuesday 28 June 2011

Change Airport & Time Machine with Applescript - update

Earlier I wrote a bit about changing mouse preferences on your Mac. I added some other settings which might change from location to location using Applescript, such as Airport and Time Machine backup disk.

Continue reading...

Sunday 26 June 2011

Analyse word distance in text

Update: I wrote an improved version in Python, read more here.

When I was writing my thesis a few years ago, I wrote a script to find the word distance for each word, and signal words that are close together. I used this to find obvious flaws in texts without having to read it over every time. Especially if you are revising a text a lot, this might be useful to see if you need to use a thesaurus somewhere.

Continue reading...

Tuesday 21 June 2011

Multiple mouse preferences

If you're using multiple mouses with your Apple as I am (work and home), it might annoy you that the mouse speeds are different for each mouse. To mitigate these problems to some extent, you can use Steermouse to configure your mouse and then use the Applescript snippet on their website to make shortcuts for your specific mouse settings in an Applescript file.

Currently I'm using the following settings:

tell application "SteerMouse"
  set text "cursor tracking speed" to 0.025
  set text "cursor sensitivity" to 800
end tell

Related info: