简体   繁体   中英

Shell script, curl with variable as part of url

I am writing a small bash script that gets the profile of a Minecraft user from their username. However, when I run the following script, the profile_json variable comes out blank while the uuid_json and uuid variables have the content they should.

#!/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

Moreover, the output from running the script in a terminal shows the Current Speed of profile_json=$(curl https://sessionserver.mojang.com/session/minecraft/profile/$uuid) as 0 . Any ideas as to why this would be?

  % 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


Your echo $uuid outputs this:

"069a79f444e94726a5befca90e38aaf5"

But it really should be:

069a79f444e94726a5befca90e38aaf5

for the curl command to work. So, tweak your jq line to strip off quotation marks.

AS you can see by executing echo $uuid you got "069a79f444e94726a5befca90e38aaf5" instead of 069a79f444e94726a5befca90e38aaf5 there should be no quotes.

jq has option to remove them it's -r with that everything works as expected(I think).

So change it to:

#!/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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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