Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Count number of files

Posted by hycheah (hycheah), 25 July 2007
I don't know what is wrong with this piece of code.
It works fine when there is 1 or more .exe files in the directory. However, Count will still print Count: 1 even when there is no .exe file in the directory.

#!sh

count=0

for file in *.exe
do
     count=`expr "$count" + 1`
done

echo "Count: $count"

Posted by Chris_Isaac (Chris Isaac), 26 July 2007
I'm guessing here, but I think a DO loop will execute once even if the expression is false, you are adding 1 to $count so $count will always show 1 more than there should be, or at at least 1 as a minimum.

I (again think) a while loop may solve your problems.

Posted by george_Ball (george), 26 July 2007
It's more subtle than that... If you supply the shell with a wildcard expression and there are no files that match the expression, then the shell uses that expression as is... In other words

echo *.exe

will actually display *.exe if there are no files that match!!

This is what is happening here, so there will always be one entry in the list so your loop will always execute once. The do...done is not a separate construct in the shell - it's tied to the "for" so the semantics are as you expect.

You could modify your code to be

count=0
for file in *.exe
do
 if [ -e $file ]
 then
   count=`expr $count + 1`
 fi
done

which catches the pathological case you've described[i][/i]

Posted by admin (Graham Ellis), 27 July 2007
Thanks, George (and welcome here!).  I saw that question briefly and was scratching my head until you posted.  Thanks!

Posted by Chris_Isaac (Chris Isaac), 27 July 2007
yep...George...  That was going to be my second guess



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho