Solaris sans-seq

Tags

, , ,

Solaris, while a fairly decent OS, can be a bit of a pain if you try and write some cross-Unix compatible scripts.

One of the functions I tend to use quite a bit is ‘seq’, a sequence generator:

~$ seq 1 10
1
2
3
4
5
6
7
8
9
10
~$

I use it in a couple of my scripts where I need to chunk, or ‘split’ some files without ending with cruft:

~$ cat /tmp/mylongfile| sed "$( seq -f 'n;' 2 5| tr -d '\n' )G;"
line 1
line 2
line 3
line 4
line 5

line 6
line 7
line 8
line 9
line 10

line 11
~$

All fair and well, but, as mentioned at the top of this post; Solaris is sometimes a bit of a pain. In this case, in particular, since Solaris is sans-‘seq’.

This is my version of ‘seq.sh’ for Solaris, especially when you don’t feel the need of dragging your C compiler closer and re-implementing it in binary; or you feel that using Perl or Python is simply overdoing matters:

#!/usr/bin/env ksh

seq() {
    __seq_PARM="";

    if [ "$1" = "-f" -o "${1##-f}" != "$1" ]; then
        if [ "$1" = "-f" ]; then
            __seq_PARM=$2;
            shift;
        else
            __seq_PARM="${1##-f}";
        fi
        shift;
    fi

    typeset -i __seq_START;
    typeset -i __seq_END;

    if [ $# -eq 1 ]; then
        __seq_START=0;
        __seq_END=$1;
    elif [ $# -eq 2 ]; then
        __seq_START=$1;
        __seq_END=$2;
    else
        echo "seq [-f format] [first] last" >&2;
        return;
    fi

    [ "${__seq_PARM}" = "" ] && __seq_PARM="%d";

    for (( __seq_i=${__seq_START}; __seq_i<=${__seq_END}; __seq_i++ )); do
        printf "${__seq_PARM}\n" $__seq_i;
    done
    unset __seq_PARM __seq_START __seq_END __seq_i;
}

__seq_CURR=$( echo $_| sed 's/.*\*//' );
if [[ $__seq_CURR = $0 ]]; then
    if [ "$1" = "-?" -o "$1" = "-h" -o "$1" = "--help" -o $# -eq 0 ]; then
        __seq_HELP="[-f format] [first] last"
        __seq_SNAME=$(basename $0);
        __seq_HELPTXT="${__seq_SNAME} usage:\n\nRun ${__seq_SNAME} directly:\n    $0 [-h|--help] ${__seq_HELP}\n\n";
        __seq_HELPTXT=${__seq_HELPTXT}"Source ${__seq_SNAME}:\n    . $0;\n    seq ${__seq_HELP}\n\n";

        printf "${__seq_HELPTXT}" >&2;
        unset __seq_HELPTXT __seq_HELP __seq_SNAME;
    else
        seq $@;
    fi
fi
unset __seq_CURR;

One of the (kinda) cool things this does is check whether you’re sourcing the file (inside your own script so you can run the seq function from there afterwards) or whether you’re running it directly (from the command-line, for example). Running it with a ‘-h‘ will attempt to explain… :-)

~$ /usr/local/bin/seq.sh -h
seq.sh usage:

Run seq.sh directly:
    /usr/local/bin/seq.sh [-h|--help] [-f format] [first] last

Source seq.sh:
    . /usr/local/bin/seq.sh;
    seq [-f format] [first] last

~$

As always, do with this as you will: you break it, you get to keep both pieces.

Update: 2015-10-15

A mere couple of hours after publishing the previous I realised that Solaris’ KSH (88) does not support for (( )) constructs. Of course not.

This means I had to replace the for (( )) loop with a while [[ ]] loop:

__seq_i=${__seq_START}
while [[ ${__seq_i} -le ${__seq_END} ]]; do
     printf "${__seq_PARM}\n" "${__seq_i}";
     let __seq_i=${__seq_i}+1
done

This should run nearly as fast as the previous loop, but will run on KSH-88, KSH-93 and Bash as well.

The updated seq.sh script now looks like this:

#!/usr/bin/env ksh

seq()
{
    __seq_PARM="";

    if [ "$1" = "-f" -o "${1##-f}" != "$1" ]; then
        if [ "$1" = "-f" ]; then
            __seq_PARM=$2;
            shift;
        else
            __seq_PARM="${1##-f}";
        fi
        shift;
    fi

    typeset -i __seq_START;
    typeset -i __seq_END;

    if [ $# -eq 1 ]; then
        __seq_START=0;
        __seq_END=$1;
    elif [ $# -eq 2 ]; then
        __seq_START=$1;
        __seq_END=$2;
    else
        echo "seq [-f format] [first] last" >&2;
        return;
    fi

    [ "${__seq_PARM}" = "" ] && __seq_PARM="%d";

    __seq_i=${__seq_START}
    while [[ ${__seq_i} -le ${__seq_END} ]]; do
        printf "${__seq_PARM}\n" "${__seq_i}";
        let __seq_i=${__seq_i}+1
    done
    unset __seq_PARM __seq_START __seq_END __seq_i;

}

__seq_CURR=$( echo $_| sed 's/.*\*//' );
if [[ $__seq_CURR = $0 ]]; then
    if [ "$1" = "-?" -o "$1" = "-h" -o "$1" = "--help" -o $# -eq 0 ]; then
        __seq_HELP="[-f format] [first] last"
        __seq_SNAME=$(basename $0);
        __seq_HELPTXT="${__seq_SNAME} usage:\n\nRun ${__seq_SNAME} directly:\n    $0 [-h|--help] ${__seq_HELP}\n\n";
        __seq_HELPTXT=${__seq_HELPTXT}"Source ${__seq_SNAME}:\n    . $0;\n    seq ${__seq_HELP}\n\n";

        printf "${__seq_HELPTXT}" >&2;
        unset __seq_HELPTXT __seq_HELP __seq_SNAME;
    else
        seq $@;
    fi
fi
unset __seq_CURR;

Mooi Loop, Sir Terry…

Tags

, , , , ,

Sir Terry Pratchett has passed away1, and the world has thus lost one of it’s bright lights. A person who had managed to get more people to read than Enid Blyton or J.K. Rowling.

Maybe even combined…

Mooi Loop2, Sir Terry…

Terry Pratchett - Enemies

1: I kinda think Death will like to keep him around a while for conversation, though.
2: Afrikaans3 for “Go Well”.
3: My isiXhosa and isiZulu sucks, but that would have been “Hamba Kahle” in both of them…

Wallpaper Mavericks…

Tags

, , , ,

I have finally had enough of Mavericks’ shenanigans when it comes to Desktop Wallpapers…

For those not in the know, in OS X Mavericks, Apple has changed the mechanism for configuring Desktop Wallpaper from the venerable .plist to using a Sqlite3 database.

While using the database is probably more robust and easier to integrate, it is not as easy to change the desktop or, even more important from my perspective, to change all your desktops to the same wallpaper.

After some Googling and digging, I finally came up with the following little Python script which I called, reasonably enough, changewall:

#!/usr/bin/python

from __future__ import print_function

import os, sys, subprocess
import sqlite3

if len( sys.argv ) < 2:
    sys.exit( 'Usage: "%s" file-name' % sys.argv[ 0 ] )

if not os.path.exists( sys.argv[1] ):
    sys.exit( 'ERROR: File "%s" was not found!' % sys.argv[ 1 ] )

desktop_picture_path = sys.argv[ 1 ]
database_location = os.path.expanduser( '~/Library/Application Support/Dock/desktoppicture.db' )

conn = sqlite3.connect( database_location )
print( 'Opened database', end=", " )

cur = conn.cursor()
cur.execute( 'SELECT count( DISTINCT display_id ) + 1 FROM pictures;' )
displays = cur.fetchone()[0]

cur.execute( 'SELECT count( DISTINCT space_id ) + 1 FROM pictures;' )
spaces = cur.fetchone()[0]

conn.execute( 'DELETE FROM preferences' )

conn.execute( 'DELETE FROM data' )
conn.execute( 'INSERT INTO data VALUES (?)', ( desktop_picture_path, ))
conn.execute( 'VACUUM data' )

screens = spaces * displays
for screen in range( screens ):
    conn.execute( 'INSERT INTO preferences VALUES (1,1,?)', ( screen + 1, ))
    conn.execute( 'VACUUM preferences' )

conn.commit()

print( 'Records created successfully', end=", " )
conn.close()
print( 'Closed database' )

subprocess.check_call( ['/usr/bin/killall', 'Dock'] )

Not all that impressive, I’m fully aware, but it does seem to do the job. Using it is simplicity itself: changewall ~/Wallpaper/funky_wallpaper.jpg

As usual, do with it what you will…

Another Hero Goes to Rest

Tags

, , ,

John Holbrook Vance (28 August 1916 – 26 May 2013) was one of the people that made my youth (not to mention the long years I spent in the military) not only bearable but, at times great fun…

Now, while I am aware that no-one lives forever, it does at times feel like the universe is conspiring to remove all of my childhood heroes from our little rock in the last couple of years. Either that, or I’m getting older than I have realised.

The late, great, Jack Vance will be greatly missed by many. So: so long Jack; and I really think that it is time for me to revisit Lyonesse.

Xscreensaver Updated

Tags

, ,

The current Xscreensaver (5.18 as of this writing) no longer seems to need the Mac hackery I had mentioned in the linked article… JWZ seems to have added the required menu item to directly configure the relevant screen-savers…

New Year Blues

Tags

, , ,

It’s getting to that time of year again we we have to pause and reflect on our accomplishments (or lack thereof) during the year. Again.

A time to compare where we are, to where we should be, to where would like to be…

And as is usual for most of humanity, we will all most likely decide: Screw that, we’ll try again next year.

Merry Christmas to all of you, and a very Happy New Year.

Dennis Ritchie (1941-2011)

Tags

, ,

It is with sadness that I’ve heard that Dennis MacAlistair Ritchie, the noted computer scientist and co-inventor/creator of both the Unix OS and the C programming language has passed away.

This is a massive loss, since the man’s work is the very reason for my career.

So long, dmr, and maybe now would be a good time to join the demigodic party…