让我们考虑以下示例(从 psql 脚本开始):
\c :db_to_run_on
TRUNCATE the_most_important_table;
-- tried to avoid similarities to anything that exists out there
Run Code Online (Sandbox Code Playgroud)
现在,如果它通过命令运行
psql [connection details] -v db_to_run_on=\'dev_database\'
Run Code Online (Sandbox Code Playgroud)
然后它就会运行并且用户很高兴。但是,如果(她)他决定指定-v db_to_run_on=production_database
怎么办?(让我们假设这可能发生,就像人们rm -rf / # don't try this at home!!!
偶尔运行一样。)希望该表有一个新的备份......
所以问题出现了:如何检查传递给脚本的变量并根据它们的值停止进一步处理?