簡體   English   中英

如何在Shell腳本的curl URL中使用變量?

[英]How to use a variable in a curl URL in shell script?

我正在嘗試將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM