无法验证字符串是否为空或不在shell脚本中

iklwldmw  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(126)

我在我的shell脚本中进行curl调用,这是成功的,然后我尝试使用以下代码验证从API调用接收到的响应中的$JIRA_DESCRIPTION是否为空:

- export JIRA_DESCRIPTION=$(echo ${JIRA_DETAIL} | jq '.fields.description')
- |
  if [[ "$JIRA_DESCRIPTION" = null ]]
    then 
      echo "FAILED! Jira description cannot be empty"; exit 1
    else 
      echo "PASSED! Jira description check"; echo $?
  fi

这个检查每次都失败,即使字符串是空的,我也会得到传递条件。如果我尝试使用echo打印jira_description变量的值,那么我只会得到""之间没有空格的双引号。我的代码有什么问题?如何解决这一问题?
完整的源代码在这里:

validate-jira:
  image: python:3.9
  variables:
    JIRA_HEADER: 'Accept: application/json'
    UNACCEPTED_JIRA_STATUSES: "Done, Completed, Closed"
  script:
    # extract the JIRA id out of the Merge Request title.
    # The following logic assumes a Merge Request title must start with a valid JIRA issue id and that all JIRA projects start with a letter
    - export MERGE_REQUEST_JIRA_ID=$(echo ${CI_MERGE_REQUEST_TITLE} | sed 's/\([0-9]\):.*$/\1/')

    # Validate if we had successfully extracted a Jira Issue Id
    - |
      if test -z "$(echo "$MERGE_REQUEST_JIRA_ID" | sed "/^${JIRA_PROJECT_CODE}-/d")"
       then 
         echo "Jira Id found in the Merge Request title!"; echo $?
       else   
         echo "Either Jira Id not found in the Merge Request title or it is invalid (should start with ${JIRA_PROJECT_CODE}-)!"; exit 1
      fi

    # Make API Call to get the JIRA Details
    - export JIRA_DETAIL=$(curl -u ${JIRA_USERNAME}:${JIRA_PASSWORD} -H "${JIRA_HEADER}" -X GET "https://${JIRA_SERVER}/rest/api/2/issue/${MERGE_REQUEST_JIRA_ID}?fields=description&fields=status&fields=customfield_11000&fields=customfield_15700")

    # extract the JIRA key id, this also validates JIRA issue referenced is valid
    - export JIRA_KEY_ID=$(echo ${JIRA_DETAIL} | jq -e '.key')

    # extract the JIRA status
    - export JIRA_STATUS=$(echo ${JIRA_DETAIL} | jq '.fields.status.name')    

    # To bypass Jira validation check in case if Jira server is down. simply exit the job with 'success' status
    - |
      if [[ "${JIRA_CASE_ID_VALIDATION}" = 0 ]] 
        then             
          echo "Bypassing Jira validation check. Exiting with success status..."; exit 0  
      fi

    # Check the Jira status from server response
    - |
      if [[ "$JIRA_STATUS" = null ]] 
        then             
          echo "Unable to retrieve the Jira details from server, exiting the job!"; exit 1  
      fi

    # optionally test status of the JIRA issue matches a desired "status"
    - |
      JIRA_STATUS_TO_COPY_FROM=$(echo $UNACCEPTED_JIRA_STATUSES | sed -e 's/\s*,\s*/|/g')
      echo $JIRA_STATUS_TO_COPY_FROM
      if test -z "$(echo ${JIRA_STATUS} | sed -r "s/\"($JIRA_STATUS_TO_COPY_FROM)\"//")"
        then 
          echo "FAILED! Not a valid Jira (Done/Completed/Closed)"; exit 1
        else 
          echo "PASSED! Jira status check"; echo $?
      fi

    # extract the Jira Description
    - export JIRA_DESCRIPTION=$(echo ${JIRA_DETAIL} | jq '.fields.description')
    # Validate if Jira description is null or not
    - |
      echo $JIRA_DESCRIPTION
      echo 'doneeee'
      if [ -z  "$JIRA_DESCRIPTION" ]
        then 
          echo "FAILED! Jira description cannot be empty"; exit 1
        else 
          echo "PASSED! Jira description check"; echo $?
      fi

GitLab CI Job的输出:-

$ export JIRA_DESCRIPTION=$(echo ${JIRA_DETAIL} | jq '.fields.description')
$ echo $JIRA_DESCRIPTION # collapsed multi-line command
""
doneeee
PASSED! Jira description check
zf2sa74q

zf2sa74q1#

在shell脚本中没有'null'这样的概念。
您正在将返回的字符串与文字4字符串null进行比较,该字符串(可能)始终为false。您希望与实际的空字符串进行比较

if [[ "$JIRA_DESCRIPTION" == "" ]]

或者对零长度字符串使用-z测试

if [[ -z "$JIRA_DESCRIPTION" ]]

相关问题