I would do this:<br><br>if [ `ls *.bad | wc -l` -gt 0 ]<br> then<br> echo Bad<br> fi<br><br>Chris<br><br><div class="gmail_quote">On Dec 20, 2007 12:58 PM, Brown, David [DNR] <<a href="mailto:David.Brown@dnr.iowa.gov">
David.Brown@dnr.iowa.gov</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Another way to check if any ".bad" files exist is piping the ls output
<br>to grep, searching for ".bad", with the count flag set and storing this<br>output in a variable.<br><br>E.g.<br> N_bad_files=`ls | fgrep -c ".bad"`<br><br> if [ $N_bad_files -gt 0 ] ;then
<br> echo bad<br> fi<br><div><div></div><div class="Wj3C7c"><br><br><br>-----Original Message-----<br>From: <a href="mailto:cialug-bounces@cialug.org">cialug-bounces@cialug.org</a> [mailto:<a href="mailto:cialug-bounces@cialug.org">
cialug-bounces@cialug.org</a>] On<br>Behalf Of Daniel A. Ramaley<br>Sent: Thursday, December 20, 2007 11:22 AM<br>To: Central Iowa Linux Users Group<br>Subject: [Cialug] [OT]: Shell script globbing<br><br>I have what should be a simple shell scripting problem. If any .bad
<br>files exist, then the script should do some stuff (display an error, do<br>some cleanup, and exit, though for testing i've simplified it). So far<br>i've come up with 2 solutions, one requiring a variable and running
<br>"ls", the other requiring a seemingly superfluous "for" loop. Is there a<br><br>more elegant solution i've not discovered?<br><br>My first attempt, doesn't work because -e expects only 1 argument and
<br>there may be multiple .bad files:<br> if [ -e *.bad ] ; then<br> echo Bad<br> fi<br><br>My first working solution, letting ls handle the glob and doing a string<br><br>comparison on the result:<br> GLOB=`ls *.bad 2> /dev/null`
<br> if [ -n "$GLOB" ] ; then<br> echo Bad<br> fi<br><br>My second solution; the "if" is required because if no .bad files exist<br>the loop will still run once with file set to the literal "*.bad":
<br> for file in *.bad ; do<br> if [ -e $file ] ; then<br> echo Bad<br> fi<br> done<br><br>------------------------------------------------------------------------<br>Dan Ramaley Dial Center 118, Drake University
<br>Network Programmer/Analyst 2407 Carpenter Ave<br>+1 515 271-4540 Des Moines IA 50311 USA<br>_______________________________________________<br>Cialug mailing list<br><a href="mailto:Cialug@cialug.org">
Cialug@cialug.org</a><br><a href="http://cialug.org/mailman/listinfo/cialug" target="_blank">http://cialug.org/mailman/listinfo/cialug</a><br>_______________________________________________<br>Cialug mailing list<br><a href="mailto:Cialug@cialug.org">
Cialug@cialug.org</a><br><a href="http://cialug.org/mailman/listinfo/cialug" target="_blank">http://cialug.org/mailman/listinfo/cialug</a><br></div></div></blockquote></div><br>