Jot

When I first started working on AIX I had come from a BSD background and wanted jot for some scripts I needed to write. This is that script. FreeBSD and Mac OSX have jot in the default install, AIX and Linux do not. You will see jot come up in many of my scripts because it is so useful in counting forward and backward. The output of this script is purposely designed to be used as input in other scripts. It is a building block upon which many of my other scripts are based.

I realize this is not necessarily about Linux for my first Linux post, but I haven’t decided on having a Code category yet.

Update: I added a code category and have added this script to it.

#!/bin/bash
#   Copyright Jud Bishop 2003-07-28.
#   Released under the BSD License.
#   This copyright must be left intact.
#   I wanted an easy way to count on my
#   AIX box and there was none.
#   This is my remedy.
#
#
#   ./jot.sh will produce
#   1\n 2\n ...(the newlines just keep going)
#
#   ./jot.sh -e 10 will end at 10 and produce
#   1\n 2\n ... to 10\n
#
#   ./jot.sh -e 10 -b 2 will end at 10 and produce
#   1\n 3\n ... 9\n
#
#   ./jot.sh -s 2 -b 2 -e 10 will start at 2 count by 2 and end at 10.
#   2\n 4\n ... 10\n

OPT=
START=0
END=10
COUNTBY=1
X=0
WORD=
JOIN=FALSE
while getopts s:e:b:w:jr OPT
do
    case $OPT in
        s)  START=$OPTARG
            ;;
        e)  END=$OPTARG
            ;;
        b)  COUNTBY=$OPTARG
            ;;
        w)  WORD=$OPTARG
            ;;
        j)  JOIN=TRUE
            ;;
        r)  REVERSE=TRUE
            ;;
    #   \?) echo
        *)  echo "Usage:"
            echo "      jot -s 2 -b 2 -e 10"
            echo "          -s start"
            echo "          -b count by"
            echo "          -e end"
            exit 1
            ;;
    esac
                                                                                                                        61,2-5        Top
            ;;
    esac
done
shift `expr $OPTIND - 1`

if [ "$REVERSE" = "TRUE" ]
then
    X=${START}
    while test $X -ge $END
    do
        if [ "$WORD" = "" ]
        then
            echo $X
            X=`expr $X - $COUNTBY`
        elif [ "$JOIN" = "TRUE" ]
        then
            echo $WORD$X
            X=`expr $X - $COUNTBY`
        else
            echo $WORD
            X=`expr $X - $COUNTBY`
        fi
    done
else
    X=${START}
    while test $X -le $END
    do
        if [ "$WORD" = "" ]
            then
            echo $X
            X=`expr $X + $COUNTBY`
        elif [ "$JOIN" = "TRUE" ]
        then
            echo $WORD$X
            X=`expr $X + $COUNTBY`
        else
            echo $WORD
            X=`expr $X + $COUNTBY`
        fi
    done
fi
exit 0
This entry was posted in Code, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s