seq on MAC OS X

I’ve got several scripts that uses ‘seq’ to egenrate a sequence of numbers. This could be backups, looping over files, etc. I’ve also come to like the simplicity of this tool over the years.

‘seq’ is not a part of OS X.

I stumbled – well actually while searching – over this post describing ‘jot‘.

Great – but I’d still like to use the old-style interface to ‘seq’ (actually only the middle part, which takes 2 arguments):

[fmr@ally ~]$ seq --help
Usage: seq [OPTION]... LAST
or:  seq [OPTION]... FIRST LAST
or:  seq [OPTION]... FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.

So I wrote this little bash-script which seems to do the work – at least for my use (just copy and save to a file called seq, and make it executable):

#!/bin/sh
# Fredrik Rodland
# dev_____AT____rodland.no
# http://rodland.no
# 20081004

MIN=$1
MAX=$2
PAD=$3
LENGTH=${#MAX}

if [ $PAD ]; then
     W="-w %0$LENGTH""d"
fi

let NMB_STEP=$MAX-$MIN+1
jot $W $NMB_STEP $MIN

I also added a (third, optional) padding argument, which pads all numbers to the same length.

Examples:

fmr@dixon ~]$ seq 8 12
8
9
10
11
12
[fmr@dixon ~]$ seq 8 12 p
08
09
10
11
12
[fmr@dixon ~]$ seq 98 104  p
098
099
100
101
102
103
104

Leave a Reply

Your email address will not be published.
Required fields are marked:*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>