如何在带有 bash 的 CURL 请求中使用变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37489695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:18:45  来源:igfitidea点击:

How to use a variable in a CURL request with bash?

bashshellapicurlcloudflare

提问by scre_www

Goal:

目标:

I'm using a bash CURL script to connect to the Cloudflare APIv4. The goal is to update an A-record. My script:

我正在使用 bash CURL 脚本连接到 Cloudflare APIv4。目标是更新A记录。我的脚本:

   # Get current public IP
      current_ip=curl --silent ipecho.net/plain; echo

   # Update A record
      curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONEIDHERE/dns_records/DNSRECORDHERE" \
        -H "X-Auth-Email: EMAILHERE" \
        -H "X-Auth-Key: AUTHKEYHERE" \
        -H "Content-Type: application/json" \
        --data '{"id":"ZONEIDHERE","type":"A","name":"example.com","content":"'"${current_ip}"'","zone_name":"example.com"}'

Problem:

问题:

The current_ip variable is not printed when I call it in my script. The output will be "content" : ""and not "content" : "1.2.3.4".

当我在脚本中调用 current_ip 变量时,它不会被打印出来。输出将是"content" : ""而不是"content" : "1.2.3.4"

I used otherstackoverflow posts and I'm trying to follow their examples but I think I'm still doing something wrong, just can't figure out what. :(

我使用了其他stackoverflow 帖子,我正在尝试遵循他们的示例,但我认为我仍然做错了什么,只是无法弄清楚是什么。:(

回答by nwk

Using jqfor this, as Charles Duffy's answer suggests, is a very good idea. However, if you can't or do not want to install jq here is what you can do with plain POSIX shell.

正如查尔斯·达菲 (Charles Duffy) 的回答所暗示的那样,为此使用jq是一个非常好的主意。但是,如果您不能或不想在此处安装 jq,那么您可以使用普通 POSIX shell 执行此操作。

#!/bin/sh
set -e

current_ip="$(curl --silent --show-error --fail ipecho.net/plain)"
echo "IP: $current_ip"

# Update A record
curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONEIDHERE/dns_records/DNSRECORDHERE" \
    -H "X-Auth-Email: EMAILHERE" \
    -H "X-Auth-Key: AUTHKEYHERE" \
    -H "Content-Type: application/json" \
    --data @- <<END;
{
    "id": "ZONEIDHERE",
    "type": "A",
    "name": "example.com",
    "content": "$current_ip",
    "zone_name": "example.com"
}
END

回答by Charles Duffy

The reliable way to edit JSON from shell scripts is to use jq:

从 shell 脚本编辑 JSON 的可靠方法是使用jq

# set shell variables with your contents
email="yourEmail"
authKey="yourAuthKey"
zoneid="yourZoneId"
dnsrecord="yourDnsRecord"

# make sure we show errors; --silent without --show-error can mask problems.
current_ip=$(curl --fail -sS ipecho.net/plain) || exit

# optional: template w/ JSON content that won't change
json_template='{"type": "A", "name": "example.com"}'

# build JSON with content that *can* change with jq
json_data=$(jq --arg zoneid "$zoneid" \
               --arg current_ip "$current_ip" \
               '.id=$zoneid | .content=$current_ip' \
               <<<"$json_template")

# ...and submit
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecord" \
  -H "X-Auth-Email: $email" \
  -H "X-Auth-Key: $authKey" \
  -H "Content-Type: application/json" \
  --data "$json_data"