LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script count files in the directory but keep loop not exit untill see 10 files. (https://www.linuxquestions.org/questions/linux-newbie-8/script-count-files-in-the-directory-but-keep-loop-not-exit-untill-see-10-files-939338/)

dotran 04-11-2012 03:27 PM

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

Kustom42 04-11-2012 05:24 PM

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.

CTM 04-11-2012 05:31 PM

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 :)

dotran 04-11-2012 06:02 PM

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'.

Kustom42 04-11-2012 06:35 PM

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


dotran 04-12-2012 11:36 AM

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

Kustom42 04-12-2012 12:40 PM

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


dotran 04-12-2012 01:09 PM

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

Kustom42 04-12-2012 03:40 PM

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.

zeeshanakhter2009 12-22-2014 04:34 AM

Files Count
 
Here is the command

find -name '*.mp3' -type f | wc -l

That will tell you how many mp3 files in this directory or sub directories.......


All times are GMT -5. The time now is 04:12 AM.