Article View: comp.sources.d
Article #5011Re: v06i100: Count program - leading zero enhancement
From: Wayne Thompson
Date: Thu, 18 May 1989 19:34
Date: Thu, 18 May 1989 19:34
107 lines
2543 bytes
2543 bytes
In article <40035@bbn.COM> cosell@bbn.com (Bernie Cosell) writes:
I hate to be picky, but the script has a bit of a problem.
[deleted error cases]
On the other hand, here's a short perl script that also does the job.
[deleted perl script]
I never seem to learn not to publish hacks (someone might use them :-)).
Here's a production version. I like perl, but it limits the potential
usefulness of a script 'cause not everyone has it.
Wayne
#! /bin/sh
#
# @(#)num 1.0 (waynet@sun) 05/18/89
#
pathname=oculus:/HOME/waynet/bin/num
myname=`basename $0`
#
# Author:
# Wayne Thompson
#
# Description:
# This script will output a sequence of integers.
#
usage="usage: $myname [-wWIDTH] [firstn] lastn."
#
# Options:
# -wN field width of N, left zero padded.
#
# Files:
#
# Diagnostics:
#
# Dependencies:
#
# Bugs:
#
## >> BEGIN function: isnum >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ##
# Function:
# isnum
#
# Description:
# This function tests whether it's argument is numeric.
#
# Variables:
#
# Usage:
# if isnum 5
# then
# echo "true"
# fi
#
# Diagnostics:
#
isnum () {
expr ${1:-null} + 1 > /dev/null 2>&1
}
## << END function: isnum <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ##
case $1 in
-w*[0-9])
width=`expr substr $1 3 1`
shift
;;
esac
case $# in
1)
start=0
end=$1
;;
2)
start=$1
end=$2
;;
*)
echo >&2 "$usage"
exit 1
;;
esac
for i in $@
do
isnum $i || {
echo >&2 "$myname: $i: non-numeric argument."
echo >&2 "$usage"
exit 1
}
done
if [ $start -gt $end ]
then
echo >&2 "$myname: firstn greater than lastn."
echo >&2 "$usage"
exit 1
fi
awk '
BEGIN {
for (i = '$start'; i <= '$end'; i++) {
printf ("%0'$width'd\n", i)
}
}
' /dev/null
Technical Details
Message-ID:
<33801@mongo.uucp>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!sun-barr!sun!mongo!waynetReferences:
<54804@uunet.UU.NET> <1989May16.000313.8145@tmsoft.uucp> <33795@mongo.uucp> <40035@bbn.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!sun-barr!sun!mongo!waynet From: waynet@mongo.uucp (Wayne Thompson) Newsgroups: comp.sources.d Subject: Re: v06i100: Count program - leading zero enhancement Message-ID: <33801@mongo.uucp> Date: 18 May 89 19:34:21 GMT References: <54804@uunet.UU.NET> <1989May16.000313.8145@tmsoft.uucp> <33795@mongo.uucp> <40035@bbn.COM> Organization: Sun Microsystems, Inc. - Mtn View, CA Lines: 107 In-reply-to: cosell@bbn.com's message of 16 May 89 21:15:05 GMT