我有cronjob在特定时间每天运行一个脚本。该脚本用于转换特定文件夹中的大文件(大约2GB)。问题是,不是每天我的同事都把文件放在文件夹之前,写成cronjob。
请帮我在脚本中添加命令或编写第二个脚本:
到目前为止,我给出了剧本的重要部分:
cd /path-to-folder
for $i in *.mpg; do avconv -i "$i" "out-$i.mp4" ; done
10x的帮助!
评论后的新代码:
文件夹中有文件!
#! /bin/bash
cdate=$(date +%Y%m%d)
dump="/path/folder1"
base=$(ls "$dump")
if [ -n "$file"]
then
file="$dump/$base"
size=$(stat -c '%s' "$file")
count=0
while sleep 10
do
size0=$(stat -c '%s' "$file")
if [ $size=$size0 ]
then $((count++))
count=0
fi
if [ $count = 2 ]
then break
fi
done
# file has been stable for two minutes. Start conversion.
CONVERSION CODE
fi
终端的消息:可能是错误???
script.sh: 17: script.sh: arithmetic expression: expecting primary: "count++"
答案 0 :(得分:3)
file=/work/daily/dump/name_of_dump_file
if [ -f "$file" ]
then
# size=$(ls -l "$file" | awk '{print $5}')
size=$(stat -c '%s' "$file")
count=0
while sleep 60
do
size0=$(stat -c '%s' "$file")
if [ $size = $size0 ]
then : $((count++))
else size=$size0
count=0
fi
if [ $count = 2 ]
then break
fi
done
# File has been stable for 2 minutes — start conversion
fi
鉴于略微修改的要求(在评论中描述),并假设文件名不包含空格或换行符或其他类似的尴尬字符,那么您可以使用:
dump="/work/daily/dump" # folder 1
base=$(ls "$dump")
if [ -n "$file" ]
then
file="$dump/$base"
...code as before...
# File has been stable for 2 minutes - start conversion
dir2="/work/daily/conversion" # folder 2
file2="$dir2/$(basename $base .mpg).xyz"
convert -i "$file" -o "$file2"
mv "$file" "/work/daily/originals" # folder 3
ncftpput other.coast.example.com /work/daily/input "$file2"
mv "$file2" "/work/daily/converted" # folder 4
fi
如果文件夹中没有任何内容,则进程退出。如果您希望它等到有要转换的文件,那么您需要循环文件测试:
while file=$(ls "$dump")
[ -z "$file" ]
do sleep 60
done
这使用了一个鲜为人知的shell循环特性;您可以在控件中堆叠命令,但它是控制循环的最后一个的退出状态。
答案 1 :(得分:0)
好吧,我最终制作了一些如下工作代码:
#!/bin/bash
cdate=$(date +%Y%m%d)
folder1="/path-to-folder1"
cd $folder1
while file=$(ls "$folder1")
[ -z "$file" ]
do sleep 5 && echo "There in no file in the folder at $cdate."
done
echo "There is a file in folder at $cdate"
size1=$(stat -c '%s' "$file")
echo "The size1 is $size1 at $cdate"
size2=$(stat -c '%s' "$file")
echo "The size2 is $size2 at $cdate"
if [ $size1 = $size2 ]
then
echo "file is stable at $cdate. Do conversion."
else sh /home/user/bin/exist-stable.sh
fi
else exec /home/user/bin/exist-stable.sh
fi