如何在shell脚本中使用curl URL中的变量? [英] How to use a variable in a curl URL in shell script?

查看:3700
本文介绍了如何在shell脚本中使用curl URL中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用包含两个日期的网址的curl。如何在URL中使用包含日期/时间戳而不是硬编码日期的两个变量的值?

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


推荐答案

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

这篇关于如何在shell脚本中使用curl URL中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆