Array only returns one element

BlackCrystal

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?

Kusalananda

"${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:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript array only running function that returns a promise on one element of array

Only one element is added to the array

Significance of array with only one element

Hibernate manyToMany only returns one element

HttpWebResponse returns only one element of the page

Selector in Puppeteer only returns one element

Function that recieves an array and returns if it can be made strictly increasing by removing only one of it's element

Javascript array with for loop, returns only last element

String Array only returns one string in android

Parsing HTML to array only returns one word

jsoncpp write json array with only one element

Only one array element is passed to my asynctask

Mule - XML Array only reads one element

Does forEach not run if there is only one element in an array?

React trigger only one element in array

Java JComboBox array only shows one element

only one element in numpy array shape

How to set only ONE element in an array in firebase

Pull only one element from and Array in Mongoose

my php array only displays one element

Array List Taking only One element

array.forEach() only returning one element

JOOQ: fetchGroups() always returns list with only one element

ADF Pipeline Json function only returns one element?

heapq only returns one smallest element and repeats in n times

for loop returns only first array element in bash script

Deserializing XML, where the root object is an array returns only a single element

mysqli_fetch_assoc returns ONLY the first element of the array

Dynamic array allocation returns only last element in all indices C

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive