【问题标题】:Failed to transfer some file with mput on sftp无法在 sftp 上使用 mput 传输某些文件
【发布时间】:2022-10-20 20:43:46
【问题描述】:

我的脚本有问题,允许我用SFTP 传输几个excel文件,每天都有一些文件没有传输,这不是文件内容或名称的问题。

这是代码:

sshpass -f password/pass_sftp sftp $SFTP_USER@$SFTP_HOST << EOF
cd /store/availability
mput *.csv
bye
EOF

你知道它可能是什么吗?

【问题讨论】:

  • 尝试引用第一个EOF

标签: bash sftp heredoc


【解决方案1】:

命令的正确使用形式, 使用“EOF”作为模式来表示输入结束, 是使用组合“<<-" 后跟{细绳}.

您的第一行应为:

sshpass -f password/pass_sftp sftp $SFTP_USER@$SFTP_HOST <<-EOF

【讨论】:

    【解决方案2】:

    heredoc 结构有时会误导交互式程序,因为您可能会遇到同步问题。也就是说,交互程序可能在提示之前不接受数据,或者在显示提示之前将目前收到的数据清空。这就是为什么使用像 expect 这样的实用程序来模拟交互式工具前面的操作员更加健壮的原因。

    让我们考虑以下交互式程序示例,它提示输入名字和姓氏并模拟两个输入之间的一些活动:

    #!/bin/bash
    
    echo "Enter your first name: "
    read fname
    
    # Do some actions here and make some cleanup in the input buffer
    read -t 1 -n 1000 garbage
    
    echo "Enter your last name: "
    read lname
    
    echo you have entered $fname $lname
    

    如果我们以交互方式运行它,它可以正常工作:

    $ ./interact.sh 
    Enter your first name: 
    azerty
    Enter your last name: 
    qwerty
    you have entered azerty qwerty
    

    但是如果我们用heredoc 运行它,它会失败,因为第二个输入来得太早了:

    $ ./interact.sh <<EOF
    > azerty
    > qwerty
    > EOF
    Enter your first name: 
    Enter your last name: 
    you have entered azerty
    

    heredoc 不提供与显示的提示同步的能力。
    使用expect 脚本,可以像人类一样在输入答案之前等待提示的显示。让我们考虑以下自动化交互.sh

    #!/usr/bin/expect
    
    set timeout -1
    spawn ./interact.sh
    expect "first name:"
    send "azerty
    "
    expect "last name:"
    send "qwerty
    "
    expect eof
    

    执行结果与人类交互时相同:

    $ ./exp
    spawn ./interact.sh
    Enter your first name: 
    azerty
    Enter your last name: 
    qwerty
    you have entered azerty qwerty
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      相关资源
      最近更新 更多