在 bash 脚本中使用变量进行卷曲

在 bash 脚本中使用变量进行卷曲

当我在下一个脚本中使用命令的实际值时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 文件中运行的命令,该命令仅包含某些值,没有垃圾值。

答案1

尝试这样的事情:

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

对固定字符串(即其中没有任何变量)使用单引号,对需要进行变量插值的字符串使用双引号。

答案2

您不能在变量上使用单引号。这将导致 bash 不解释$特殊字符。您可以使用双引号代替。 http://tldp.org/LDP/abs/html/quoting.html

编辑

我现在意识到您之前关闭了所有单引号,并在变量之后重新打开它们,但变量仍然可能被错误地读取,因为它们没有被引用。我不确定您是否需要单引号该命令中的所有内容,但如果您这样做,您仍然可以将双引号放入如下所示:

改变你的线路:

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

到:

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

但我认为这也可行:

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

相关内容