LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-11-2012, 03:27 PM   #1
dotran
LQ Newbie
 
Registered: Apr 2012
Posts: 27
Blog Entries: 2

Rep: Reputation: Disabled
Script count files in the directory but keep loop not exit untill see 10 files.


Can someone help me with this syntax? Let say client send 10 files called (test1 to test10.txt). I want the script keep run/loop until see 10 file then complete send out the e-mail. If not see 10 files yet then go sleep or wait whatever. What's the best way do this?

cd /tmp
-rw-rw-r-- 1 ca7prod ftpusers 0 Apr 11 11:16 test1.txt
-rw-rw-r-- 1 ca7prod ftpusers 0 Apr 11 11:16 test2.txt
-rw-rw-r-- 1 ca7prod ftpusers 0 Apr 11 11:16 test3.txt

#!/bin/ksh

COUNT=`ls -l test*.txt| wc -l`

if [ "$COUNT" -ge "10" ]
then
echo "See 10 files!" | mailx -s "Complete 10 files" abc@yahoo.com
else
echo "See not complete 10 files"
sleep 200
fi

Last edited by dotran; 04-11-2012 at 03:37 PM.
 
Old 04-11-2012, 05:24 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,565

Rep: Reputation: 410Reputation: 410Reputation: 410Reputation: 410Reputation: 410
Switch to using a while loop possibly? Your if statement will work but it will not continue to check it will only run one time and will not check again. You could also set that script to be a cron job to run every minute and check.

---------- Post added 04-11-12 at 03:24 PM ----------

And get rid of the sleep statement its not gonna do anything for you in an if statement.
 
Old 04-11-2012, 05:31 PM   #3
CTM
Member
 
Registered: Apr 2004
Distribution: Slackware64 13.37 / 14.0
Posts: 127

Rep: Reputation: 45
Counting the number of lines output by ls contains too many failure scenarios to be used reliably. A common case is if there are no files matching "test*.txt", in which case ls prints

Code:
total 0
and you'll end up with a "count" of 1; worse, if you end up with a subdirectory whose name matches "test*.txt" (e.g. "test1.txt"), ls will also recurse into that directory and print

Code:
-rw-r--r-- 1 ctm users    0 Apr 11 18:18 test2.txt

test1.txt:
total 0
and you could end up being orders of magnitude away from having the right count. Using find is probably the safest option (remember not to count newlines with wc, as many file systems allow file names to contain newline characters that will end up getting printed):

Code:
find [DIRECTORY] -maxdepth 1 -type f -name 'test*.txt' -exec printf '.' \; | wc -c
This might seem like overkill for such a simple task, but I've seen shell scripts fail spectacularly because of input I wasn't expecting, so get in the habit of writing good ones early
 
Old 04-11-2012, 06:02 PM   #4
dotran
LQ Newbie
 
Registered: Apr 2012
Posts: 27
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the reply, I thought there's the way to loop through a directory and check see all the files there then the script complete. Just like use the filewath on the autosys. I don't like use the crontab run the script every 5'.
 
Old 04-11-2012, 06:35 PM   #5
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,565

Rep: Reputation: 410Reputation: 410Reputation: 410Reputation: 410Reputation: 410
Right so use a while loop with a nested if statement.

So something like

Code:
while [ condition ]
do
   statements1      #Executed as long as condition is true and/or, up to a disaster-condition if any.
   statements2
  if (disaster-condition)
  then
	break       	   #Abandon the while lopp.
  fi
  statements3          #While good and, no disaster-condition.
done
 
Old 04-12-2012, 11:36 AM   #6
dotran
LQ Newbie
 
Registered: Apr 2012
Posts: 27
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Help help.....Let say client send 10 files called (test1.txt to test10.txt). I want the script keep run/loop until see 10 file then complete send out the e-mail. If not see 10 files yet then go sleep or wait whatever. I like the break command, but still can't make this work yet. Please anyone help or any more idea on this. Thanks

-rw-rw-r-- 1 ca7prod ftpusers 0 Apr 11 11:16 test1.txt
-rw-rw-r-- 1 ca7prod ftpusers 0 Apr 11 11:16 test2.txt
-rw-rw-r-- 1 ca7prod ftpusers 0 Apr 11 11:16 test3.txt

#!/bin/bash

count=`ls -l test*.txt | grep -v ^l | wc -l`

while [ $count -le 5 ]
do
if [ $count = true ]
then
break
fi
sleep 300 <==Something rite here not rite or check the last filename test10.txt ????
done
echo Finished
 
Old 04-12-2012, 12:40 PM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,565

Rep: Reputation: 410Reputation: 410Reputation: 410Reputation: 410Reputation: 410
Code:
#!/bin/bash

count=`ls -l test*.txt | grep -v ^l | wc -l`

while [ $count -le 10 ]
do
count=`ls -l test*.txt | grep -v ^l | wc -l`
if [ $count = 10 ]
then
echo "See 10 files!" | mailx -s "Complete 10 files" abc@yahoo.com
break
fi
 
Old 04-12-2012, 01:09 PM   #8
dotran
LQ Newbie
 
Registered: Apr 2012
Posts: 27
Blog Entries: 2

Original Poster
Rep: Reputation: Disabled
Oh...my god you save my day Mr.Kuscom42. This script work really well. Thanks alot.

#!/bin/bash

count=`ls -l test*.txt | grep -v ^l | wc -l`

while [ $count -le 10 ]
do
count=`ls -l test*.txt | grep -v ^l | wc -l`
if [ $count = 10 ]
then
echo "See 10 files!" | mailx -s "Complete 10 files" abc@yahoo.com
break
fi
done
 
Old 04-12-2012, 03:40 PM   #9
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,565

Rep: Reputation: 410Reputation: 410Reputation: 410Reputation: 410Reputation: 410
Ok, now you should start to look at why so you can understand what is happening and can write it yourself next time. A bash while loop will continue to execute a command until the condition is no longer true.

Try this test script to see an example:

Code:
while true
do echo 'command being executed'
sleep 2 # Sleep two seconds so your terminal doesnt blow up
done
You will have to ctrl+c to kill the script as it will always echo the statement because the statement is true. So the while loop above is doing an ls on the directory constantly and resetting the count variable.

Here is a good reference to bookmark: http://linux.die.net/Bash-Beginners-...ect_09_02.html

---------- Post added 04-12-12 at 01:40 PM ----------

Also, please mark the thread as solved if you no longer have questions. It is under thread tools near the top of the thread.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help making specific script loop over multiple files in directory novloski Linux - Newbie 3 05-11-2011 05:13 AM
[SOLVED] exit in script files cause konsole terminal itself to exit Star_Gazer Programming 11 04-12-2010 08:32 AM
Count files in dir then move that many files in a script? chuckycharms Linux - Newbie 3 12-18-2009 11:31 AM
Loop through all files in a directory. Bash/Perl script? Nzo Linux - Newbie 9 12-09-2009 07:09 PM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 06:22 AM


All times are GMT -5. The time now is 09:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration