1

i'm trying to generate a script that ftp some files to a server using lftp. when i run these commands in shell:

DBNAME=TESTDB  
ls -t /data*/${DBNAME,,}Backup/$DBNAME.0.db21.DBPART000.`date +%Y%m%d`*

i get 2 paths:

/data4/testdbBackup/TESTDB.0.db1.DBPART000.20191007010004.001
/data5/testdbBackup/TESTDB.0.db1.DBPART000.20191007010004.002

but when i use this command to create an array and loop through it i only get the first element. here is the script:

echo "lftp -u $FTPUSER,$FTPPASSWD $FTPSRV  <<end_script
mkdir BackUp
cd BackUp
mkdir $CURRENTDATE
cd $CURRENTDATE
mkdir $IP
cd $IP " >> $FTPFILES
for DBNAME in "${DBNAME_ARRAY[@]}"
do
BACKUP_FILE_COUNT=$(ls -t /data*/${DBNAME,,}Backup/$DBNAME.0.db21.DBPART000.`date +%Y%m%d`*|wc -l)
COUNTER=($(echo $COUNTER + $BACKUP_FILE_COUNT | bc))
mapfile -t BACKUP_FILE_ARRAY < <(ls -t /data*/${DBNAME,,}Backup/$DBNAME.0.db21.DBPART000.`date +%Y%m%d`*)
    for BACKUP_FILE in "${BACKUP_FILE_ARRAY=[@]}"
            do
            echo "lcd $(dirname $BACKUP_FILE)" >> $FTPFILES
            echo "put $(basename $BACKUP_FILE)" >> $FTPFILES
    done
done
echo "quit
end_script
exit 0  " >> $FTPFILES

the output of this script is:

 lftp -u someuser,somepassword 1.1.1.1  <<end_script
 mkdir BackUp
 cd BackUp
 mkdir 19-10-07
 cd 19-10-07
 mkdir 192.168.22.22
 cd 192.168.22.22
 lcd /data4/testdbBackup
 put TETSTDB.0.db21.DBPART000.20191007010004.001
 quit
 end_script
 exit 0  

in that part of changing directories i expect this:

lcd /data4/testdbBackup
put TETSTDB.0.db21.DBPART000.20191007010004.001
lcd /data5/testdbBackup
put TETSTDB.0.db21.DBPART000.20191007010004.002

i also added a echo "${BACKUP_FILE_ARRAY=[@]}" to my script and it only has one element.
i had this problem before in this question and i used the solution in many scripts and they worked perfectly. what am i missing here?

1 Answers1

2

"${BACKUP_FILE_ARRAY=[@]}" has one too many = in it.

Also, to set the array, don't use mapfile. Just use the shell pattern:

BACKUP_FILE_ARRAY=( /data*/"${DBNAME,,}"Backup/"$DBNAME.0.db21.DBPART000.$(date +%Y%m%d)"* )

(if $CURRENTDATE is set with CURRENTDATE=$(date +%Y%m%d) somewhere at the top of the script, then use that variable instead of the command substitution with date, so that the script is not confused if it happens to run across midnight).

Using the output of ls is a bit problematic in the general case, as it disqualifies the script from working with some filenames. It also makes the script difficult to read.

To count the number of files that match that pattern, first create the array, then use

BACKUP_FILE_COUNT=${#BACKUP_FILE_ARRAY[@]}

to get the number of elements in it.

To add this number to COUNTER, use a standard arithmetic expansion:

COUNTER=$(( COUNTER + BACKUP_FILE_COUNT ))

If you don't know when you need to double quote a variable's expansion and when it's not necessary to do so, opt for using double quotes (as in "$myvar"), or you will likely run into issues when using variables whose values contain whitespace or shell globbing patterns.

Related:

Kusalananda
  • 333,661