繁体   English   中英

Shell 脚本,将变量卷曲作为 url 的一部分

[英]Shell script, curl with variable as part of url

我正在编写一个小的 bash 脚本,它从他们的用户名中获取 Minecraft 用户的个人资料。 但是,当我运行以下脚本时, profile_json变量显示为空白,而uuid_jsonuuid变量具有它们应有的内容。

#!/bin/bash

# settings
username="Notch"


# script
uuid_json=$(curl https://api.mojang.com/users/profiles/minecraft/$username)
echo $uuid_json
uuid=$(jq '.id' <<< "$uuid_json")
echo $uuid

profile_json=$(curl https://sessionserver.mojang.com/session/minecraft/profile/$uuid)
echo $profile_json

此外,在终端中运行脚本的输出显示profile_json=$(curl https://sessionserver.mojang.com/session/minecraft/profile/$uuid)Current Speed0 关于为什么会这样的任何想法?

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0    160      0 --:--:-- --:--:-- --:--:--   160
{"name":"Notch","id":"069a79f444e94726a5befca90e38aaf5"}
"069a79f444e94726a5befca90e38aaf5"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0


你的echo $uuid输出这个:

"069a79f444e94726a5befca90e38aaf5"

但它真的应该是:

069a79f444e94726a5befca90e38aaf5

使 curl 命令起作用。 因此,调整您的jq行以去除引号。

"069a79f444e94726a5befca90e38aaf5"您可以通过执行echo $uuid看到的,您得到了"069a79f444e94726a5befca90e38aaf5"而不是069a79f444e94726a5befca90e38aaf5应该没有引号。

jq可以选择删除它们-r一切都按预期工作(我认为)。

所以改成:

#!/bin/bash
# settings
username="Notch"

# script 
uuid_json=$(curl https://api.mojang.com/users/profiles/minecraft/$username)
echo $uuid_json
uuid=$(jq -r '.id' <<< "$uuid_json")                                                                                                                                                                           echo $uuid

profile_json=$(curl https://sessionserver.mojang.com/session/minecraft/profile/$uuid)
echo $profile_json 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM