Basic tips for scripting with sh(1) and ksh(1) on OpenBSD

Historically, /bin/sh was the Bourne shell. However, OpenBSD's /bin/sh is the same program as /bin/ksh, so in reality any script that works with one will work with the other. If you want to write a more portable script, invoke /bin/sh and don't use any ksh(1) features not found in sh(1) (see the man pages for details.)

Check if a directory doesn't exist:

if [ ! -d some-directory ]
	echo does not exist

The interesting part here is the '[', which is actually a program, /bin/[ (a hard link to /bin/test). It is this program, test(1), which takes the arguments '!' (negate), '-d' (true if directory exists), 'some-directory' and (if called as [) finally ']'.

Check if a command failed:

if ! mkdir -p /somepath
	echo mkdir failed

This works because every command has an 'exit status'—zero on success, nonzero on error. You can see the exit status of the last command you ran with echo $?.