[SunHELP] dirname command pipe failure

Sandwich Maker adh at an.bradford.ma.us
Thu Jun 24 13:41:04 CDT 2004

" -----Original Message-----
" From: adh at an.bradford.ma.us [mailto:adh at an.bradford.ma.us]
" " From: "Jopson, Andy" <Andrew.Jopson at Rolls-Royce.com>
" " 
" " 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 #

" From: "Jopson, Andy" <Andrew.Jopson at Rolls-Royce.com>
" Crikey, it's virtually instantaneous! Before, it was taking nearly a second
" to execute each line in the directory. I didn't realise that the wrong
" choice of commands could have such a large effect on processing times. 

one of the first lessons i learned in shell prog class, many moons

" The only problem is that count is defined within a function and I can't make
" it available to the main routine by exporting it with the -x argument in
" typeset or the export command, so I need some way of passing it back.

easy peasy.  define count -outside- the function, and it will retain
the value set -inside- the function.  this is one of the neatest
abilities functions have.

such as

typeset -i count=0
function XXX
	# function body

then try
print $count
print $count
print $count
print $count
print $count
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