Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop through array to assign dynamic variable with results from 'find'

I’m trying to assign the results of find to a dynamic variable using the entries of an array both as the variable name and search string.

databases=(ABC DEF GHI JKL)
for i in "${databases[@]}"
do
    schema_$i=$(find '/hana/shared/backup_service/backup_shr/Schema' -type d -iname ${i} -mtime 0)
done

The result should be 0 to 4 variables that look something like this (depending on how many folders were found):

schema_ABC=/hana/shared/backup_service/backup_shr/Schema/ABC

However when I try this I get ‘No such file or directory’ as an error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

./find_schema.sh: line 4: schema_ABC=/hana/shared/backup_service/backup_shr/Schema/ABC: No such file or directory
./find_schema.sh: line 4: schema_DEF=/hana/shared/backup_service/backup_shr/Schema/DEF: No such file or directory
./find_schema.sh: line 4: schema_GHI=: command not found
./find_schema.sh: line 4: schema_JKL=/hana/shared/backup_service/backup_shr/Schema/JKL: No such file or directory

The folder structure looks like this:

Server:/hana/shared/backup_service/backup_shr/Schema # ll
total 0
drwxr-xr-x 2 root root 0 Sep 29  2020 Test
drwxr-xr-x 2 root root 0 Jan 24 21:15 ABC
drwxr-xr-x 2 root root 0 Jan 24 21:30 DEF
drwxr-xr-x 2 root root 0 Jan 12 22:00 GHI
drwxr-xr-x 2 root root 0 Jan 24 21:45 JKL

I believe that I’m not declaring that variable correctly but I can’t make out what’s wrong.

>Solution :

Consider:

schema_$i=$(cmd)

When bash parses that line, it first checks for variable assignments. Since $ is not a valid token in a variable name, it does not see any variable assignments. Then it expands the string schema_$i to the string schema_ABC (or whatever the current value of $i is). Then it executes cmd to get some output, and then it attempts to execute the command schema_ABC=foo, but fails to find a command that matches that name. Intuitively, you are trying to make bash evaluate the string schema_ABC=foo, in which case you would need to tell it to do so explicitly with eval "schema_$i=$(find ...)". But you really don’t want to go down that rabbit hole.

Instead, you could use an associative array and do something like:

declare -A schema
databases=(ABC DEF GHI JKL)
for i in "${databases[@]}"
do
    schema[$i]=$(...) 
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading