【问题标题】:How Can I Run Commands In A SFTP Session Via A Bash Script? [duplicate]如何通过 Bash 脚本在 SFTP 会话中运行命令? [复制]
【发布时间】:2021-11-09 01:30:10
【问题描述】:

我正在尝试将 bash 脚本中的 cd 命令运行到 SFTP 会话。我的代码目前看起来像

#!/bin/bash
sftp $1@$2

然后我想在 SFTP 会话和其他命令中使用cd,但现在 cd 没问题。我该怎么做?

【问题讨论】:

    标签: bash sftp


    【解决方案1】:

    尝试批处理模式。引用手册页:

         -b batchfile
                 Batch mode reads a series of commands from an input
                 batchfile instead of stdin. [...] A batchfile of ‘-’ may be used to indicate standard input.
    

    您可以使用批处理模式按顺序运行命令,例如

    #!/bin/bash
    echo << EOF > sftp-commands-to-run.txt
    ls
    put myfile.txt
    ... more commands ...
    EOF
    sftp -b sftp-commands-to-run.txt $1@$2
    

    你也可以通过stdin传递命令来运行,例如

    #!/bin/bash
    echo ls myfile.txt | sftp -b - $1@$2
    

    【讨论】:

    • 所以我做 sftp -b 然后呢?如果我在同一行做“ls”后记,它会返回一个使用错误
    • 刚刚更新了答案以澄清。
    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 2017-09-21
    • 2011-07-20
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多