Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在bash脚本中使用lftp传输文件_Linux_Bash_Unix_Centos - Fatal编程技术网

Linux 在bash脚本中使用lftp传输文件

Linux 在bash脚本中使用lftp传输文件,linux,bash,unix,centos,Linux,Bash,Unix,Centos,我有服务器Atestlx,服务器Btest2lx,我想将文件从服务器A传输到服务器B。 在传输文件时,仅当驱动器不存在时,我才需要创建驱动器,如何在lftp会议期间检查目录是否存在?如何在一个命令中输出多个文件,而不是在两行中执行此操作。 是否有使用find-maxdepth 1-name DirName 这是我的密码: lftp -u drop-up,1Q2w3e4R ftp://ta1bbn01:21 << EOF cd $desFolder mkdir test cd t

我有服务器A
testlx
,服务器B
test2lx
,我想将文件从服务器A传输到服务器B。 在传输文件时,仅当驱动器不存在时,我才需要创建驱动器,如何在lftp会议期间检查目录是否存在?如何在一个命令中输出多个文件,而不是在两行中执行此操作。 是否有使用
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
lftp-u下拉列表,1Q2w3e4Rftp://ta1bbn01:21 使用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
您还可以通过
bye


您可以检查文件夹是否存在,如下所示:

put what_you_want_to_upload
put what_you_want_to_upload2
put what_you_want_to_upload3
#!/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手册:

-u <user>[,<pass>]  use the user/password for authentication
-c <cmd>            execute the commands and exit
-c执行命令并退出
您可以打开另一个连接以放置一些文件


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

#!/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
#/bin/bash
checkfolder=$(lftp-c“打开-u用户,通过ip;ls/home/test1/test2”)
如果[“$checkfolder”==”;
然后

lftp-u user,pass ip我使用了与phe相同的基本编码大纲,但是我发现如果文件夹为空,使用ls/foldername将输出“folder not exist”。为了解决这个问题,我使用

#!/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

首先将凭据记录准备到~/.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命令,从azure基础设施上运行linux应用程序实例的java spring启动应用程序获取所有日志。
当然,您可以将命令放在那里,用分号分隔。

如果我通过终端运行此命令,它会工作,但在bash脚本中它不工作,它正在建立连接,但它不会执行
put
cd
命令。这是我用来建立连接的命令:
lftp-u myUser,myPassftp://ta1bbn01:21 SwDrop\Repository
sleep 5 cd$desFolder sleep 5 put$srcFile bye
实际上它现在已经工作了,您知道如何在打开的连接中使用条件吗?我想检查我要创建的文件夹是否已经存在。这就是我写的:
lftp-u下拉列表,1Q2w3e4Rftp://ta1bbn01:21 不能使用if,因为它不是ftp命令。打开ftp连接后,只能使用ftp命令。您可以通过另一个连接检查文件夹是否存在。你能解释一下你想要什么吗?请编辑你的问题。即使目录不存在,命令
ls
也会返回一个字符串。这是我得到的字符串:
550/MSP-3.0.0.0:没有这样的文件或目录
我找到了使
find
命令工作的方法<代码>查找-d 1目录搜索
lftp -e "lftp-command-here" ftps://user-login-here@site-url-here/initial-folder-here/`