I have a bash script that is responsible for querying a database and piping the results to a file:
date=`date +"%Y/%m/%d"`
/usr/bin/mysql -u $db_user -p$db_pass -h $db_name $db_schema << MYSQLEOF > $output_file
select *
from table
where date = $date
MYSQLEOF
Now expanding this project into other areas, for the sake of re-usability/orthogonality I wanted to house the query in it's own file and call with something like:
cmd=`echo $sql_file`
/usr/bin/mysql -u $db_user -p$db_pass -h $db_name $db_schema -e "$cmd" > $output_file
sql file:
select *
from table
where date = $date
I am having trouble finding out how I can (in bash) do this, while still being able to have and adjust variables ($date) inside the sql file. Echoing the .sql file into a bash variable, I haven't been able to get "$date" to not be taken as a literal string.
Is there a solution in bash, or should I look into something like perl to handle this?