I have a question about Shell Variable substitution.
Requirement:
I need to insert something to mysql db.And I don’t want to write the insert script in main shell ,so I would like to define a insert sql script as a variable $SQL .
This variable and many other variables are stored in another file named sql.cfg
but this variable will refer another variable $Value.
when i execute the main shell, the $Value will be replace by actually value。
Example:
in sql.cfg
SQL="insert into table column1,column2 values($Value1,$Value2)"
DB_NAME=dbname
DB_IP=192.168.0.x
USR=username
PWD=password
in main.sh
. ./sql.cfg
Value1=100
Value2=200
` mysql $DB_NAME -h$DB_IP -u $USR -p$PWD -se "$SQL;"`
Question:
How can I substitute the $sql with $Value1 $Value2 properly so that I can insert them into to mysql db?
Thanks a lot!!!
you have to set
Value1,Value2in your main.sh before you load/source your sql.cfgexample:
And if you really want to load the sql.cfg first and set the value in your main.sh later. you could change the $Value1,2 in your sql.cfg as placeholders, and in main.sh substitute those values as you want. See the example: