In shell scripting I am retrieving data from database and trying to print it, but it does not print as I am trying to do it.
My code is:
mysql -uroot -proot -Dproject_ivr_db -rN --execute "SELECT Regular FROM
Fee_MSc WHERE Fee_Type='Total:'" | while read value
x=1
do
echo "V,FeeRegular_$y=$value"
let "y+=1"
done
echo "V,fee_msc_regular="for students on regular basis admission fee
including other charges is $FeeRegular_1 and semester fee is $FeeRegular_2""
Output is:
V,FeeRegular_1=12590
V,FeeRegular_2=12850
V,fee_msc_regular=for students on regular basis admission fee including other
charges is and semester fee is
It does not print the values of $FeeRegular_1 and $FeeRegular_2 in the string output.
How can I get the values of these variables in the output string?
Assuming a bash-like shell, the variables that you are looking to create do not exist when the
while-loop exists. This is because you are piping the results and creating a subshell with a new environment, which is not shared with the parent shell.You can restructure your loop to be a
for-loop using process substitution. Rather than attempting to append a number to the end of a variable name, use an array.I also recommend not using the root account for querying the database, increasing the strength of your passwords and not publishing them.
Now… are you sure the results you are looking for are in rows 1 and 2? You’ve specified no
order byclause in your query. But that’s a separate issue.