You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash脚本if条件判断异常:获取user_exists后未执行后续逻辑求助

Bash脚本执行中断问题解决

问题描述

编写了一个用于检查/创建PostgreSQL的reportuser的Bash脚本,但脚本在获取user_exists变量的值后便停止执行,不再运行后续分支逻辑。脚本代码及执行输出如下:

脚本代码

#!/bin/bash -xe
source /reTech/REB3bastionscripts/setup/setenv.sh
echo "##### Begin check/create reportuser   #####################################################"
if [ $REPORT_DB_ENDPOINT != none ]; then
  user_exists=$(psql -U $REPORT_DB_USER -h $REPORT_DB_ENDPOINT -d $REPORT_DB -c "SELECT rolname FROM pg_roles WHERE rolname='reportuser'" | grep reportuser -c)
  if [ "$user_exists" == "1" ]; then
    echo Reportuser exists already in $REPORT_DB database. Resetting password and updating permissions...
  else
    echo creating user reportuser for $REPORT_DB
  fi
fi
if [ !$GRAFANA_DB_ENDPOINT != none ]; then
  user_exists=$(psql -U $GRAFANA_DB_USER -h $GRAFANA_DB_ENDPOINT -d $GRAFANA_DB -c "SELECT rolname FROM pg_roles WHERE rolname='reportuser'" | grep reportuser -c)
  if [ "$user_exists" == "1" ]; then
    echo Reportuser exists already in $GRAFANA_DB database. Resetting password and updating permissions...
  else
    echo creating user reportuser for $GRAFANA_DB
  fi
fi
echo "##### End check/create reportuser   #####################################################"

执行输出

##### Begin check/create reportuser   #####################################################
+ '[' psql-reb3az-devaz-reportdbserver.postgres.database.azure.com '!=' none ']'
++ grep reportuser -c
++ psql -U rebotics -h psql-reb3az-devaz-reportdbserver.postgres.database.azure.com -d report -c 'SELECT rolname FROM pg_roles WHERE rolname='\''reportuser'\'''
+ user_exists=0

问题原因及解决方法

1. bash -e选项触发脚本强制终止

脚本开头的#!/bin/bash -xe中,-e选项会让脚本在任何命令返回非0退出码时立即终止。如果psql查询无结果、grep未匹配到内容,或命令执行中出现权限/连接问题,都会触发脚本中断。

  • 解决方式:
    • 移除-e选项,改为#!/bin/bash -x(仅保留调试输出功能);
    • 或在可能返回非0的赋值命令后追加|| true,确保语句退出码始终为0:
      user_exists=$(psql -U $REPORT_DB_USER -h $REPORT_DB_ENDPOINT -d $REPORT_DB -c "SELECT rolname FROM pg_roles WHERE rolname='reportuser'" | grep reportuser -c || true)
      

2. 第二个if语句存在语法错误

原语句if [ !$GRAFANA_DB_ENDPOINT != none ]; then有两处问题:

  • !与变量之间缺少空格,会被解析为无效字符串,导致test命令报错;
  • !和!=叠加导致逻辑冗余混乱。
  • 修正为以下两种写法之一:
    # 直接判断变量不等于none
    if [ "$GRAFANA_DB_ENDPOINT" != "none" ]; then
    # 明确否定变量等于none的逻辑
    if [ ! "$GRAFANA_DB_ENDPOINT" = "none" ]; then
    

3. 变量未加双引号引发潜在语法问题

所有变量引用都应添加双引号,避免变量为空时出现语法错误(比如[ $REPORT_DB_ENDPOINT != none ]在变量为空时会变成[ != none ],触发语法报错)。修正后的条件判断应为:

if [ "$REPORT_DB_ENDPOINT" != "none" ]; then

4. 统一标准比较运算符

在[(test命令)中,标准字符串比较运算符是=,==属于bash非标准扩展。将内部if语句的比较改为:

if [ "$user_exists" = "1" ]; then

修正后的完整脚本

#!/bin/bash -x
source /reTech/REB3bastionscripts/setup/setenv.sh
echo "##### Begin check/create reportuser   #####################################################"
if [ "$REPORT_DB_ENDPOINT" != "none" ]; then
  user_exists=$(psql -U "$REPORT_DB_USER" -h "$REPORT_DB_ENDPOINT" -d "$REPORT_DB" -c "SELECT rolname FROM pg_roles WHERE rolname='reportuser'" | grep reportuser -c || true)
  if [ "$user_exists" = "1" ]; then
    echo "Reportuser exists already in $REPORT_DB database. Resetting password and updating permissions..."
  else
    echo "creating user reportuser for $REPORT_DB"
  fi
fi
if [ "$GRAFANA_DB_ENDPOINT" != "none" ]; then
  user_exists=$(psql -U "$GRAFANA_DB_USER" -h "$GRAFANA_DB_ENDPOINT" -d "$GRAFANA_DB" -c "SELECT rolname FROM pg_roles WHERE rolname='reportuser'" | grep reportuser -c || true)
  if [ "$user_exists" = "1" ]; then
    echo "Reportuser exists already in $GRAFANA_DB database. Resetting password and updating permissions..."
  else
    echo "creating user reportuser for $GRAFANA_DB"
  fi
fi
echo "##### End check/create reportuser   #####################################################"

内容的提问来源于stack exchange,提问作者Wolfgang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:07:06