我在Makefile中有以下内容,用于重新创建我的数据库,包括在必要时销毁它.这是行不通的.
.PHONY: rebuilddb
exists=$(psql postgres --tuples-only --no-align --command "SELECT 1 FROM pg_database WHERE datname='the_db'")
if [ $(exists) -eq 1 ]; then
dropdb the_db
fi
createdb -E UTF8 the_db
Run Code Online (Sandbox Code Playgroud)
运行它会导致错误:
$ make rebuilddb
exists=
if [ -eq 1 ]; then
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [rebuilddb_postgres] Error 2
Run Code Online (Sandbox Code Playgroud)
为什么这是错的?据我所知,它看起来像有效的Bash?在Makefile中执行此操作时是否需要特别注意?
更新:
使用答案我得到了一个工作版本:
.PHONY: rebuilddb
exists=$$(psql postgres --tuples-only --no-align --command "SELECT 1 FROM pg_database WHERE datname='the_db'"); \
if [ "$$exists" == "1" ]; then \
dropdb the_db; \
fi;
createdb -E UTF8 the_db
Run Code Online (Sandbox Code Playgroud)
Wil*_*ell 46
至少有两个考虑因素. $()引用Make变量.您必须转义$to do命令替换.此外,shell命令必须全部在一行上.尝试:
exists=$$(psql postgres --tuples-only --no-align --command "SELECT 1 FROM \
pg_database WHERE datname='the_db'"); \
if [ "$$exists" -eq 1 ]; then \
dropdb the_db; \
fi; \
createdb -E UTF8 the_db
Run Code Online (Sandbox Code Playgroud)
另一方面,似乎总是尝试删除数据库并允许失败更简单:
rebuilddb:
-dropdb the_db # Leading - instructs make to not abort on error
createdb -E UTF8 the_db
Run Code Online (Sandbox Code Playgroud)