Create, validate and using dynamic variables in Bash scripting

Hi All,

I am really struggling to solve this problem, this might be small but I am not able to, can somebody help me?

I have few directories and these directories receives text files in large amount with in fraction of seconds. So I just want to send all the files in current directory to another server (Which is AS400) via FTP( I have to use FTP, no other tool). While sending file/s, if any failure occurs, need to retry 4 times. If still failures after 4th time, file should me moved to a directory called RETRYFAILED in same location. Once I moved to destination, I need to change the extension from txt to iur.
Directory structure is same at source and destination servers.
My problem is,

  1. How can I split a single script which contains repeated piece of code below?
  2. In Following code, I am taking one variable which includes current file name, retry$currentfile, but its not working as expected. That is not creating and initilazing say for example

currentfile="abcd.txt"
i'm creating and initializing dynamic variable like this - retryabcd=0
and checking if this variable value is less than 5, i'm reprocessing file, otherwise moving file to RETRYFAILED Directory in same location

For this I am using following code:

#!/bin/bash
# Author: Vasu
# Create Date: 11/20/2013

# host=$1
# uname=$2
# pwd=$3
# sdir=$4
# tdir=$5
# loglocation=$6

file_ext="iur"
delete_Files=""
function log {
echo "$(date +'%Y/%m/%d %H:%M:%S'): $*" >> ${log_file}
}
log_file="$loglocation/ShellScript-$(date +%Y%m%d_%H%M%S).log"
ftplogfile="FTP-OutPut-$(date +%Y%m%d_%H%M%S).log"
while true; ### This is endless loop, because I have many directories that need to run push like this, this is one of them
do 
	processed=0
	timestamp=$(date +%Y%m%d%H%M%S)
	while [ $processed -le 20 ] || [ $(date +%Y%m%d%H%M%S) - $timestamp -le 60 ]; 
	do
		tmpfilename=`ls -tr | head -n 1` ## taking with oldest time stamp
		currentfile=`rev <<< "$tmpfilename" | cut -d"." -f2- | rev`
		pwd=`pwd`
		dirname=`basename $pwd`
		if [ -f "$tmpfilename" ]; then
			if [[ -z 'eval \$retry$currentfile' ]]; then ### Here is my problem, I am checking if variable (retry$currentfile) is pre-exist, if not I am creating and assigning zero to that in next statement, but its not working
				eval export "retry${currentfile}=0"
			fi
			
			if [[ 'eval \$retry$currentfile' < 5 ]]; then ### here I am checking if the dynamic value that I created is less than 5
				echo "open $host
				user $uname $pwd
				binary
				cd /SabreIUR
				cd $dirname
				lcd $dirname
				put $tmpfilename
				rename $currentfile ${currentfile}$file_ext
				exit" > /tmp/ftp.$$
				ftp -ivn < /tmp/ftp.$$ > $ftplogfile
				ftpresult=$?
				bytesindatafile=`wc -c $currentfile | cut -d " " -f 1`
				bytestransferred=`grep -e '^[0-9]* bytes sent' $loglocation/$ftplogfile | cut -d " " -f 1`
				ftptransfercomplete=`grep -e '226 ' $loglocation/$ftplogfile | cut -d " " -f 1`
				if [ "$ftpresult" != "0" ] || [ "$bytestransferred" != "$bytesindatafile" ] || ["$ftptransfercomplete" != "226" ]
				then 
					echo "[ $ftpresult != 0 ] || [ $bytestransferred != $bytesindatafile ] || [$ftptransfercomplete != 226 ]"
					# currentfile_retry = `expr $currentfile_retry + 1`
					(( retry$currentfile++ ))
					log FTP Error occurred for the File : $currentfile
				else
					delete_Files+="`pwd`/$currentfile "
					log File: $tmpfilename Transfered via FTP and renamed to ${leftname}$file_ext
					#rm `pwd`/$currentfile
				fi
				# processed = `expr $processed + 1`
				(( processed++ ))
			else 
				if [ ! -d "RETRYFAILED" ]; then
					mkdir RETRYFAILED
				fi
				mv $tmpfilename RETRYFAILED/ 
			fi
		else
			echo "There are no file/s in $dirname"
		fi
	done
	sleep 60
done

Can some body please help me?

Thanks & Regards,
VasuBabu Kukkapalli.

eval = parse this line again.

Here sort example which maybe help you:

a=1
b=6
c=3
d=""


var=c  # change this testing a, b, c, d, e, ...

# take some variable value
eval val=\""\$$var"\"

# test that value
[ "$val" = "" ] && eval $var=0  && echo "$var set 0"

# take value again to the fixed variable - easier to handle this way
eval val=\""\$$var"\"

if ((val<5)) ; then
        eval echo \""$var<5"\"
        eval echo \""\$$var"\"
        eval \$$(($var++))  # add 1 to some variable
        eval echo \"+1 = "\$$var"\"

else
        eval echo \""$var>=5"\"
fi


You could use bash indirect expansion for this by replacing:

if [[ -z 'eval \$retry$currentfile' ]]; then
    eval export "retry${currentfile}=0"
fi
			
if [[ 'eval \$retry$currentfile' < 5 ]]; then

with below:

retry="retry$currentfile"
if [[ -z "${!retry}" ]]; then
    (($retry=0))
fi

if [[ ${!retry} < 5 ]]; then

Edit: also consider using currentfile=${tmpfilename%.*} as a replacement for

currentfile=`rev <<< "$tmpfilename" | cut -d"." -f2- | rev`