我有很多目录,每个目录都有一些传入的文件流。 需要脚本,将每个已定义目录中的所有文件上传到已定义的ftp url。 最好的,如果可以通过bash循环完成,睡眠结束 - 循环完成所有定义,睡眠,比如60秒,然后重新开始 - 循环。
现在我有类似的东西,但这是一个丑陋的,非常长的剧本:
####ana
echo "Starting script:ana $(date +%Y.%m.%d\ %H:%M:%S)..."
getfmts() { if [ -f "$1" ] || [ -d "$1" ]; then echo $(stat -c %Y $1); else echo 0; fi; }
DIRIN_ana=/DWD_sorted/ana # Full path to input directory
DIROUT_ana=/DWD_sorted/ana_sent # Full path to output directory
cd $DIRIN_ana
if [ "$(ls -A .|grep bufr)" ]; then echo "Processing files..."; else echo "No files"; exit; fi
for f in *; do
ts=$(date +%s); tsf=$(getfmts $f)
if [ $((ts-tsf)) -gt 10 ]; then
echo "Sending file $f to ftp..."
curl -T $f ftp://smart:smart@SM/../../smart/edit/dwd/ana
if [ $? -eq 0 ]; then
mv -f $f $DIROUT_ana/
else
echo "There was an error when trying to upload file!"
fi
fi
done
echo "Script finished: $(date +%Y.%m.%d\ %H:%M:%S)"
####hsy
echo "Starting script:hsy $(date +%Y.%m.%d\ %H:%M:%S)..."
getfmts() { if [ -f "$1" ] || [ -d "$1" ]; then echo $(stat -c %Y $1); else echo 0; fi; }
DIRIN_hsy=/DWD_sorted/hsy # Full path to input directory
DIROUT_hsy=/DWD_sorted/hsy_sent # Full path to output directory
cd $DIRIN_hsy
if [ "$(ls -A .|grep bufr)" ]; then echo "Processing files..."; else echo "No files"; exit; fi
for f in *; do
ts=$(date +%s); tsf=$(getfmts $f)
if [ $((ts-tsf)) -gt 10 ]; then
echo "Sending file $f to ftp..."
curl -T $f ftp://smart:smart@SM/../../smart/editor/dwd/gme/hsy
if [ $? -eq 0 ]; then
mv -f $f $DIROUT_hsy/
else
echo "There was an error when trying to upload file!"
fi
fi
done
echo "Script finished: $(date +%Y.%m.%d\ %H:%M:%S)"
答案 0 :(得分:1)
一些粗略的清理和简化:
echo "Starting script:ana $(date +%Y.%m.%d\ %H:%M:%S)..."
getfmts() {
if [ -f "$1" ] || [ -d "$1" ]; then
echo $(stat -c %Y $1)
else
echo 0
fi
}
for dir in ana hsy; do
echo "Processing $dir"
dir_in="/DWD_sorted/$dir"
dir_out="${dir_in}_sent"
cd $dir_in
if [ "$(ls -A .|grep bufr)" ]; then
echo "Processing files..."
else
echo "No files"
continue
fi
for f in *; do
ts=$(date +%s); tsf=$(getfmts $f)
if [ $((ts-tsf)) -gt 10 ]; then
echo "Sending file $f to ftp..."
curl -T $f ftp://smart:smart@SM/../../smart/edit/dwd/$dir
if [ $? -eq 0 ]; then
mv -f $f $dir_out/
else
echo "There was an error when trying to upload file!"
fi
fi
done
echo "Script finished: $(date +%Y.%m.%d\ %H:%M:%S)"
done