LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-24-2012, 10:06 AM   #1
maksaraswat
LQ Newbie
 
Registered: Jul 2011
Posts: 15

Rep: Reputation: Disabled
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
 
Old 04-24-2012, 10:09 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Well, you haven't closed your 'for' loop. Does it run if you do?
 
Old 04-24-2012, 10:23 AM   #3
maksaraswat
LQ Newbie
 
Registered: Jul 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-24-2012, 10:27 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Spacing after [ and before ] is mandatory in tests:
Code:
if [ -f /dir/$i ]
 
Old 04-24-2012, 10:39 AM   #5
maksaraswat
LQ Newbie
 
Registered: Jul 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-24-2012, 11:10 AM   #6
maksaraswat
LQ Newbie
 
Registered: Jul 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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)
 
Old 04-24-2012, 11:27 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
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?
 
Old 04-24-2012, 11:57 AM   #8
maksaraswat
LQ Newbie
 
Registered: Jul 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-24-2012, 12:36 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
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?
 
Old 04-24-2012, 12:51 PM   #10
maksaraswat
LQ Newbie
 
Registered: Jul 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-24-2012, 01:00 PM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
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.
 
Old 04-24-2012, 02:29 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
 
  


Reply

Tags
error, ftp, shell script, upload


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script.pl with sed shell calls: sh error syntax error near unexpected token `(' MMaddoxx Programming 7 11-24-2011 08:00 AM
Shell script (iptables) syntax error mrmnemo Programming 6 12-04-2009 08:57 AM
why is my shell script giving me - syntax error near unexpected token 'enterInfo() chisunum Linux - Newbie 3 10-23-2009 03:37 PM
shell script help required syntax error sridhar11 Programming 1 10-31-2005 08:38 AM


All times are GMT -5. The time now is 09:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration