-1

I am reading DB properties from databse.prop file and below is the code.

....
 for (( i=1;i<=$Dbcount;i++ ))
                        do
echo "$DBCONNECTIONNAME_${i}"                 
sqlplus -S ${user1}@$DBconnection_${i}/${Password} <<EOF &
spool sqlcsvdb_"$i".csv
                         @squery.sql $para1 $para2
                          exit
EOF
done

Properties file. ....

 DBcount=2
    user1=user
    Password=password
    DBconnection1=Connection1
    DBconnection2=Connection2

..... I am getting the $DBconection_$i variable output as 1, 2 as looping and getting error message as bad substitution, plese help me on this.

Naidu5
  • 17
  • 2
  • 8
  • This looks like a job for an array (or possibly indirect expansion, but an array's probably better). This is a possible duplicate of ["Is it possible to build variable names from other variables in bash?"](https://stackoverflow.com/questions/3963494/is-it-possible-to-build-variable-names-from-other-variables-in-bash) (which is marked as a duplicate of ["Dynamic variable names in Bash"](https://stackoverflow.com/questions/16553089/dynamic-variable-names-in-bash), but you don't need an associative array). Also, see [BashFAQ #006](http://mywiki.wooledge.org/BashFAQ/006). – Gordon Davisson Aug 16 '20 at 07:20
  • ... Though a better solution if the suffix is a running number is to simply use an array. Bash 5+ has associative arrays, too. – tripleee Aug 17 '20 at 04:21

1 Answers1

1

After source "properties file" you have the variables you want, and you can do

for ((i=1;i<=DBcount;i++)); do
   conn=DBconnection${i} # you need an extra var
   echo "${!conn}"
done

Another possibility is getting the value inside the loop:

for ((i=1;i<=$Dbcount;i++ )); do
   conn=$(sed -n "/DBconnection${i}=/ s/.*=//p" "properties file")
   echo "${conn}"
   sqlplus -S ${user1}@${conn}/${Password} <<EOF &
spool sqlcsvdb_"$i".csv
@squery.sql $para1 $para2
exit
EOF
done
Walter A
  • 19,067
  • 2
  • 23
  • 43