Transform string into array Shell Script

4

I'm trying to make a script to backup my database.

But I wanted to not need to provide the bases name when I run the script, but save each base in a different file.

My script looks like this:

#!/bin/bash

BASES= mysql -u *** -p"***" -B -N -e "SHOW DATABASES"    
IFS='\n ' read -r -a array <<< "$BASES"    
echo $BASES
echo $IFS

In the first line I'm listing the bases and saving in the variable BASES . The second line, I tried to break the variable in an array separating by the line break but it did not work.

The output of BASES looks like this:

base1
base2
base3
.
.
.

How to turn this output into an array?

    
asked by anonymous 27.01.2017 / 11:23

1 answer

1

Do this:

ARRAY=()

while read line
do
   [[ "$line" != '' ]] && ARRAY+=("$line")
done <<< "$BASES"

To check the array:

for x in "${ARRAY[@]}"
do
   echo "$x"
done
    
20.03.2017 / 12:57