I'm writing a small script, for starting scripts created in the background. This script is running in a loop and has to start the created file when it is found in the specified directory.
It works when only one file is in the directory, the created script removes itself when it's finisched. But when 2 or more scripts are created at the same time it failes to run the scripts.
I get a error : binary operator expected
#!/bin/bash
files="/var/svn/upload/*.sh"
x=1
while :
do
echo Sleeping $x..
if [ -f $files ]
then
for file in $files
do
echo "Processing $file file..."
sh $file
echo $(date +%d-%m-%y) $(date +%H:%M:%S) - Sleep $x - Script $f >>/var/log/upload.log
x=0
wait
done
fi
x=$(( $x + 1 ))
sleep 1
done
I created a work around wich is working without any problems:
#!/bin/bash
files="/var/upload/*.sh"
x=1
while :
do
count=$(ls $files 2> /dev/null | wc -l)
echo Sleeping $x..
if [ "$count" != "0" ]
then
for file in $files
do
echo "Processing $file file..."
sh $file
echo $(date +%d-%m-%y) $(date +%H:%M:%S) - Sleep $x - Script $f >>/var/log/upload.log
x=0
wait
done
fi
x=$(( $x + 1 ))
sleep 1
done
One potential source of a similar problem is when the files matching the wildcard do not exist. In that case it just handles the word containint the * as such.
$ touch exist{1,2} alsoexist1
$ for file in exist* alsoexist* notexist* neitherexist*
> do echo $file
> done
exist1
exist2
alsoexist1
notexist*
neithereixt*
The -f operator applies to only a single file, not the list that results by expanding your unquoted $files. If you really need to capture the full list of files in a single variable, use an array, not a string. The nullglob option ensures that files is truly empty if the glob fails to match any files, eliminating the need for the -f test. There is also no need to call wait, as you aren't starting any background jobs.
#!/bin/bash
shopt -s nullglob
x=1
while :
do
echo Sleeping $x..
for file in /var/svn/upload/*.sh
do
echo "Processing $file file..."
sh "$file"
echo $(date +%d-%m-%y) $(date +%H:%M:%S) - Sleep $x - Script "$f" >>/var/log/upload.log
x=0
done
x=$(( $x + 1 ))
sleep 1
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With