【问题标题】:Transfer files using lftp in bash script在 bash 脚本中使用 lftp 传输文件
【发布时间】:2023-03-17 04:05:01
【问题描述】:

我有服务器 A test-lx 和服务器 B test2-lx,我想将文件从服务器 A 传输到服务器 B。 在传输文件时,我只需要在不存在的情况下创建一个 driectory,如何在 lftp 连接期间检查目录是否存在?我怎样才能在一个命令中输出多个文件,而不是在 2 行中执行此操作。 是否可以选择使用find -maxdepth 1 -name DirName

这是我的代码:

lftp -u drop-up,1Q2w3e4R   ftp://ta1bbn01:21 << EOF

cd $desFolder
mkdir test
cd test
put $srcFil
put $srcFile

bye 
EOF

【问题讨论】:

    标签: linux bash unix centos


    【解决方案1】:

    首先将凭证记录准备到 ~/.netrc 文件中:

    machine site-url-here
    login user-login-here
    password user-password-here
    

    因此您不必在命令行上公开您的密码即可在脚本中使用它。

    然后调用:

    lftp -e "lftp-command-here" ftps://user-login-here@site-url-here/initial-folder-here/`
    

    在我的例子中,我运行 mget -c *lftp 命令从运行 linux 应用程序实例的 java spring boot 应用程序获取所有日志。 当然你可以把你的命令用分号隔开。

    【讨论】:

      【解决方案2】:

      我使用了与 phe 相同的基本编码大纲,但是我发现如果文件夹为空,使用 ls /foldername 将输出“文件夹不存在”。为了解决这个问题,我使用

      #!/bin/bash
      checkfolder=$(lftp -c "open -u user,pass ip; ls | grep /test1231")
      
      if [ "$checkfolder" == "" ];
      then
      echo "folder does not exist"
      else
      echo "folder exists"
      fi
      

      请注意,这仅适用于文件夹位于根目录中的情况。对于文件夹中的子目录,以下应该可以工作。

      #!/bin/bash
      checkfolder=$(lftp -c "open -u user,pass ip; find | grep home/test1/test1231")
      
      if [ "$checkfolder" == "" ];
      then
      echo "folder does not exist"
      else
      echo "folder exists"
      fi
      

      【讨论】:

        【解决方案3】:

        ftp 的简单方法:

        #!/bin/bash
        
        ftp -inv ip << EOF
        user username password
        
        cd /home/xxx/xxx/what/you/want/
        put what_you_want_to_upload
        
        bye
        EOF
        

        使用 lftp:

        #!/bin/bash
        
        lftp -u username,password ip << EOF
        
        cd /home/xxx/xxx/what/you/want/
        put what_you_want_to_upload
        
        bye
        EOF
        

        来自 lftp 手册:

        -u <user>[,<pass>]  use the user/password for authentication
        

        您可以使用 mkdir 创建目录。你可以像这样多次使用 put 命令:

        put what_you_want_to_upload
        put what_you_want_to_upload2
        put what_you_want_to_upload3
        

        你可以关闭与bye的连接


        您可以像这样检查文件夹是否存在:

        #!/bin/bash
        checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test1231")
        
        if [ "$checkfolder" == "" ];
        then
        echo "folder does not exist"
        else
        echo "folder exist"
        fi
        

        来自 lftp 手册:

        -c <cmd>            execute the commands and exit
        

        你可以打开另一个连接来放一些文件。


        我不知道如何通过一个连接检查文件夹是否存在,但我可以这样做。也许你能找到更好的解决方案:

        #!/bin/bash
        checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test2")
        
        if [ "$checkfolder" == "" ];
        then
        
        lftp -u user,pass ip << EOF
        
        mkdir test2
        cd test2
        put testfile.txt
        bye
        EOF
        
        else
        
        echo "The directory already exists - exiting"
        
        fi
        

        【讨论】:

        • 如果我通过终端运行它,它可以工作,但在 bash 脚本中它不起作用,它正在建立连接,但它没有执行 putcd 命令。这是我用来建立连接的命令:lftp -u myUser,myPass ftp://ta1bbn01:21 SwDrop\Repositorysleep 5 cd $desFolder sleep 5 put $srcFile bye
        • 实际上它现在工作了,你知道我如何在打开的连接中使用条件吗?我想检查我要创建的文件夹是否已经存在。这就是我写的:lftp -u drop-up,1Q2w3e4R ftp://ta1bbn01:21 &lt;&lt; EOF cd $desFolder if [ ! -d /test ]; then mkdir test cd test put $srcFile else echo "The directory already exists - exiting" fi bye EOF
        • 不能用if,因为不是ftp命令。打开 ftp 连接后,您只能使用 ftp 命令。您可以通过其他连接检查文件夹是否存在。你能解释一下你想要什么,请编辑你的问题。
        • 即使目录不存在,命令ls 也会返回一个字符串。这是我得到的字符串:550 /MSP-3.0.0.0: No such file or directory
        • 我找到了一种使find 命令起作用的方法。 find -d 1 DirToSearch
        猜你喜欢
        • 1970-01-01
        • 2019-11-02
        • 1970-01-01
        • 2017-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        相关资源
        最近更新 更多