Shell Script - syntax error
Hi,
I am trying to upload multiple files from one folder to a ftp site and wrote this script: Quote:
Quote:
Thanks, Mayank |
Well, you haven't closed your 'for' loop. Does it run if you do?
|
Thanks for a quick response. I did add "done" at the end. However it is now giving following error
Quote:
Mayank |
Spacing after [ and before ] is mandatory in tests:
Code:
if [ -f /dir/$i ] |
Thanks again! I did add the space and now the script executes without any error. But nothing happens and files doens't get upload to ftp. Is there a way I can have a debug mode?
Mayank |
My scrip now looks like this
#!/bin/bash HOST='x.x.x.x' USER='user' PASSWD='password' DIR=archives for i in /dir/*; do if [ -f "$i" ]; then File=$i ftp -n "$HOST" <<END_SCRIPT quote USER $USER quote PASS $PASSWD ascii put $FILE quit END_SCRIPT fi done and I get following error: user@host:~/Documents/Python$ ./scritp.sh Bad sequence of commands. Bad sequence of commands. (local-file) (remote-file) Bad sequence of commands. Bad sequence of commands. (local-file) (remote-file) |
Please use [code][/code] tags and format the code as it is extremely difficult to read.
The error you now receive would seem to be coming from ftp, are you sure what you have is correct? Are you able to upload a single file using the same format on the command line? |
My apologies for not doing it. Following is the script
Quote:
Thanks in advance! Mayank |
Can I just confirm, when you say you can do it manually, this means you type exactly the following on the command line:
Code:
ftp -n "x.x.x.x" <<END_SCRIPT |
By manually I mean I login to the sftp server by executing "sftp uername@host", enter the password, navigate to the correct directory where the file needs to be uploaded and then execute put command to upload a file.
Thanks, Mayank |
Right ... but as that is not what you are doing in the script it is not the same. Remember, a script is only there to mitigate the excessive typing that one might need to do and hence automate part of the
task. You need to test each part of your script on the command line. Once you find the correct sequence of commands and data to enter, then you can look at putting it in a script. |
As grail asked, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Note again, code tags, NOT quote tags. Of course that assumes that you are using good formatting in the first place. Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line). Many people also think that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block. Code:
for var in fee fai foo fum ; do Finally, when using bash or ksh, it's recommended to use ((..)) for numerical tests, and [[..]] for string tests and other complex expressions. Avoid using the old [..] test unless you specifically need POSIX-style portability. http://mywiki.wooledge.org/ArithmeticExpression http://mywiki.wooledge.org/BashFAQ/031 http://wiki.bash-hackers.org/commands/classictest http://wiki.bash-hackers.org/syntax/...nal_expression Concerning the script itself, it generally looks ok to me. The only funny thing that I see offhand is that you set a "DIR" variable that you never use anywhere. It's also not advisable to store the username and password directly in the script. It would be better to either pass them to it from the command line, or store them in a separate file and source or read that into the script. |
All times are GMT -5. The time now is 11:57 AM. |