Public Domain version of: yes(1)
Steven Sargent
sarge at fraud.Berkeley.EDU
Sat Apr 9 03:57:15 AEST 1988
> If you look closely, you'll see that you are doing a comparision on each
>
> if [ "$#" -gt 1 ]
> then
> ans=$*
> else
> ans=y
> fi
> while true
> do
> echo $ans
> done
>
> Check what you want to echo ONCE ONLY, assign it and echo it.
Why not?
1. It's wrong -- doesn't implement the spec (4bsd manual page) in
two ways: it should only echo $1, not $*; also, you should use -ge,
not -gt.
2. Any puny performance advantage you might get from doing the test
only once is completely swamped by the exec penalty on true, once
per loop. Benchmark your version against
*** cut here
#!/bin/sh
while :; do
echo ${1-y}
done
*** end
Recall that ${1-y} is built into the shell. test may or may not be
(it isn't on 4bsd; is in V.3). : also is built-in. If you need more
complicated stuff than $ provides, "case" covers a lot of situations,
again without exec. The manual page gives much cryptic info here.
3. (careful, big words coming up) Your adding the spurious temporary
variable "ans" breaks an otherwise referentially transparent program;
the fact that doing so confers no compensating performance advantage
is all the worse.
> I have watched all of these varieties of yes(1) come whizzing by, and
> must wonder: Are you all all that bad?
"We all" may be hurling a lot of cement pies your way; maybe you
should leave town for awhile?
Steven "Trust but verify" Sargent.
---
"I'm sorry... you must have me confused with some other plate-lipped
white girl named Irene."
-- Good Girls #2
Steven Sargent
ARPA Internet: sarge at scam.berkeley.edu
MILnet: sarge%scam.berkeley.edu at ucbvax.berkeley.edu
TPCnet: {anywhere at all, really}!ucbvax!scam!sarge
More information about the Alt.sources
mailing list