带有变量的Linux lftp mv命令(Linux lftp mv command with variable)
我正在尝试从txt文件中读取文件名,并将FTP服务器中的文件从一个文件夹移动到另一个文件夹。 我有以下命令
grep '.rar' /home/xxxxx/public_html/xxxx/download.txt | while read -r line ; do lftp -e 'set net:timeout 20; mv "Folder Name/${line}" "Folder Name/tmp/${OUTPUT}"; bye' -u username,password ftps://11.11.11.11:990 ; done
但是,$ {$ line}变量不会被值替换,并且FTP服务器正在显示
file/directory not found (Folder Name/${line})
任何指针将不胜感激。 (如果有帮助,我在Centos 6.5上)。
I'm trying to read file name from a txt file and move files in a FTP server from one folder to another. I have the following command
grep '.rar' /home/xxxxx/public_html/xxxx/download.txt | while read -r line ; do lftp -e 'set net:timeout 20; mv "Folder Name/${line}" "Folder Name/tmp/${OUTPUT}"; bye' -u username,password ftps://11.11.11.11:990 ; done
However, the ${$line} variable is not being replaced with values and the FTP server is showing
file/directory not found (Folder Name/${line})
Any pointer would be appreciated. (I'm on Centos 6.5 if that helps).
原文:https://stackoverflow.com/questions/37548985
更新时间:2019-09-10 20:37
最满意答案
你有整个命令单引号,这可以防止bash参数扩展。 您可以通过反转单引号和双引号来修复该部分,如下所示:
grep '.rar' /home/xxxxx/public_html/xxxx/download.txt | while read -r line ; do lftp -e "set net:timeout 20; mv 'Folder Name/${line}' 'Folder Name/tmp/${OUTPUT}'; bye" -u username,password ftps://11.11.11.11:990 ; done
假设您没有包含换行符或单引号的文件,这应该可行。
为了帮助防止特殊字符,您可以使用printf而不是直接扩展到适当位置,如:
grep '.rar' /home/xxxxx/public_html/xxxx/download.txt | while read -r line ; do lftp -e "set net:timeout 20; mv '$(printf 'Folder Name/%q' "${line}")' '$(printf 'Folder Name/tmp/%q' "${OUTPUT}")'; bye" -u username,password ftps://11.11.11.11:990 ; done
因为我们可以使用带有%q printf来打印可以在下一层命令中使用的带引号/转义字符串
You have the whole command single quoted, which prevents bash parameter expansion within. You can fix that part by reversing the single and double quotes like so:
grep '.rar' /home/xxxxx/public_html/xxxx/download.txt | while read -r line ; do lftp -e "set net:timeout 20; mv 'Folder Name/${line}' 'Folder Name/tmp/${OUTPUT}'; bye" -u username,password ftps://11.11.11.11:990 ; done
Assuming you have no files with newlines or single quotes this should work I expect.
To help protect against special characters you can use printf instead of just directly expanding in place like:
grep '.rar' /home/xxxxx/public_html/xxxx/download.txt | while read -r line ; do lftp -e "set net:timeout 20; mv '$(printf 'Folder Name/%q' "${line}")' '$(printf 'Folder Name/tmp/%q' "${OUTPUT}")'; bye" -u username,password ftps://11.11.11.11:990 ; done
since we can use printf with %q to print a quoted/escaped string that can be used in the next layer of command
2016-05-31
相关问答
perl:执行命令执行stdin 您可以使用open打开管道来执行程序( lftp )输入。 open( my $LFTP,'|-', "lftp -u $ftpuser,$pass -e open $hostname" )
or die "Cannot open lftp: $!";
print $LFTP <
set ftp:ssl-allow no
set mirror:use-pget-n 5
glob -a rm -r 'remote_dir/db-import.ph
...
使用Command Substitution将Command Substitution的输出存储在变量中: output=`lftp -c 'open -e "mirror /path/to/remote /path/to/local/" ftp://username:password@ftp.domain.com:21'`
echo "Test: ${output}"
Use Command Substitution to store the output of a command in a
...
在用lftp实验之后,我使用bash脚本发布解决方案。 所以bash脚本文件的内容是 #!/bin/bash
USER='username'
PASS='password'
HOST='ftp.mydomain.com'
LOCAL_BACKUP_DIR='/backups'
REMOTE_DIR='/backupfiles'
lftp -u $USER,$PASS $HOST <
set ftp:ssl-protect-data true
set ftp:ssl-force true
...
你有整个命令单引号,这可以防止bash参数扩展。 您可以通过反转单引号和双引号来修复该部分,如下所示: grep '.rar' /home/xxxxx/public_html/xxxx/download.txt | while read -r line ; do lftp -e "set net:timeout 20; mv 'Folder Name/${line}' 'Folder Name/tmp/${OUTPUT}'; bye" -u username,password ftps://11.
...
lftp的mirror命令的问题在于它在请求页面时向给定的URL添加斜杠(见下文)。 因此,它归结了远程端将如何处理URL以及它是否会对尾部斜杠感到不安。 在我的测试中,Drupal站点例如不喜欢尾部斜杠并且将返回404但是其他一些站点工作正常。 不幸的是,如果你坚持使用lftp,我无法找到解决方法。 测试 我针对Web服务器尝试了以下请求: 1. lftp -c 'mirror -v http://example/path'
2. lftp -c 'mirror -v http://exampl
...
使用-p选项,它不应尝试更改权限。 我从来没有发送到Windows主机,但你是正确的,它不应该对Windows框上的权限级别做任何事情。 Use the -p option and it shouldn't try to change permissions. I've never sent to a windows host, but you are correct in that it shouldn't do anything to the permission levels on the
...
尝试这个: set ftp:use-feat off
Lftp不会使用FEAT查找现有的服务器功能,因此不会使用MFMT命令。 Try this: set ftp:use-feat off
Lftp won't use FEAT to find out existing server features and thus won't use the MFMT command.
刚刚使用示例脚本遇到了同样的问题,并在另一个论坛上找到了解决方案。 发出的正确命令是: lftp -e ... 干杯, Just ran into the same problem by using a sample script and found a solution on another forum. The right command to issue is: lftp -e ... Cheers,
对于cookie,有http:cookie设置。 请参见手册页。 目前还不支持自定义标头,但有一些是通过http:*设置支持的。 For cookies there is the http:cookie setting. See the man page. Custom headers are not supported yet, but there are a few supported via http:* settings.
这是由于密码中的特殊字符 - 我的密码以&结尾,导致lftp期望不同的命令。 为了解决这个问题,我删除了引号并使用|转义& ,像这样: PASSWORD_PROD: password\&
This was due to a special characters in the password - my password ended with & which caused lftp to expect a different command. To fix this, I removed the
...