ftp through shell script
I have made a research through the net on how can I write a basic shell script to automate file transfer using FTP. What I need is just to connect to a server and send/recieve a file.
What I have found is: #!/bin/sh HOST='host' USER=’user' PASS=’pass' FILE=’file’ ftp -n $HOST < quote USER $USER quote PASS $PASS get $FILE quit exit 0 When I run the script, I get the following: ./test.sh: quote: cannot open ./test.sh: quote: not found First, why this error appears to me? Second, how the -n option in ftp works? Thanks for your replies in advance. |
The -n switch instructs ftp not to automatically try to log in when you ftp. This gives you the chance to log in using an alternative way; your shell script is attempting that alternative way.
You seem to have a few syntax errors in your script. Try this: Code:
#!/bin/sh Code:
man ftp |
Hi,
The quoting is also incorrect (variable declaration). Backticks and single quotes are used, they should all be single (or double) quotes. I.e.: Code:
#!/bin/sh |
Still, it's not working.
I modified the script as you suggested. But when I run it, nothing happens. It looks like going into an infinite loop! |
Hi,
You say that nothing happens. Do you mean that the script does not stop? A loop is not possible, but it can hang (or is waiting for user input). It could be that you need to change/add a few things, depending on the ftp server you are connecting to. Things you can try: - Add passive between quote PASS $PASS and get $FILE, - Add binary before get $FILE (only needed if you are trying to ftp a binary file), - Add the -v option to see what is happening (ftp -n -v $HOST). A few questions if the above doesn't help: 1) Can you post the complete script you are using (except for the user and password, don't edit anything). 2) Is it possible to ftp from the command line using the host/user etc from the file? 3) Are you running linux or unix. 4) What is the output when the -v option is added. Hope this gets you going again. |
or better, use wget.
|
Quote:
|
This code works fine for me:
Code:
#!/bin/sh |
I think that colucix nailed it.
I had to rename a large set of files (around 3000) in around 50 devices that had ftp access. Doing it manually would be no good. So instead I retrieved the directory, used sed to convert it into a script and called it like: ftp user:pass@host <script There were many devices to loop through, and I found I needed to add the 'bye' command at the end of the script for it to exit. As a side note, this doesn't work as advertised if you are using cygwin. |
Actually the code is very simple and I have modified it according to what I'm reading in this thread:
Code:
#!/bin/sh |
So are you saying that it works now, or that it still hangs?
|
No, as I said it hangs.
|
Have you tried the ftp directly from the command line? Does it works? Anyway, why don't you try the bye command instead of quit? Bye is the native command to terminate a session, and it is recognized from all versions of ftp, while quit is not accepted from some of them.
|
Again: why aren't you using wget ?
|
Yes, actually I'm using ftp itself everyday to manually pull some backup files. That's why I'm willing to write a script for that.
Ok.. I'll try bye command instead. jlliagre what benefit do I obtain from wget? |
All times are GMT -5. The time now is 08:42 AM. |