Create, validate and using dynamic variables in Bash scripting

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create, validate and using dynamic variables in Bash scripting
# 1  
Old 01-11-2014
CPU & Memory 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

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:

# Author: Vasu
# Create Date: 11/20/2013

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

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
	timestamp=$(date +%Y%m%d%H%M%S)
	while [ $processed -le 20 ] || [ $(date +%Y%m%d%H%M%S) - $timestamp -le 60 ]; 
		tmpfilename=`ls -tr | head -n 1` ## taking with oldest time stamp
		currentfile=`rev <<< "$tmpfilename" | cut -d"." -f2- | rev`
		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"
			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
				cd /SabreIUR
				cd $dirname
				lcd $dirname
				put $tmpfilename
				rename $currentfile ${currentfile}$file_ext
				exit" > /tmp/ftp.$$
				ftp -ivn < /tmp/ftp.$$ > $ftplogfile
				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" ]
					echo "[ $ftpresult != 0 ] || [ $bytestransferred != $bytesindatafile ] || [$ftptransfercomplete != 226 ]"
					# currentfile_retry = `expr $currentfile_retry + 1`
					(( retry$currentfile++ ))
					log FTP Error occurred for the File : $currentfile
					delete_Files+="`pwd`/$currentfile "
					log File: $tmpfilename Transfered via FTP and renamed to ${leftname}$file_ext
					#rm `pwd`/$currentfile
				# processed = `expr $processed + 1`
				(( processed++ ))
				if [ ! -d "RETRYFAILED" ]; then
					mkdir RETRYFAILED
				mv $tmpfilename RETRYFAILED/ 
			echo "There are no file/s in $dirname"
	sleep 60

Can some body please help me?

Thanks & Regards,
VasuBabu Kukkapalli.

Last edited by Don Cragun; 01-11-2014 at 02:38 AM.. Reason: Add CODE tags.
# 2  
Old 01-11-2014
eval = parse this line again.

Here sort example which maybe help you:

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"\"

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

# 3  
Old 01-13-2014
You could use bash indirect expansion for this by replacing:

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

with below:

if [[ -z "${!retry}" ]]; then

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

Edit: also consider using currentfile=${tmpfilename%.*} as a replacement for
currentfile=`rev <<< "$tmpfilename" | cut -d"." -f2- | rev`

Last edited by Chubler_XL; 01-13-2014 at 11:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

Generating dynamic variables

Hi , i am unable to generate dynamic variables can any one please help me on the below issue j=1 {record_count_"$j"}=`db2 -xselect substr\(job_name,24\) rec_count from $libname.audit_table_nrt where job_name like \'DATAMART_DEL_RUN%\' and STS_FLAG=\'E\' and seq_no=$i` echo " record... (3 Replies)
Discussion started by: bhaskar v
3 Replies

3. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

4. Shell Programming and Scripting

How to Create LOG and Validate for this Script

Hi all, This is my Script #!/bin/bash current_date=`date +%d-%m-%Y_%H.%M.%S` Date=`date +%d%m%Y` Lfriday=`date +%d%m%Y -d last-friday` RootPath=/var/domaincount/com mysql zonefile<<EOFMYSQL CREATE TABLE com$Date(domain VARCHAR(255),col2 VARCHAR(255),col3 VARCHAR(255),col4... (10 Replies)
Discussion started by: anishkumarv
10 Replies

5. Shell Programming and Scripting

Defining Dynamic Number of Variables in a Bash Script

Code: $ cat test.bash #!/bin/bash job=$1 steps=$2 num=$(echo "$@" | wc -w) Example Submission: $ ./test.bash BS01 3 1 2 3 What: (2 Replies)
Discussion started by: mkastin
2 Replies

6. Shell Programming and Scripting

Dynamic variables and eval

Hi, I need some direction with the following. The below code is semi-psuedo code which will hopefully make it easier to understand what I am trying to achieve: for i in `echo ${testarray }` do let c=c+1 eval "first$c=$i" while... (4 Replies)
Discussion started by: yonderboy
4 Replies

7. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

8. Shell Programming and Scripting

Validate Variables insert from user

Hi Can you help me validate 2 variables?The first is an input date from the user and should be like this (yyyy-mm-dd). The second variable is an input time from the user and should be like this(hh:mm).When the input is wrong i want to give the chance to the user to insert again the date or time... (8 Replies)
Discussion started by: DDoS
8 Replies

9. UNIX for Dummies Questions & Answers

dynamic variables

I am new to unix and the following problem is bugging me.:confused: var1="hello1" var2="hello2" var3="hello3" counter=1 while do echo $var$counter done the idea here is to display the value of "var" based on the counter. I am using the korn shell. I used array here but the... (4 Replies)
Discussion started by: max_payne1234
4 Replies

10. UNIX for Dummies Questions & Answers

server variables in bash scripting

are they any server variables in bash/SSH scripting as is in PHP or Perl, for example in PHP: $_SERVER or $_SERVER I'm trying to retrieve the absolute path of an .SH script I'm running thank you (6 Replies)
Discussion started by: basher400
6 Replies
Login or Register to Ask a Question