0

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

#!/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:

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 much appreciated.

Thanks, Mayank

maksaraswat
  • 1
  • 1
  • 2
  • The 'unexpected end of file' is because you have a `for` loop without a matching `done` at the end. – Charles Duffy Apr 24 '12 at 15:36
  • To make it easier for your helpers, please indent the control structure bodies of `for`, `if` etc. Thanks! – Jens May 04 '12 at 15:12

4 Answers4

1

It's complaining because your for loop does not have a done marker to indicate the end of the loop. You also need more spaces in your if:

if [ -f "$i" ]; then

Recall that [ is actually a command, and it won't be recognized if it doesn't appear as such.

And... if you single quote your glob (at the for) like that, it won't be expanded. No quotes there, but double quotes when using $i. You probably also don't want to include the /dir/ part when you use $i as it's included in your glob.

FatalError
  • 52,695
  • 14
  • 99
  • 116
  • I have made the changes you suggested and getting following error: **user@host:~/Documents/Python$ ./script.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)** – maksaraswat Apr 24 '12 at 16:13
0

If I'm not mistaken, ncftp can take wildcard arguments:

ncftpput -u username -p password x.x.x.x archives /dir/*

If you don't already have it installed, it's likely available in the standard repo for your OS.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
0

First, the literal, fixing-your-script answer:

#!/bin/bash
# no reason to set variables that don't change inside the loop
host='x.x.x.x'
user='username'
password='password'
dir=archives
for i in /dir/*; do # no quotes if you want the wildcard to be expanded!
  if [ -f "$i" ]; then # need double quotes and whitespace here!
    file=$i
    ftp -n "$host" <<END_SCRIPT
quote USER $user
quote PASS $password
ascii
put $file $dir/$file
quit    
END_SCRIPT
  fi
done

Next, the easy way:

lftp -e 'mput -a *.i' -u "$user,$password" "ftp://$host/"

(yes, lftp expands the wildcard internally, rather than expecting this to be done by the outer shell).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thanks everyone for all the suggestions. It really helps. I have changed my script to what Charles suggested and when I execute it I get following errors: username@host:~/Documents/Python$ ./script.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) – maksaraswat Apr 24 '12 at 15:59
  • @user1354065 FTP implementations vary, so no one script can be guaranteed to work across all of them. Might I suggest you try the lftp version? – Charles Duffy Apr 24 '12 at 16:17
  • @user1354065 ...that said, on review -- variable capitalization was inconsistent; the name was being assigned to `$File`, but `$FILE` was being dereferenced. For variables not exported into the environment, using all-uppercase is actually contrary to convention, so I've updated this to use all-lowercase. – Charles Duffy Apr 24 '12 at 16:21
  • Thanks Charles! I tried to execute the script the way as you updated above and it gives following message `user@host:~/Documents/Python$ ./updated.sh Bad sequence of commands. Bad sequence of commands. Bad sequence of commands. Bad sequence of commands.` – maksaraswat Apr 24 '12 at 17:13
  • I also changed the script to `#!/bin/bash host='x.x.x.x' user='uname' passwd='passwd' dir='archives/sfg' lftp -e 'mput -a /localdir/*.*' -u "$user,$passwd" "sftp://$host/"` and its getting hung up at following message `user@host:~/Documents/Python$ ./script.sh cd ok, cwd=/ `...abc.log' at 20 [Sending data]` and then I have to hit CTRL+C to stop it as it is taking forever. – maksaraswat Apr 24 '12 at 17:14
  • @maksaraswat err, sftp? that's a completely different protocol than ftp; which one do you actually want to use? – Charles Duffy Apr 24 '12 at 17:39
  • I can only login to the the site by using sftp ("sftp uname@host") and not ftp ("ftp uname@host"). So, it is sftp (my bad for not being clear). – maksaraswat Apr 24 '12 at 17:58
  • @maksaraswat ...then this entire question is misguided, since it starts out with FTP, not SFTP, as a premise. – Charles Duffy Apr 24 '12 at 18:06
0

First of all my apologies in not making myself clear in the question. My actual task was to copy a file from local folder to a SFTP site and then move the file to an archive folder. Since the SFTP is hosted by a vendor I cannot use the key sharing (vendor limitation. Also, SCP will require password entering if used in a shell script so I have to use SSHPASS. SSHPASS is in the Ubuntu repo however for CentOS it needs to be installed from here

Current thread and How to run the sftp command with a password from Bash script? did gave me better understanding on how to write the script and I will share my solution here:

#!/bin/bash
#!/usr/bin

for i in /dir/*; do 
 if [ -f "$i" ]; then 
file=$i
    export SSHPASS=password
    sshpass -e sftp -oBatchMode=no -b - user@ftp.com << !
    cd foldername/foldername
    put $file
 bye
 !
mv $file /somedir/test
fi
done

Thanks everyone for all the responses! --Mayank

Community
  • 1
  • 1
maksaraswat
  • 1
  • 1
  • 2