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 |
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. |
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 Code:
-rw-r--r-- 1 ctm users 0 Apr 11 18:18 test2.txt Code:
find [DIRECTORY] -maxdepth 1 -type f -name 'test*.txt' -exec printf '.' \; | wc -c |
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'.
|
Right so use a while loop with a nested if statement.
So something like Code:
while [ condition ] |
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 |
Code:
|
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 |
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:
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. |
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. |