2016-05-31 90 views
0

我想从txt文件中读取文件名并将FTP服务器中的文件从一个文件夹移动到另一个文件夹。我有以下命令Linux lftp mv命令与变量

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 

然而,$ {$线}变量没有被替换的值和FTP服务器表示

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

任何指针将不胜感激。 (如果有帮助,我在Centos 6.5上)。

+0

您将整个命令包围在单引号中,并且变量不会在单引号字符串中扩展。如果你只是在'lftp'命令中反转单引号和双引号,它可能会工作 –

回答

1

你有整个命令单引号,它可以防止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 

,因为我们可以用printf%q打印报价/转义字符串可以在使用下一层命令

+0

修复它。谢谢! – everisk