简体   繁体   English

带有变量的Linux lftp mv命令

[英]Linux lftp mv command with variable

I'm trying to read file name from a txt file and move files in a FTP server from one folder to another. 我正在尝试从txt文件读取文件名,并将FTP服务器中的文件从一个文件夹移动到另一个文件夹。 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 但是,$ {$ line}变量未替换为值,并且FTP服务器正在显示

file/directory not found (Folder Name/${line})

Any pointer would be appreciated. 任何指针将不胜感激。 (I'm on Centos 6.5 if that helps). (如果有帮助,我使用Centos 6.5)。

You have the whole command single quoted, which prevents bash parameter expansion within. 您将整个命令用单引号引起来,这可以防止bash参数在其中扩展。 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: 为了帮助防止特殊字符,您可以使用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

since we can use printf with %q to print a quoted/escaped string that can be used in the next layer of command 因为我们可以将printf%q一起使用以打印带引号/转义的字符串,该字符串可以在命令的下一层使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM