[comp.unix.shell] KSH if-then v. [[ ]] && || notation and relative speed of equivalent command constructs

jbn35564@uxa.cso.uiuc.edu (J.B. Nicholson) (04/15/91)

In KSH, which is faster:

if [[ ${a} = 5 ]] then print 'a is set to 5'
else print 'a is not set to 5'

...or...

[[ ${a} = 5 ]] && print 'a is set to 5' || print 'a is not set to 5'

I'm leaning toward the latter only because it looks like KSH wouldn't have
to bother with the IF command (or the THEN command either).

And on the same note, does anyone have a list or some sort of a reference
to which commands are faster than their equivalent options (such as the
above comparison)?  If so, could you post it (if there's enough interest)
or mail it to me (if nobody else cares)?

Thanks.

Jeff
--
+----------------------------------------------------------------------------+
| "If you hear an onion ring - answer it."                    J.B. Nicholson |
+----------------------------------------------------------------------------+
| jeffo@uiuc.edu (Internet)              These opinions are mine, that's all.|
+----------------------------------------------------------------------------+

dws@margay.cs.wisc.edu (DaviD W. Sanderson) (04/17/91)

In article <1991Apr15.165925.26321@ux1.cso.uiuc.edu> jeffo@uiuc.edu writes:
>In KSH, which is faster:
>
>if [[ ${a} = 5 ]] then print 'a is set to 5'
>else print 'a is not set to 5'
>...or...
>[[ ${a} = 5 ]] && print 'a is set to 5' || print 'a is not set to 5'

I would guess these are about the same (I don't know for sure).
In this specific instance both are left in the dust by

	case $a
	{
	5)	print 'a is set to 5'
		;;
	*)	print 'a is not set to 5'
		;;
	}

A general rule is that it is fastest to use "case" when all you want to
do is pattern matching.  Note that the $a is implicitly quoted in the
case syntax, so it doesn't matter if it contains white space.  It is
superflous to use

	case "$a"

Other supposedly sh-compatible shells may not do this implicit quoting.
-- 
       ___
      / __\  U N S H I N E	           DaviD W. Sanderson
     |  |  | I N E			    dws@cs.wisc.edu
_____|  |  |_____  ________
\      / \   |__/ /////__	Fusion Powered Locomotives Made to Order
 \____/   \__|_/  \\\\\______	 (TARDIS model available at extra cost)