bash 上传文件到 sftp 的脚本

bash script to upload files to sftp

我想创建一个智能脚本来将文件上传到 sftp 站点。我无法使用 scp,因为我无法控制 sftp 站点,而且供应商不允许 public 密钥身份验证。

#!/bin/bash

            read -p "Do you want to list the sftp site contents (y/n): " list
            read -p "Upload Selected file, type the name of the file: " uploadfilename


    conenctsfttp() {
            export SSHPASS=****
            sshpass -e sftp -oBatchMode=no -b - testuser@example.com
    }

    listfiles() {
            if [[ $list = y ]];
            then
            conenctsfttp <<< $'ls\n bye'
            else
            echo not list
            fi
    }

    uploadfilen() {
    #        if [[ $uploadfilename = y ]];
    #        then
            conenctsfttp << EOF
            put $(uploadfilename)
    EOF
    }

    listfiles
    uploadfilen

如何让脚本读取文件名并将文件上传到 sftp 服务器。文件名永远不会被拾取,它会进入无限循环。如果可能的话,我想定义上传文件所在的位置。

$(uploadfilename) 应该是 $uploadfilename.

$(...) 是命令替换的另一种形式。要从变量中引用(检索)值,您需要使用 $variable 语法。