如何在 shell 脚本中的 curl URL 中使用变量



我正在尝试将curl与包含两个日期的URL一起使用。如何在 URL 中使用包含日期/时间戳的两个变量的值而不是硬编码日期?我想使用从 50 分钟前到当前时间的范围。

这是我的代码,其中包含硬编码的日期/时间值:

#!/bin/bash
currentTime=$(date +"%R")
currentdate=$(date +'%m/%d/%Y')
oldtime=$(date +%R -d "50 min ago")
echo "Current time : $currentTime"
echo "Current Date : $currentdate"
echo "Old time : $oldtime"
Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.companyname.com:8080/v1/organizations/companyname/environments/environmentname/stats/apis?select=sum(is_error)&timeRange=01/24/2017%2002:00~01/25/2017%2006:00")

我尝试按如下方式替换硬编码日期,但它不起作用:

Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.company.com:8080/v1/organizations/company/environments/environmentname/stats/apis?select=sum(is_error)&timeRange="$currentdate" "$currentTime"~"$currentdate" "$oldtime"")
Response=$(curl -X GET -H "Authorization: Basic Token=" "http://targetserver.company.com:8080/v1/organizations/company/environments/environmentname/stats/apis?select=sum(is_error)&timeRange=${currentdate}%20${currentTime}~${currentdate}%20${oldtime}")

我在这里做了一个工作示例:

#!/bin/bash
example="Just_A_Test_String"
response=$(curl -X GET "http://5.135.224.191/test_curl.php?var1=${example}")
echo $response

最新更新