Syntax error: Bad for loop variable

I'm getting an error while running this script. Need help.

set -x verbose #echo on
clear #clear the screen

USERNAME="bbb"
PASSWORD="password"
SERVER="192.168.1.100"
WAIT_TIME=300
FILE_PATH="/home/users/xxx/MMM" # local directory to pickup *.dat file
REMOTE_PATH="/Drop_off/xxx/yyy" # remote server directory to upload file

#Process files
for file in $FILE_PATH/*.dat
do
	echo "Processing $file ..."
	# take action on each file. f store current file name
	./UFTFTP_SecondScript $file
	echo "Seconds until next uploadls : $WAIT_TIME"
	for (( x=0; x<$WAIT_TIME"; x++ ))
	do
		echo "Sleeping for " $x
		sleep 1
	done
done

-----------UFTFTP_SecondScript------------

#!/bin/sh

set -x verbose #echo on

#CURRENT PROCESSING FILE: 1
#USERNAME: 2
#PASSWORD: 3
#SERVER: 4
#FILE_PATH: 5
#REMOTE_PATH: 6

#login to remote server
ftp -n -i $4 
<<END_INPUT
	user $2 $3
	cd $6
	mput $5/$1 
	quit
END_INPUT

Hi.

Did you miss a quote, on this line:

for (( x=0; x<$WAIT_TIME"; x++ ))

I removed the quote but i'm still having the same error.

What's your system? What's your shell? Not all shells have for ((x=0; x<5; x++)) syntax

You could do

X=0
while [ "$X" -lt "$WAIT_TIME" ]
do
...
        X=`expr $X + 1`
done

I noticed that I wasn't passing all parameters but I'm still having the same issue.

USERNAME="bbb"
PASSWORD="password"
SERVER="192.168.1.100"
WAIT_TIME=300
FILE_PATH="/home/users/xxx/MMM" # local directory to pickup *.dat file
REMOTE_PATH="/Drop_off/xxx/yyy" # remote server directory to upload file

#Process files
for file in $FILE_PATH/*.dat
do
	echo "Processing $file ..."
	# take action on each file. f store current file name
	./UFTFTP_SecondScript $file $USERNAME $PASSWORD $SERVER $FILE_PATH $REMOTE_PATH
	echo "Seconds until next uploadls : $WAIT_TIME"
	for (( x=0; x<$WAIT_TIME; x++ ))
	do
		echo "Sleeping for " $x
		sleep 1
	done
done

---------- Post updated at 03:11 PM ---------- Previous update was at 03:09 PM ----------

Shell: /bin/bash
System: lububtu

You could try x<WAIT_TIME instead of $WAIT_TIME. Inside (( )) the $ shouldn't be necessary.

Or you could try my loop above

What is the value of WAIT_TIME? Make sure the value contains no spaces, etc. which might interfere with comparison

WAIT_TIME=300

---------- Post updated at 03:56 PM ---------- Previous update was at 03:16 PM ----------

I tried with your loop and it worked.

#Process files
for file in $FILE_PATH/*.dat
do
	echo "Processing $file ..."
	# take action on each file. f store current file name
	./UFTFTP_SecondScript.sh $file $USERNAME $PASSWORD $SERVER $FILE_PATH $REMOTE_PATH
	echo "Seconds until next upload: $WAIT_TIME"
	#for (( x=0; x<$WAIT_TIME; x++ ))
	x=0
	while [$x -lt $WAIT_TIME];
	do
		echo "Sleeping for $x"
		sleep 1
		x='expr $x + 1'
	done
done

Now the problem is connecting to the ftp.

+ ftp -n -i
Not connected.
Not connected.
Not connected.
+ echo Seconds until next upload: 300
Seconds until next upload: 300
+ x=0
+ [0 -lt 300]
./UploadFileToFtp.sh: 1: [0: not found

It looks like you messed up a [ ] type of comparison statement, but there's nothing like that in the ftp script you posted, could you post it in complete?

Most of the recent error messages come from this line:

There must be a space character after a [ and before a ]. The semicolon at the end of the line is not required at all.

	while [ $x -lt $WAIT_TIME ]

The FTP script has some issues: The << "Start of Here Document" is misplaced. Also why "mput" when the calling script logic is for one file? We need to use "lcd" to change local directory. Don't indent "Here Documents" - it will bite you in the hand.

#!/bin/sh

set -x verbose #echo on

#CURRENT PROCESSING FILE: 1
#USERNAME: 2
#PASSWORD: 3
#SERVER: 4
#FILE_PATH: 5
#REMOTE_PATH: 6

#login to remote server
ftp -n -i $4 <<END_INPUT
user $2 $3
cd $6
lcd $5
put $1
quit
END_INPUT 

In post #4 you were asked what is your shell and what is your system. Please post (blotting anything confidential with X's):

uname -a
echo ${SHELL}

Linux lubuntu 3.0.0-14-generic #23-Ubuntu SMP Mon Nov 21 20:34:47 UTC 2011 i686 i686 i386 GNU/Linux

/bin/bash

If the pair of scripts are still giving trouble, please post the current version of both scripts and any error execution messages.

Current Code

-----FirstFile-----

#!/bin/bash

set -x verbose #echo on
clear #clear the screen

USERNAME="xxxx"
PASSWORD="xxxx"
SERVER="192.168.225.1"
WAIT_TIME=300
FILE_PATH="/home/user/Desktop/XXX" # local directory to pickup *.dat file
REMOTE_PATH="/Drop_off/Region_3_prod/XXX" # remote server directory to upload backup

#Process files
for file in $FILE_PATH/*.dat
do
	# take action on each file. [file] store current file name
	./UFTFTP_SecondScript $file $USERNAME $PASSWORD $SERVER $REMOTE_PATH
	echo "Seconds until next upload: $WAIT_TIME"
	x=0
	while [ $x -lt $WAIT_TIME ]
	do
		echo "Sleeping for $x..."
		sleep 1
		x='expr $x + 1'
	done
done

-----SecondFile-----

#!/bin/bash

set -x verbose #echo on

#file: 1
#USERNAME: 2
#PASSWORD: 3
#SERVER: 4
#REMOTE_PATH: 5

#login to remote server
echo "Processing file $1..."
ftp -n -i $4 <<END_INPUT
user $2 $3
cd $5
put $1 
quit
END_INPUT

---------- Post updated at 10:13 AM ---------- Previous update was at 10:10 AM ----------

+ clear

+ USERNAME=XXX
+ PASSWORD=XXX
+ SERVER=192.168.225.1
+ WAIT_TIME=300
+ FILE_PATH=/home/user/Desktop/XXX
+ REMOTE_PATH=/Drop_off/Region_3_prod/XXX
+ for file in '$FILE_PATH/.dat'
+ ./UFTFTP_SecondScript '/home/user/Desktop/XXX/
.dat' XXX XXX 192.168.225.1 /Drop_off/Region_3_prod/XXX
./UploadFileToFtp.sh: line 25: ./UFTFTP_SecondScript: No such file or directory
+ echo 'Seconds until next upload: 300'
Seconds until next upload: 300
+ x=0
+ '[' 0 -lt 300 ']'
+ echo 'Sleeping for 0...'
Sleeping for 0...
+ sleep 1
+ x='expr $x + 1'
+ '[' expr '$x' + 1 -lt 300 ']'
./UploadFileToFtp.sh: line 28: [: too many arguments

x='expr $x + 1'

This sets x to the literal string 'expr $x + 1' which naturally doesn't work when you compare it to an integer.

I think you meant to use backticks.

Thanks,

while [ $x -lt $WAIT_TIME ]
	do
		echo "Sleeping for $x..."
		sleep 1
		x=`expr $x + 1` # " ` " use backticks not " ' " apostrophe
	done
#!/bin/bash
#Filename: UploadFileToFtp.sh
################################COMMENTS################################
#hacer los siguiente hasta que se acaben los archivos:
# leer el path de un archivo
# subirlo al ftp
# mover el archivo que se subio a submitted folder
# esperar 5 minutos (300 segundos)
################################COLORS##################################
#Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed 

#Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white

#Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#########################################################################

#set -x verbose #echo on everything
set -o errexit

clear #clear the screen

USERNAME="z"
PASSWORD="z"
SERVER="z"
WAIT_TIME=1
FILE_PATH="/home/user/Desktop/MMM" # local directory to pickup *.dat file
REMOTE_PATH="/Drop_off/Region_3_prod/EDI" # remote server directory to upload backup
FILES_PROCESSED=0

echo -e "\e[01;34mStarting process...\e[0m"
echo "Using the following values:"
echo "Username: $USERNAME"
echo "Password: $PASSWORD"
echo "Server: $SERVER"
echo "Wait time: $WAIT_TIME seconds"
echo "Remote Path: $REMOTE_PATH"

#Process files
for file in $FILE_PATH/*.dat
do
	# take action on each file. [file] stores current file name
	./UFTFTP_SecondScript.sh $file $USERNAME $PASSWORD $SERVER $REMOTE_PATH
	echo "Seconds until next upload: $WAIT_TIME"
	x=0
	while [ $x -lt $WAIT_TIME ]
	do
		echo -ne "Sleeping for ...`expr $WAIT_TIME - $x`\r"
		sleep 1
		x=`expr $x + 1` # " ` " use backticks not " ' " apostrophe
	done
FILES_PROCESSED=`expr $FILES_PROCESSED + 1`
echo "Procesing file number: $FILES_PROCESSED"
done
echo -e "\e[01;33
mTotal number of files processed: $FILES_PROCESSED\e[0m"
#!/bin/bash
#filename: UFTFTP_SecondScript.sh
################################COLORS##################################
#Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed 
#
#Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
#
#Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#########################################################################

#set -x verbose #echo on

#file: 1
#USERNAME: 2
#PASSWORD: 3
#SERVER: 4
#REMOTE_PATH: 5

#login to remote server
echo -e "\e[01;31mConnecting to server...\e[0m"
echo -e "\e[01;34mProcessing file $1...\e[0m"
ftp -n -i $4 <<END_INPUT
user $2 $3
cd $5
put $1 
quit
END_INPUT
echo -e "\e[01;32mFinihed procesing file $1...\e[0m"

Execution error:

Starting process...
Using the following values:
Username: z
Password: z
Server: z
Wait time: 1 seconds
Remote Path: /Drop_off/Region_3_prod/EDI
Connecting to server...
Processing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837IEDIMMM20111117193200001v1-5010-50.dat...
Filename invalid
Finihed procesing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837IEDIMMM20111117193200001v1-5010-50.dat...
Seconds until next upload: 1
Procesing file number: 1
Connecting to server...
Processing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837IEDIPMC20111117194600001v1-5010-50.dat...
Filename invalid
Finihed procesing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837IEDIPMC20111117194600001v1-5010-50.dat...
Seconds until next upload: 1
Procesing file number: 2
Connecting to server...
Processing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837PEDIMMM20111117195600001v1-5010.dat...
Filename invalid
Finihed procesing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837PEDIMMM20111117195600001v1-5010.dat...
Seconds until next upload: 1
Procesing file number: 3
Connecting to server...
Processing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837PEDIMMM20111117200200003v1-5010-50.dat...
Filename invalid
Finihed procesing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837PEDIMMM20111117200200003v1-5010-50.dat...
Seconds until next upload: 1
Procesing file number: 4
Connecting to server...
Processing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837PEDIPMC20111117194900001v1-5010-50.dat...
Filename invalid
Finihed procesing file /home/user/Desktop/MMM/EDEInmediataInboundClaims837PEDIPMC20111117194900001v1-5010-50.dat...
Seconds until next upload: 1
Procesing file number: 5
Total number of files processed: 5
user@lubuntu:~/Documents/Projects/Bash/UploadWithDelayToFtp$

The problem is in this line:

In your example $1 contains a full hierarchial path name "put" defaults the "to filename" to be the same as the "from filename". Thus the full hierarchial filename is invalid on the destination system.

You need to strip the directory portion off the filename with a command such as "basename", then use both "lcd" an "cd" within the ftp script.
Something like this:

# In the script (before executing the ftp)
local_filename=`basename $1`
local_dir=`dirname $1`
remote_dir=$5
#Then in the ftp portion of the script:
lcd ${local_dir}
cd ${remote_dir}
put ${local_filename}

You need a space between the variable and the bracket

Thanks methyl,

Final working code:

#!/bin/bash
################################COLORS##################################
#Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed 
#
#Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
#
#Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#########################################################################

#set -x verbose #echo on

#file: 1
#USERNAME: 2
#PASSWORD: 3
#SERVER: 4
#REMOTE_PATH: 5
LOCAL_FILENAME=`basename $1`
LOCAL_DIR=`dirname $1`

#login to remote server
echo -e "\e[01;31mConnecting to server...\e[0m"
echo -e "\e[01;34mLocal Directory $LOCAL_DIR...\e[0m"
echo -e "\e[01;34mProcessing file $LOCAL_FILENAME...\e[0m"
ftp -n -i $4 <<END_INPUT
user $2 $3
lcd $LOCAL_DIR
cd $5
put $LOCAL_FILENAME 
quit
END_INPUT
echo -e "\e[01;32mFinihed procesing file $LOCAL_FILENAME...\e[0m"

OUTPUT

Starting process...
Using the following values:
Username: zzzz
Password: zzzz
Server: xxx.xxx.xxx.xxx
Wait time: 5 seconds
Remote Path: /Drop_off/Region_3_prod/EDI
Connecting to server...
Local Directory /home/user/Desktop/MMM...
Processing file EDEInmediataInboundClaims837.dat...
Local directory now /home/user/Desktop/MMM
Finihed procesing file EDEInmediataInboundClaims837.dat...
Seconds until next upload: 5
Procesing file number: 1
Connecting to server...
Local Directory /home/user/Desktop/MMM...
Processing file EDEInmediataInboundClaims837IEDIMMM20111117193200001v1-5010-50.dat...
Local directory now /home/user/Desktop/MMM
Finihed procesing file EDEInmediataInboundClaims837IEDIMMM20111117193200001v1-5010-50.dat...
Seconds until next upload: 5
Procesing file number: 2
Connecting to server...
Local Directory /home/user/Desktop/MMM...
Processing file EDEInmediataInboundClaims837IEDIPMC20111117194600001v1-5010-50.dat...
Local directory now /home/user/Desktop/MMM
Finihed procesing file EDEInmediataInboundClaims837IEDIPMC20111117194600001v1-5010-50.dat...
Seconds until next upload: 5
Procesing file number: 3
Connecting to server...
Local Directory /home/user/Desktop/MMM...
Processing file EDEInmediataInboundClaims837PEDIMMM20111117195600001v1-5010.dat...
Local directory now /home/user/Desktop/MMM
Finihed procesing file EDEInmediataInboundClaims837PEDIMMM20111117195600001v1-5010.dat...
Seconds until next upload: 5
Procesing file number: 4
Connecting to server...
Local Directory /home/user/Desktop/MMM...
Processing file EDEInmediataInboundClaims837PEDIPMC20111117194900001v1-5010-50.dat...
Local directory now /home/user/Desktop/MMM
Finihed procesing file EDEInmediataInboundClaims837PEDIPMC20111117194900001v1-5010-50.dat...
Seconds until next upload: 5
Procesing file number: 5

Total number of files processed: 5