use*_*857 3 bash loops file wildcard
我正在编写一个小脚本,用于启动在后台创建的脚本。该脚本在循环中运行,并且必须在指定目录中找到创建的文件时启动该文件。
当目录中只有一个文件时它会起作用,创建的脚本在完成后会自行删除。但是,当同时创建 2 个或更多脚本时,将无法运行脚本。
我收到错误:需要二元运算符
#!/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
Run Code Online (Sandbox Code Playgroud)
我创建了一个工作正常的解决方案,没有任何问题:
#!/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
Run Code Online (Sandbox Code Playgroud)
类似问题的一个潜在根源是与通配符匹配的文件不存在。在这种情况下,它只处理单词 containsint the*本身。
$ touch exist{1,2} alsoexist1
$ for file in exist* alsoexist* notexist* neitherexist*
> do echo $file
> done
exist1
exist2
alsoexist1
notexist*
neithereixt*
Run Code Online (Sandbox Code Playgroud)