LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script - syntax error (https://www.linuxquestions.org/questions/programming-9/shell-script-syntax-error-941494/)

maksaraswat 04-24-2012 10:06 AM

Shell Script - syntax error
 
Hi,

I am trying to upload multiple files from one folder to a ftp site and wrote this script:
Quote:

#!/bin/bash
for i in '/dir/*'
do
if [-f /dir/$i]; then
HOST='x.x.x.x'
USER='username'
PASSWD='password'
DIR=archives
File=$i

ftp -n $HOST << END_SCRIPT
quote USER $USER
quote PASS $PASSWD
ascii
put $FILE
quit
END_SCRIPT
fi

It is giving me following error when I try to execute:
Quote:

username@host:~/Documents/Python$ ./script.sh
./script.sh: line 22: syntax error: unexpected end of file
I can't seem to get this to work. Any help is appreciated.

Thanks,
Mayank

Snark1994 04-24-2012 10:09 AM

Well, you haven't closed your 'for' loop. Does it run if you do?

maksaraswat 04-24-2012 10:23 AM

Thanks for a quick response. I did add "done" at the end. However it is now giving following error

Quote:

username@host:~/Documents/Python$ ./script.sh
.script.sh: line 4: [-f: command not found
Thanks,
Mayank

colucix 04-24-2012 10:27 AM

Spacing after [ and before ] is mandatory in tests:
Code:

if [ -f /dir/$i ]

maksaraswat 04-24-2012 10:39 AM

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

maksaraswat 04-24-2012 11:10 AM

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)

grail 04-24-2012 11:27 AM

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?

maksaraswat 04-24-2012 11:57 AM

My apologies for not doing it. Following is the script
Quote:

#!/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
Yes, I am able to upload and delete the files manually.


Thanks in advance!

Mayank

grail 04-24-2012 12:36 PM

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
quote USER user
quote PASS password
ascii
put file_name
quit
END_SCRIPT

Obviously you need to fill in the relevant details, but using the above format on the command line has the correct result?

maksaraswat 04-24-2012 12:51 PM

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

grail 04-24-2012 01:00 PM

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.

David the H. 04-24-2012 02:29 PM

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

        if [[ "$var" == "foo" ]]; then
                echo "Found 'foo'."
        fi

done

Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

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.