Reputation: 16049
I am trying to write a shell script that waits until the number of files in a specified directory ( say ~/fit/) has reached a predefined number. What I've got so far is:
limit = 10
while [ls ~/fit/ | wc -l -lt $limit]
do
sleep 1
done
This says -lt is an invalid option to wc. I also tried
[$limit -gt ls ~/fit/ | wc -l]
but it didn't work either. Any help is greatly appreciated.
Upvotes: 0
Views: 2425
Reputation: 1342
You can use below command;
watch -n 1 'ls -l /your/path/|wc -l'
Upvotes: 1
Reputation: 3247
Try this
while(true)
do
var=`ls -l ~/fit/ | wc -l`
if [ $var -lt 10]
then
sleep 1
else
break
fi
done
Upvotes: 1
Reputation: 1644
A solution that minimize the use of external processes, like ls
, wc
and true
, and manage correctly the (unusual) case of filenames containing newlines:
#!/bin/sh
nmax=10
while :; do
n=0
for f in ~/fit/*; do
n=$((n+1))
done
[ $n -ge $nmax ] && break
sleep 1
done
echo "Limit reached!"
Upvotes: 1
Reputation: 455122
You need:
limit=10
while [ `ls ~/fit/ | wc -l` -lt $limit ]
do
sleep 1
done
Changes:
=
in limit=10
[
and ]
ls ~/fit/ |
wc -l
) in back ticks.Upvotes: 2