[SunHELP] dirname command pipe failure

Sandwich Maker adh at an.bradford.ma.us
Thu Jun 24 10:38:33 CDT 2004


" From: "Jopson, Andy" <Andrew.Jopson at Rolls-Royce.com>
" 
" I have another one for you:
" 
" I'm attempting to list a directory so that files can be chosen for
" processing, rather than entering as arguments. I find that the for command
" is incredibly SLOW though. Is there a faster way to list the files on
" numbered lines using another technique?
" 
" 	count=0
" 	for file in ./*
" 	do
" 		count=`expr $count + 1`
" 		files[$count]=`basename $file`
" 		echo "	$count)	${files[count]}"
" 	done

one thing that's slowing it is that expr and basename are both cmds
that have to be forked and execed.  cpus are fast enough so it doesn't
mean so much these days, but compare

#!/usr/bin/ksh

typeset -i count=0
for file in ./*
do
	(( count += 1 ))
	files[$count]=${file##*/}
	print "  $count) ${files[$count]}"
done

# end #

and btw ./* should be redundant; * should expand to all file names.
then
files[$count]=${file##*/}
would simplify to
files[$count]=$file

the ksh builtin 'select' may be just what you need, but i've never had
occasion to use it.

eg.

#!/usr/bin/ksh

PS3="Choose from this list: "

select CHOICE in *
do
	print you picked file $CHOICE
done
________________________________________________________________________
Andrew Hay                                  the genius nature
internet rambler                            is to see what all have seen
adh at an.bradford.ma.us                       and think what none thought



More information about the SunHELP mailing list