【问题标题】:ftp upload script - from many directories, each to his own ftp path?ftp 上传脚本 - 从许多目录,每个到他自己的 ftp 路径?
【发布时间】:2018-05-15 09:26:11
【问题描述】:

我有很多目录,每个目录都有一些传入的文件流。 需要脚本,将每个已定义目录中的所有文件上传到已定义的 ftp url。 最好的,如果这可以通过 bash 循环和 sleep in end 来完成 - 循环通过所有定义,睡眠 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)"

【问题讨论】:

  • 请缩进脚本:)
  • 现在可以了吗?我想,我可以定义一个链接变量,例如,FTP1=smart:smart@SM/../../smart/editor/dwd/gme/hsy,DIRIN1=/DWD_sorted/hsy 等等,但是如何构建一个循环脚本,谁把它全部按部分,以及何时结束,回到起点,重新开始?

标签: bash ftp upload


【解决方案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

【讨论】:

  • 谢谢,看起来不错,除了两个小问题: 1:ftp路径并不总是根据in_dir只改变最后一个目录。有时 ftp 上传 url 上的路径明显不同。第二个:如果脚本可以连续,无休止,那就太好了——在第一次运行完成后,有“sleep 60”,它又开始了,没完没了。那有可能吗?附:非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-07-07
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多