【问题标题】:FTP session code is not working in while/for loop. How to resolve?FTP 会话代码在 while/for 循环中不起作用。如何解决?
【发布时间】:2014-03-11 09:55:41
【问题描述】:

我有这样的需求,读取服务器 A 上的文件 X 的内容并连接到服务器 B 并获取文件 X 的内容。

我的代码是

set -x
idFile="/tserver/Ravi/logs/FilesToBeCopied.txt"
while read line
do
echo $line
ftp -in hrserver << ! > FtpLog.log
user ravi welcome1
cd /hrserver/kumar/Images
lcd /tserver/Ravi/logs/FTPFiles/
get $line
quit
done < $idFile

我得到的错误是:

./myScript.sh
+ idFile=/tserver/Ravi/logs/FilesToBeCopied.txt
./myScript.sh: line 13: syntax error: unexpected end of file

也尝试使用 for 循环:

for line in `cat /tserver/Ravi/logs/FilesToBeCopied.txt`;do
echo $line
echo "$line $FileStorageLocation"
ftp -in hrserver << ! > FtpLog.log
user ravi welcome1
cd /hrserver/kumar/Images
lcd /tserver/Ravi/logs/FTPFiles/
get $line
quit
done

我遇到了同样的错误,指向文件的最后一行并显示语法错误。

当我尝试通过注释 while/for 循环来运行相同的脚本时,脚本运行成功并且 ftp 完成(这里我硬编码了行值)。由于我的要求是从文件中读取每一行并执行 ftp,因此我选择了循环。

我不熟悉 shell 脚本。我的问题是,是否可以将 FTP 命令合并到一个循环中?如果是这样,你能帮我确定我的代码哪里出错并解决它吗?如果没有,您能否针对我的要求提供解决方案?

【问题讨论】:

    标签: shell loops unix ftp


    【解决方案1】:

    您的Here-document 缺少关闭哨兵。 (或者您在发帖时遇到了复制粘贴问题)。

    试试

    idFile="/tserver/Ravi/logs/FilesToBeCopied.txt"
    while read line
    do
    echo $line
    ftp -in hrserver << ! > FtpLog.log
        user ravi welcome1
        cd /hrserver/kumar/Images
        lcd /tserver/Ravi/logs/FTPFiles/
        get $line
        quit
    !
    done < $idFile
    

    这就是为什么我更喜欢使用更大的令牌。 EOF(文件结束)是一个常见的。

    还记得 here-docs 允许在你的 shell 脚本中缩进令牌,如果你引用令牌,或者在它前面加上 - 字符,即

    ftp -in hrserver <<-EOF > ftpLog.log
         .....
    <tab>EOF
    

    <tab><tab>ftp -in hrserver <<'EOF' > ftpLog.log
    <tab><tab>   ....
    <tab><tab>EOF     
    

    或根据需要进行许多其他组合。首先获取一个简单的缩进 Here-doc,然后尝试使用您真正需要的缩进样式。

    注意:结束 EOF 前面没有空格,但此处文档中的空格保留在原处,允许进行小范围的格式设置。

    IHTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 2018-06-12
      • 1970-01-01
      • 2019-05-17
      • 2014-10-15
      • 2022-12-10
      • 2021-12-30
      • 2020-03-30
      相关资源
      最近更新 更多