2

Edit: This question has been edited for context. 1.sh and 2.sh scripts in the original question are replaced by their real-world implementations: monitor.sh and api-check.sh respectively.

Function:

function checkAlive() {
    script="$1"
    args="${@:2}"
    startCheck=$(date +%s)
    echo "$@"
    tempResult="$( "$@" )"
    T_RC="$?"
    if [ "$T_RC" -ne 0 ]; then
        if [ "$result" = "" ]; then
            result="ERRORS: $tempResult"
        else
            result="$result ,, $tempResult"
        fi
    fi
    RC="$((RC > T_RC ? RC : T_RC))"
    timePassed
}

api-check.sh:

method="$1"
url="https://$2"
jwt="$3"
body="$4"
echo "$body"

if ! response="$(curl -s --request "$method" "$url" -w "%{http_code}" --header \
    'Authorization: Bearer '"$jwt" --header 'Content-Type: application/json' --header 'Accept: application/json' --data-raw "$body")"; then
    echo "$url curl error getting code: $response"
    exit 2
fi

monitor.sh:

api_url="foo"
oauth_token"bar"
body='{"address":"Test address"}'
checkAlive "api-check.sh" '"POST" "'$api_url'" "'$oauth_token'" '"'$body'"''

Execution Flow:

  • monitor.sh passes the string json body as an argument to checkAlive function (defined above).
  • Within checkAlive, the echo "$@" returns:
api-check.sh "POST" "foo" "bar" '{"address":"Test address"}'
  • The api-check.sh script never executes properly due to some escaping issue again

Question: What json body should I pass as an argument to checkAlive to escape correctly OR Which line of code and in which script do I need to modify to evaluate the json body correctly?

8
  • 4
    The culprit is eval $script $args. First, you don't double-quote your variable expansions ('{"address":"Test address"}' becomes '{"address":"Test' 'address"}'), then even if you double-quote them, eval will jinx it... the correct way is to simply use result="$( "$@" )" Commented Jan 26, 2023 at 10:37
  • 4
    If you really need to store all but the first argument, use an array, like args=("${@:2}"), then use it like result=$("$script" "${args[@]}"). Avoid eval, it's extremely hard to use correctly, and almost never necessary. Commented Jan 26, 2023 at 10:47
  • Why are you using "${@:2}" instead of simply "$2"? Commented Jan 26, 2023 at 13:58
  • Further, what is the point of checkAlive instead of just calling 2.sh on the given JSON? Commented Jan 26, 2023 at 13:59
  • '"'$body'"''?? Jsut "$body"` Commented Jan 26, 2023 at 16:03

1 Answer 1

2
  • do func() not function func()
  • just "$var". not anything else. Just make sure any $var is inside " ". Nothing more required.

Do:

api_url="foo"
oauth_token"bar"
body='{"address":"Test address"}'
checkAlive "api-check.sh" "POST" "$api_url" "$oauth_token" "$body"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.