How to use a variable in a curl URL in shell scrip

2019-03-04 16:16发布

I am trying to use curl with a URL containing two dates. How can I use the values of two variables containing date/time stamps instead of hard coded dates in the URL? I want to use a range from 50 minutes ago to the current time.

Here is my code with hard coded date/time values:

#!/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")

I tried replacing the hard coded dates as follows, but it didn't work:

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"")

标签: bash shell curl
1条回答
神经病院院长
2楼-- · 2019-03-04 17:02
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}")

I made a working example, here:

#!/bin/bash
example="Just_A_Test_String"
response=$(curl -X GET "http://5.135.224.191/test_curl.php?var1=${example}")
echo $response
查看更多
登录 后发表回答