Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash: command substitution to return string, skips if statement

I’m writing a bash script which will use curl to make a POST.
The make_request function should run curl, take it’s response and compare the exit code.

The PROBLEM is: the if statements doesn’t run and the script goes to parse_response

#! /bin/bash
AUTH_BEARER=yaddayadda

unknown() {
    echo "UNKNOWN: $1"
    exit 3
}

make_request() {
  local response
  local exit_status

  response=$(curl --max-time 1 --location --request POST 'https://api.cloudflare.com/client/v4/graphql/' \
  --header 'Authorization: Bearer "'$AUTH_BEARER'"' \
  --header 'Content-Type: application/json' \
  --data-raw "$data")

  exit_status=$?

  # Check if curl command was successful
  if [ $exit_status -ne 0 ]; then
    unknown "Curl exited with: $exit_status"
  else
    echo "$response"
  fi
}

data=yaddayadda

# take response and
response=$(make_request)
# parse it
parse_response "$response"

EDIT: adding set -x output (aaaaaaapi is to make it fail)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

curl: (6) Could not resolve host: aaaaaaaapi.cloudflare.com
++ response=
++ exit_status=6
++ '[' 6 -ne 0 ']'
++ unknown 'Curl exited with: 6'
++ echo 'UNKNOWN: Curl exited with: 6'
++ exit 3
+ response='UNKNOWN: Curl exited with: 6'
+ parse_response 'UNKNOWN: Curl exited with: 6'
+ local response
+ response='UNKNOWN: Curl exited with: 6'
+ local errors
++ echo 'UNKNOWN: Curl exited with: 6'
++ jq .errors
parse error: Invalid numeric literal at line 1, column 8
+ errors=
+ '[' '' '!=' null ']'
+ local code
+ local message
++ echo 'UNKNOWN: Curl exited with: 6'
++ jq '.errors[].code'
parse error: Invalid numeric literal at line 1, column 8
+ code=
++ echo 'UNKNOWN: Curl exited with: 6'
++ jq '.errors[].message'
parse error: Invalid numeric literal at line 1, column 8
+ message=
+ unknown 'API error: message= - code='
+ echo 'UNKNOWN: API error: message= - code='
UNKNOWN: API error: message= - code=
+ exit 3

EDIT2: I believe this is the problem: response='UNKNOWN: Curl exited with: 6'.
Basically, when I use echo inside unknown function, that echoed goes straight into response which is in the main script.

Unfortunately I cannot use nameref because of bash version

>Solution :

You’re exiting from a subshell, because the exit 3 gets called from the command substitution $(make_request). To really exit, you have to call the exit from the shell that executes the script.

Also, output the error message to standard error instead of standard output so the command substitution doesn’t consume it.

unknown() {
    echo "UNKNOWN: $1" >&2
    exit 3
}
...
response=$(make_request) || exit $?
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading