シェル:カール式の評価

シェル:カール式の評価

以下のスクリプトはステータスコードを生成し、statusCode = 200の場合はデータを送信します。それ以外の場合はstatusCode! =200の場合、トークンを生成してeval "$ request_cmd"を呼び出してメッセージを送信します。 強力なテキスト しかし、実際にこれを行うと、eval "$ request_cmd"行で "command introuvable"エラーが発生します。

#!/bin/bash

#variables
randomNumber=$(shuf -i000000-999999 -n1)
eventTime=$(date --rfc-3339=ns | sed 's/ /T/')
idEpc="OneVariable"
fromRecordtime=`date --utc +%FT%T.%3NZ`
goodStatus="200"


printf "\n ---------------------------------> SENDING MESSAGE <-------------------------------------- \n\n"

request_cmd=$(curl -X POST -w "%{http_code}" --http1.0 \
"$1/api/acquire/rabbitmq/epcis" \
-H 'Authorization: Basic YWtytrytrytrytrW46trttrytrytr' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: text/xml' \
-H 'Postman-Token: 2c4f9rtertzertertrezatre4' \
-H "X-Authorization: Bearer ${generatedToken}" \
-d 'Here i have the data')
echo $request_cmd

if [ "$request_cmd" != "$goodStatus" ]
then
    printf "\n ---------------------------------> GETTING TOKEN <-------------------------------------- \n\n"
    generatedToken=$(curl -X POST $2/MyURL/token -H 'Cache-Control: no-cache' -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=client_credentials&client_secret=abesdfsd677c-6dsdfsba-4ddfsc8-978fsdfdsfsb-ec256cf65914&client_id=che-gateway' | jq -r .access_token)
eval "$request_cmd"
echo $request_cmd

ベストアンサー1

これにはいくつかの問題があります。

以下のスクリプトは、statusCode = 200の場合にステータスコードを生成します。

それはあなたのコードがすることではありません。また、ステータスコードを配置します。そして変数内のすべての文書を返します$request_cmd。ドキュメントが空の場合、今は問題は発生しないかもしれませんが、少し混乱しています。

バラよりhttps://superuser.com/questions/272265/getting-curl-to-output-http-status-code

statusCode !=200 の場合はデータを送信し、それ以外の場合はトークンを生成し、eval "$request_cmd" を呼び出してメッセージを送信します。

これはあなたのコードが行うことではありません。最初にカールを実行しましたが、「401 Unauthorized」などの結果が返されたとします。 evalは "401"コマンドを実行しようとしますが、それを呼び出すコマンドはありません。

使うという意味だと思います。バッシュ機能ここで。 Bash関数を使用して同じコードを複数回実行できます。

私が意味するものの例は次のとおりです。

function request_cmd {
status_code=$(curl -s -o /dev/null -X POST -w "%{http_code}" --http1.0 \
"$1/api/acquire/rabbitmq/epcis" \
-H 'Authorization: Basic YWtytrytrytrytrW46trttrytrytr' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: text/xml' \
-H 'Postman-Token: 2c4f9rtertzertertrezatre4' \
-H "X-Authorization: Bearer ${generatedToken}" \
-d 'Here i have the data')
echo $status_code
}
request_cmd # calls curl and puts result into $status_code
generatedToken=foo # changes the value of generatedToken
request_cmd # calls curl again with new generatedToken value

おすすめ記事