I have a file like this
> cat /tmp/lists
# user0 db0
# user1 db1
# user2 db2
I want to extract two arrays (column 2, column 3) from this, so I end up with roles=(role0 role1 role2), dbs=(db0 db1 db2). Finally I want to go through those like this.
for i in "${!roles[@]}" ; do
createdb "${dbs[$i]}" -O "${roles[$i]}"
done
which should create 3 dbs (db0, db1, db2) with it's respective owners (role0, role1, role2)
not I am certain this should work And I see that on one rhel9.6 machine, but on another rhel9.7 machine I fail with declaration of the arrays.
> dbs=($(grep ^# /tmp/lists | awk {'print $2'}))
-bash: eval: line 3: syntax error near unexpected token `('
-bash: eval: line 3: `echo dbs=($(grep ^# /tmp/lists | awk {'print $2'}))'
The declaration works, once I skip the enclosing () surrounding my grep command, like dbs=$(grep ^# /tmp/lists | awk {'print $2'})
But that does not provides a successful looping. I think because the arrays see all entries as one.
> for i in "${!dbs[@]}" ; do echo createdb ${dbs[$i]} -O ${roles[$i]} ; done
createdb db0 db1 db2 -O role0 role1 role2
Any idea, where the root cause may be lying? Maybe some issue with IFS ?
alpha,beta,gammasupposed to come out of that file?userprefix from entries such asuser0to derive0per yourroles=(0 1 2)?dbs=($(grep ^# /tmp/lists | awk {'print $2'})works? That can't work, you have an opening parenthesis with no closing one. Please show us your actual code, these details matter.-bash: eval: line 3:indicates at least 3 lines in your script and a call toeval... which doesn't match with the single line of code you've provided (dbs=($(grep ^# /tmp/lists | awk {'print $2'}))); as others have suggested, update the question with your actual code; we should be able to cut-n-paste your data + code into our environment and generate the same errors/wrong-output as you're seeing, if we can't reproduce your issues then we're left guessing at what your real problem may be