3

I have 2 spaces:
My Synology NAS. And my FTP.

If we assume that some files on my local NAS are also in my FTP repository, I want to move some files on my local NAS AND on my FTP on different folders.

eg: After downloading files from FTP to NAS
NAS:
- Move from /volume1/Downloading/file001 to /volume1/Downloaded/file001

FTP:
- Move from /downloads/file001 to /downloads/Finished/file001

Every files on the NAS are moved at the right path : OK
On my FTP, only files/folders that doesn't contain blank spaces are moved : KO

So here is a part of the script we need to know:

#!/bin/sh
# Inits
ficLog=/volume1/ScriptsAndOutputs/logFTPSeedibox.txt
downloadingFolderPath=/volume1/Downloading
downloadedFolderPath=/volume1/Downloaded
ftpDestinationPath=FinishedTmp

# Configuration : ftp / user / pass
servFTP=server
userFTP=user
passFTP=password

for filePath in "${downloadingFolderPath}"/* ; do

    # As filePath is the complete path of the file we need to get the file NAME
    fileName=`basename "${filePath}"`
    #Try to move it on FTP
    lftp ftp://${userFTP}:${passFTP}@${servFTP} -e 'set ssl:verify-certificate false;set file:charset utf8;set ftp:charset utf8;cd downloads;mv "${fileName}" "${ftpDestinationPath}"'
    res2=$?

    #Then we move file on the NAS
    mv "${filePath}" "${downloadedFolderPath}"
done
exit 0

Here is the output :

+ ficLog=/volume1/ScriptsAndOutputs/logFTPSeedibox.txt
+ downloadingFolderPath=/volume1/Downloading
+ downloadedFolderPath=/volume1/Downloaded
+ ftpDestinationPath=FinishedTmp
+ servFTP=server
+ userFTP=user
+ passFTP=password
+ for filePath in '"${downloadingFolderPath}"/*'
++ basename /volume1/Downloading/@eaDir
+ fileName=@eaDir
+ lftp ftp://user:password@server -e 'set ssl:verify-certificate false;set file:charset utf8;set ftp:charset utf8;cd downloads;mv "${fileName}" "${ftpDestinationPath}"'
cd ok, cwd=/downloads           
mv ${fileName}=>${ftpDestinationPath} [Waiting for response...]
mv: Access failed: 550 RNFR command failed. (${fileName})         
+ res2=1
+ mv /volume1/Downloading/@eaDir /volume1/Downloaded
mv: inter-device move failed: ‘/volume1/Downloading/@eaDir’ to ‘/volume1/Downloaded/@eaDir’; unable to remove target: Directory not empty
+ for filePath in '"${downloadingFolderPath}"/*'
++ basename '/volume1/Downloading/Folder With Files'
+ fileName='Folder With Files'
+ lftp ftp://user:password@server -e 'set ssl:verify-certificate false;set file:charset utf8;set ftp:charset utf8;cd downloads;mv "${fileName}" "${ftpDestinationPath}"'
cd ok, cwd=/downloads           
mv ${fileName}=>${ftpDestinationPath} [Waiting for response...]
mv: Access failed: 550 RNFR command failed. (${fileName})         
+ res2=1
+ mv '/volume1/Downloading/Folder With Files' /volume1/Downloaded
+ for filePath in '"${downloadingFolderPath}"/*'
++ basename /volume1/Downloading/Test_no_spaces.txt
+ fileName=Test_no_spaces.txt
+ lftp ftp://user:password@server -e 'set ssl:verify-certificate false;set file:charset utf8;set ftp:charset utf8;cd downloads;mv "${fileName}" "${ftpDestinationPath}"'
cd ok, cwd=/downloads           
mv ${fileName}=>${ftpDestinationPath} [Waiting for response...]
mv: Access failed: 550 RNFR command failed. (${fileName})         
+ res2=1
+ mv /volume1/Downloading/Test_no_spaces.txt /volume1/Downloaded
+ for filePath in '"${downloadingFolderPath}"/*'
++ basename '/volume1/Downloading/test_under et espaces.txt'
+ fileName='test_under et espaces.txt'
+ lftp ftp://user:password@server -e 'set ssl:verify-certificate false;set file:charset utf8;set ftp:charset utf8;cd downloads;mv "${fileName}" "${ftpDestinationPath}"'
cd ok, cwd=/downloads           
mv ${fileName}=>${ftpDestinationPath} [Waiting for response...]
mv: Access failed: 550 RNFR command failed. (${fileName})         
+ res2=1
+ mv '/volume1/Downloading/test_under et espaces.txt' /volume1/Downloaded
+ exit 0

As you can see, it's working for any folder/file on the NAS but only for files/folders names without blank-spaces on the FTP.

Can someone help me? Thanks.

JuGdx
  • 31
  • 1
  • 4
  • Do you actually have a file called `@eaDir`? With the `@`? Also, is that your actual script? Including the `_` before the `>>` on line 29 (the 2nd lftp command)? Does it work if you remove that? – terdon Nov 30 '16 at 10:54
  • @terdon : Yes eaDir is a default hidden folder on Synology nas. But I don't care about this fail. It's normal. My interest is only on the FTP downloading files. I dont't see any "_" ? – JuGdx Nov 30 '16 at 11:18
  • Well, your FTP downloading is trying (and failing) to "move @eaDir into /downloads/FinishedTmp". So you have to care about it, that's the first part that's failing. Anyway, I might have found the problem. You have a non-printing character ("U+00A0, NO-BREAK SPACE", which is displayed as a `_` in my emacs) before every `>>`. I am guessing you didn't use a normal text editor to create this script. Can you try removing them and see if that fixes the issue? They are right before the `>>` on lines 32,34,36,38,40,42 and 52 . – terdon Nov 30 '16 at 11:29
  • Please [edit] your question and post a minimal, reproducible example. Just get the simplest possible script that reproduces the error. Also, I recommend you print the actual command you tried to run when using `echo` to debug. The information you print now isn't very useful since we have no way of knowing what commands actually failed. – terdon Nov 30 '16 at 14:01
  • @terdon : here is the new post, thank you for helping – JuGdx Nov 30 '16 at 15:33
  • You're still not printing useful debug information. We need to see the command the script is running so we can see what values your variables have. Make your script print out the actual `lftp` command it is running. Or, at least, the variables it is using. As you can see in your error message, it seems to be missing one of the two. – terdon Nov 30 '16 at 15:44
  • @terdon : I changed it by using the debug 8 ! – JuGdx Nov 30 '16 at 19:20
  • 1
    Once more: we need to see the exact command being run. Look at the error message, it suggests that one of your variables is undefined. We don't need more verbose debug info, we need to see the `lftp` command that your script is running. Either run the script with `set -x` (and without the rest of the debugging messages that just clutter everything up) or, simpler, `echo` the `lftp` command before running it so we can see what it is attempting to do. – terdon Dec 01 '16 at 09:34
  • @terdon : Okay.... Well sorry that it took so long :D I think this was my last edit, and hope it'll help... – JuGdx Dec 01 '16 at 10:49
  • Related: [How do you expand a variable in a heredoc as one argument when its value has spaces?](//unix.stackexchange.com/q/698545) – Stéphane Chazelas Jul 12 '22 at 06:00

1 Answers1

2

This line:

lftp ftp://${userFTP}:${passFTP}@${servFTP} -e 'set ssl:verify-certificate false;set file:charset utf8;set ftp:charset utf8;cd downloads;mv "${fileName}" "${ftpDestinationPath}"'

The -e bit is in single quotes, which means that the shell won't replace ${fileName} or ${ftpDestinationPath} with the values of those variables.

To fix this, use double quotes:

.... -e "set ssl:verify-certificate false;set file:charset utf8;set ftp:charset utf8;cd downloads;mv '${fileName}' '${ftpDestinationPath}'"

If that somehow fail (I can't currently test it), use \" in place of each of the single quotes above.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • See also: [How do you expand a variable in a heredoc as one argument when its value has spaces?](//unix.stackexchange.com/a/698600) about quoting in lftp – Stéphane Chazelas Jul 12 '22 at 06:03