加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 如何在Makefile操作中使用shell变量?

发布时间:2020-12-15 18:54:02 所属栏目:安全 来源:网络整理
导读:我在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 d
我在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
至少有两个考虑因素. $()引用一个Make变量.您必须逃避$才能做过程替换.此外,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

另一方面,似乎总是尝试删除数据库并允许失败会更简单:

rebuilddb:
    -dropdb the_db  # Leading - instructs make to not abort on error
    createdb -E UTF8 the_db

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读