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

时间:2017-01-26 14:17:12

标签: bash shell curl

我正在尝试使用包含两个日期的网址的curl。如何在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"")

1 个答案:

答案 0 :(得分:1)

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
相关问题