Click to See Complete Forum and Search --> : Bash Script for checking folder
VoiDeR
08-28-2004, 11:41 AM
Im trying to write a script that checks certiant folders and depending if theres any files in them it will start a program. I have been looking through a few of the bash scripting toutorials that have been posted and i can't find how to do this. I have all the script figured out exept how to tell if there are files in a folder.
Thanks
VoiDeR
ernieg
08-28-2004, 12:28 PM
Here's one way you can probably do it...
Listing a directory and piping it to the "wc" command with the "-l" option will yield the number of files in a given directory. Take the output of that into a variable and test for a value greater than zero. below is a quick sample of ls against an empty directory vs one that contains 93 files. You can check the man or info pages for options to ls , wc, if and test.
[ernieg@manito ernieg]$ ls Shared |wc -l
0
[ernieg@manito ernieg]$ ls Video |wc -l
93
You could put something like this in a script:
[ernieg@manito ernieg]$ if ((`ls Video |wc -l`)); then echo "there are files"; else echo "there are no files"; fi
there are files
[ernieg@manito ernieg]$ if ((`ls Shared |wc -l`)); then echo "there are files"; else echo "there are no files"; fi
there are no files
There are of course a ton of variations to this...
This is just one example.
Hope that helps!
bwkaz
08-28-2004, 01:02 PM
Why can't you just run the program and trap the error status that it would return if there aren't any files?
What program are you running?
VoiDeR
08-28-2004, 01:17 PM
giFT is the program. Basicaly what i want to do is on boot up check the incoming folder and if there are any files in it to start giftd so it will continue downloading. Its kinda pointless to start it if im not currently downloading anything.
VoiDeR
ps wheres the spell check button i cant seem to find it
bwkaz
08-29-2004, 01:22 PM
Ah, yeah that makes sense.
There isn't really a spellcheck button that I've ever seen...
As for the issue, I think a:
files=$(ls /directory | wc -l
files=${files:-0} # set it to 0 if it's blank for some reason
if [ "$files" -gt 0 ] ; then
# do whatever
fi would probably work...