我在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
运行它会导致错误:
$ make rebuilddb
exists=
if [ -eq 1 ]; then
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [rebuilddb_postgres] Error 2
为什么会出错?就我所知,它看起来像是有效的 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
exists = ...
和if ... fi
必须用分号和反斜杠制成单个命令行,但是createdb ...
可以愉快地保留为配方中的独立第二个命令。 - John MarshallVAR="foo" \
语句的末尾加上\
。 - redolent.ONESHELL
,可以放宽“全在一行”的限制。 - sdive