I need some help converting this following code from Batch to Bash for my Mac. It is just a simple program which gets files from my computer to put into an ftp server:
In the batch file:
ftp -s:FTP.txt ftp.example.com
In the FTP.txt file:
*user*
*password*
cd /files/
put "C:\Users\*username*\Pictures\image.jpg"
So I have almost got the equivalent, with help of a few hours of research, but need some help still:
#!/bin/sh
HOST='ftp.example.com'
USER='*user*'
PASS='*password*'
FILE='/Users/*username*/Desktop/image.jpg'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
cd /files/
put $FILE
quit
END_SCRIPT
This just doesn't quite work, it logs in, then changes the current directory, but says:
local: /Users/*username*/Desktop/image.jpg remote: /Users/*username*/Desktop/image.jpg
Can't open that file: No such file or directory.
Then proceeds to logs out. I have checked over the directory path, and it's completely fine, I can even open another terminal and do cd /Users/username/Desktop/image.jpg and it works.
Any help is appreciated, thanks.
The problem is that when you do:
put /Users/*username*/Desktop/image.jpg
it is trying to create a file on your server called
/Users/*username*/Desktop/image.jpg
but there is probably no directory called /Users/.../Desktop
on your FTP server to put it in and it will not create the path itself.
What you actually need to do, is add the name of the file on the server where you want the uploaded file to be stored, like this:
put /Users/*username*/Desktop/image.jpg NEW.jpg
Also, you should quote variables, so you would want:
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
cd /files/
put "$FILE" NEW.jpg
quit
END_SCRIPT
Alternatively, you can avoid passing a full path to the FTP server by locally changing directory first, so you would do:
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
cd /files/
lcd "$USER/Desktop"
put image.jpg
quit
END_SCRIPT
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments