【发布时间】:2012-11-26 07:43:26
【问题描述】:
我在 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
【问题讨论】: