[英]How to pass arguments to .sql file from shell script
Hi there if anyone can help me, I have a .sh script that executes 4 .sql scripts, each executing against a schema. 嗨,如果有人可以帮助我,我有一个.sh脚本执行4 .sql脚本,每个脚本执行一个模式。 Currently the schema name is hardcoded but i want to make it configurable.
目前模式名称是硬编码的,但我想使其可配置。
Given the following below how will i pass the arguments from the shell script to the .files? 鉴于以下内容,我将如何将shell脚本中的参数传递给.files?
an eg call to a .sql is done in my shell script is done so like the following 例如,在我的shell脚本中完成对.sql的调用,如下所示
ECHO “DELETING SCHEME….”
psql -f $SCRIPT_DIR/delete_data.sql my_db postgres
ECHO “DATABASE SCHEMA DELETED..”
delete_data.sql delete_data.sql
drop schema my_schema cascade;
create schema my_schema;
You could replace the my_schema
part with a placeholder, like %SCHEMA%
: 您可以使用占位符替换
my_schema
部分,例如%SCHEMA%
:
drop schema %SCHEMA% cascade;
create schema %SCHEMA%;
We then run a substitution using sed
, and pipe the results into psql
(reading from stdin is equivalent to reading from file): 然后我们使用
sed
运行替换,并将结果传递给psql
(从stdin读取相当于从文件读取):
sed "s/%SCHEMA%/$schemaName/" $SCRIPT_DIR/delete_data.sql | psql powa_aim_db postgres
You can do this using a heredoc for your SQL: 你可以使用heredoc为你的SQL做到这一点:
my_schema="$1"
ECHO “DELETING SCHEME….”
psql <<SQL
drop schema $my_schema cascade
create schema $my_schema
SQL
ECHO “DATABASE SCHEMA DELETED..”
Then call your script with the schema name as the first argument: 然后使用模式名称作为第一个参数调用脚本:
$ ./my_script my_schema_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.