可以在ftp脚本中插入for循环吗?

时间:2016-07-21 02:20:33

标签: bash ftp

我想在ftp脚本中加入for循环。我的代码如下。

ftp -n ftp.server.com <<EOF
    user name passwd
    bin
    prompt
    for DATA in d f g l m n o p q; do
        mkdir /directory/$DATA
        cd /directory/$DATA
        mput *.$DATA
    done
    hash
    bye
EOF

它无效,错误是

Interactive mode off.
We only support non-print format, sorry.
?Invalid command
?Invalid command
?Invalid command
Create directory operation failed.
local: mput: No such file or directory
(local-files) ?Invalid command
Hash mark printing on (1024 bytes/hash mark).

当然,我可以将这个ftp脚本反转为for循环。有什么好建议吗?

2 个答案:

答案 0 :(得分:1)

您无法在bash提示符中使用ftp循环。但您可以通过bash循环为stdin生成ftp

{ 
    echo username passwd
    echo bin
    echo prompt
    for DATA in d f g l m n o p q; do
        echo mkdir /directory/$DATA
        echo cd /directory/$DATA
        echo mput *.$DATA
    done
    echo hash
    echo bye
} | ftp -n ftp.server.com

答案 1 :(得分:1)

我的解决方案,用于将所有今天已创建/修改的文件上传到远程服务器。

#!/bin/bash

HOST='hostname'
USER='username'
PASSWD='password'

# Local directory where the files are stored.
cd "/local/directory/from where to upload files/"

# To get all the files added today only.
TODAYSFILES=`find -maxdepth 1 -type f -mtime -1`


# remote server directory to upload backup
REMOTEDIR="/directory on remote ftp computer/"

for FILENAME in ${TODAYSFILES[@]}; do
  ftp -n -v $HOST << EOT
  ascii
  user $USER $PASSWD
  prompt
  cd $REMOTEDIR
  put $FILENAME
  bye
  EOT
done