[SunHELP] dirname command pipe failure

Jopson, Andy Andrew.Jopson at Rolls-Royce.com
Thu Jun 24 12:21:25 CDT 2004


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. 

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.

-----Original Message-----
From: adh at an.bradford.ma.us [mailto:adh at an.bradford.ma.us]
Sent: 24 June 2004 16:39
To: sunhelp at sunhelp.org
Subject: RE: [SunHELP] dirname command pipe failure


" 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
_______________________________________________
SunHELP maillist  -  SunHELP at sunhelp.org
http://www.sunhelp.org/mailman/listinfo/sunhelp


The data contained in, or attached to, this e-mail, may contain confidential
information. If you have received it in error you should notify the sender
immediately by reply e-mail, delete the message from your system and contact
+44(0)1332 242424 (the Rolls-Royce IT Security Director) if you need
assistance. Please do not copy it for any purpose, or disclose its contents
to any other person.

An e-mail response to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.

(c) 2004 Rolls-Royce Group plc

Registered office: 65 Buckingham Gate, London SW1E 6AT
Company number: 1003142. Registered in England.



More information about the SunHELP mailing list