Bash

curl 在 bash 腳本上使用變數

  • April 12, 2018

當我curl在下一個腳本中使用命令的實際值時,它正在工作並給我一個結果,但是當我使用變數時它不能正常工作

我認為這是我如何在命令中定義它們的問題

amz_t=$(cat amazon-token.txt )
flx_id=$(cat flex-id.txt )
ses_t=$(cat session-token.txt )

curl -s -H 'Host: flex-capacity-na.amazon.com' \
 -H 'Cookie: session-token='$ses_t'' \
 -H 'x-amz-access-token: '$amz_t'' \
 -H 'x-flex-instance-id: '$flx_id'' \
 -H 'Accept: */*' \
 -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' \
 -H 'Accept-Language: en-us' \
 --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt

這是我嘗試在上面提到的腳本中執行的命令 txt 文件只包含某些值,沒有垃圾值。

嘗試這樣的事情:

amz_t=$(cat amazon-token.txt)
flx_id=$(cat flex-id.txt)
ses_t=$(cat session-token.txt)

UA='iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141'

URL='https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122'

curl -s -H 'Host: flex-capacity-na.amazon.com' \
 -H "Cookie: session-token=$ses_t" \
 -H "x-amz-access-token: $amz_t" \
 -H "x-flex-instance-id: $flx_id" \
 -H 'Accept: */*' \
 -H "User-Agent: $UA" \
 -H 'Accept-Language: en-us' \
 --compressed "$URL" >> output.txt

對固定字元串使用單引號(即其中沒有任何變數),對需要進行變數插值的字元串使用雙引號。

引用自:https://unix.stackexchange.com/questions/386127