[SunHELP] dirname command pipe failure

Jopson, Andy Andrew.Jopson at Rolls-Royce.com
Fri Jun 25 04:35:52 CDT 2004


You're a godsend Andrew! Works perfectly now.

It's clear that I have to read a book on the korn shell before anything
else. The man pages are practically useless for learning. I downloaded
learning the korn shell in 21 days last night and O'Reilly's Learning the
Korn Shell (CD Bookshelf). If they turn out to be any good, I'll buy one of
them. Can you recommend any others? 

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


" -----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
ago!

" 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
	count=${1:-1}
}

then try
print $count
XXX
print $count
XXX 4
print $count
XXX
print $count
XXX 0
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
_______________________________________________
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