[net.sources] shell printf - a sensible solution

johnr@metro.oz (John Andrew Rosauer) (03/16/87)

Below is a shell printf NOT written in C.
People should spend a few minutes thinking to see if something can be
done using available utilities rather than immediately writing
thousands of lines of C. It's wasting someone's money.
###########################################################
if [ $# -lt 1 ]
then
	echo >&2 "Usage: printf format [ arg ...]"
	exit 1
fi
x="\"$1\""
shift
for i
do
	x="$x,\"$i\""
done
awk "BEGIN { printf $x; exit }"

dm@bfly-vax.bbn.com (03/18/87)

John Andrew Rosauer <johnr%metro.oz@bbn.com>, writes:

>Below is a shell printf NOT written in C.
>People should spend a few minutes thinking to see if something can be
>done using available utilities rather than immediately writing
>thousands of lines of C. It's wasting someone's money.

>	(shell file that uses awk)

Having spent that time thinking, one should realize that, like echo,
printf should be faster than starting up awk.  To do otherwise is
wasting someone's time.

Look, ``the UNIX philosophy'' does NOT mean bending existing utilities
to do poorly what a simple C program can do well.  It is perfectly
fine to prototype the function one wants using the suite of tools that
UNIX comes packaged with.  However, once you've done that prototype,
if you're going to make extensive use of it, translating a shell-file
into C is a perfectly acceptable optimization.

mwm@eris.BERKELEY.EDU (Mike (No one lives forever.) Meyer) (03/18/87)

In article <5008@brl-adm.ARPA> dm@bfly-vax.bbn.com writes:
>Look, ``the UNIX philosophy'' does NOT mean bending existing utilities
>to do poorly what a simple C program can do well.  It is perfectly
>fine to prototype the function one wants using the suite of tools that
>UNIX comes packaged with.  However, once you've done that prototype,
>if you're going to make extensive use of it, translating a shell-file
>into C is a perfectly acceptable optimization.

Quite correct. This is the path Berkeley took to get the ls they've
got now. Other tools have gone that route. It should be noted that
things that are really heavily used get moved into the kernel, but not
at Berkeley (well, not very often).

	<mike
--
But I'll survive, no you won't catch me,		Mike Meyer
I'll resist the urge that is tempting me,		ucbvax!mwm
I'll avert my eyes, keep you off my knee,		mwm@berkeley.edu
But it feels so good when you talk to me.		mwm@ucbjade.BITNET

dce@mips.UUCP (David Elliott) (03/19/87)

In article <5008@brl-adm.ARPA> dm@bfly-vax.bbn.com writes:
>
>John Andrew Rosauer <johnr%metro.oz@bbn.com>, writes:
>
>>People should spend a few minutes thinking to see if something can be
>>done using available utilities rather than immediately writing
>>thousands of lines of C. It's wasting someone's money.
>
>Having spent that time thinking, one should realize that, like echo,
>printf should be faster than starting up awk.  To do otherwise is
>wasting someone's time.

With machine speed increasing all of the time, this argument is
becoming obsolete. Until we all have 20% of a 5 mips machine, it's
still valid. Then again, how much of a speed difference are you
talking about? Did you benchmark it? How often do you use printf?
On my system, the command

	time awk 'BEGIN {printf "hello, world\n" ; exit}'

gives 0 for all times.

>Look, ``the UNIX philosophy'' does NOT mean bending existing utilities
>to do poorly what a simple C program can do well.  It is perfectly
>fine to prototype the function one wants using the suite of tools that
>UNIX comes packaged with.  However, once you've done that prototype,

On the other hand, there is another type of waste involved, and there
is an issue of portability.

An object file certainly takes up more disk space than a shell script
that uses awk, and it costs to have the source and the makefile and
the manual page. It also costs maintenance. If I were to add it to
my system product, it would cost in other ways, such as software
manufacturing costs.

As for portability, I'm not talking about whether or not the printf
command is portable, but whether or not I can expect it to always
be available. There was a case last year in which our local version
of the BSD 'install' command used the local printf command (yes, it's
been written a number of times), and I was pressured to add it to
our system. I did, and the command worked fine. Later, when we
installed it on a clean machine, it was found to be broken.
-- 
			David Elliott

UUCP: 	{decvax,ucbvax,ihnp4}!decwrl!mips!dce, DDD:  	408-720-1700

madd@bucsb.bu.edu.UUCP (Jim "Jack" Frost) (03/19/87)

In article <377@metro.oz> johnr@metro.su.oz (John Andrew Rosauer) writes:
>Below is a shell printf NOT written in C.
>People should spend a few minutes thinking to see if something can be
>done using available utilities rather than immediately writing
>thousands of lines of C. It's wasting someone's money.
[deleted shell printf not written in C]

Right.  You might want to use that in a quickie shell script, but I've
found that shell scripts can be annoyingly slow, and use a lot of
resources.  If the C program can do it in 1/3 the time, the time spent
developing the C program can quickly be made up for.  After all, time
is money.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                   - Jim Frost * The Madd Hacker -
UUCP:  ..!harvard!bu-cs!bucsb!madd | ARPANET: madd@bucsb.bu.edu
CSNET: madd%bucsb@bu-cs            | BITNET:  cscc71c@bostonu
-----------------------------------+-----+------------------------------
"New minds, fresh ideas -- be tolerant." | _@o.  <- Mick the Micro Mouse