Skip to content
Advertisement

How can I save in a variable a portion of my cURL GET command?

I’m currently trying to setup an automated server on DigitalOcean API, and I need to extract the ID of my server to use it later in a command.

My bash command is the following :

DROPLETID="$(curl -X GET 
  -H "Content-Type: application/json" 
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" 
  "https://api.digitalocean.com/v2/droplets?tag_name=$TAG" | grep "id": | sed 's/"id": //g' | tr -d 'r')"

The answer for the cURL command is this :

{"droplets":[{"id":012345678,"name":"ppg-active","memory":1024,"vcpus":1,"disk":25,"locked":false,"status":"active","kernel":null,"created_at":"2022-01-30T10:28:10Z","features":["droplet_agent","private_networking"],"backup_ids":[],"next_backup_window":null,"snapshot_ids":[],"image":{"id":01234567,"name":"18.04 (LTS) x64","distribution":"Ubuntu","slug":"ubuntu-18-04-x64","public":true,"regions":["nyc3","nyc1","sfo1","nyc2","ams2","sgp1","lon1","ams3","fra1","tor1","sfo2","blr1","sfo3"],"created_at":"2022-01-11T21:07:39Z","min_disk_size":15,"type":"base","size_gigabytes":0.41,"description":"Ubuntu 18.04 x86 image","tags":[],"status":"available"},"volume_ids":[],"size":{"slug":"s-1vcpu-1gb","memory":1024,"vcpus":1,"disk":25,"transfer":1.0,"price_monthly":5.0,"price_hourly":0.00744,"regions":["ams3","blr1","fra1","lon1","nyc1","nyc3","sfo3","sgp1","tor1"],"available":true,"description":"Basic"},"size_slug":"s-1vcpu-1gb","networks":{"v4":[{"ip_address":"XXX.XXX.XXX.XX","netmask":"255.255.240.0","gateway":"XXX.XXX.XXX.X","type":"public"},{"ip_address":"XX.XXX.XX.X","netmask":"255.255.240.0","gateway":"XX.XXX.X.X","type":"private"}],"v6":[]},"region":{"name":"Frankfurt 1","slug":"fra1","features":["backups","ipv6","metadata","install_agent","storage","image_transfer"],"available":true,"sizes":["s-1vcpu-1gb","s-1vcpu-1gb-amd","s-1vcpu-1gb-intel","s-1vcpu-2gb","s-1vcpu-2gb-amd","s-1vcpu-2gb-intel","s-2vcpu-2gb","s-2vcpu-2gb-amd","s-2vcpu-2gb-intel","s-2vcpu-4gb","s-2vcpu-4gb-amd","s-2vcpu-4gb-intel","s-4vcpu-8gb","c-2","c2-2vcpu-4gb","s-4vcpu-8gb-amd","s-4vcpu-8gb-intel","g-2vcpu-8gb","gd-2vcpu-8gb","s-8vcpu-16gb","m-2vcpu-16gb","c-4","c2-4vcpu-8gb","s-8vcpu-16gb-amd","s-8vcpu-16gb-intel","m3-2vcpu-16gb","g-4vcpu-16gb","so-2vcpu-16gb","m6-2vcpu-16gb","gd-4vcpu-16gb","so1_5-2vcpu-16gb","m-4vcpu-32gb","c-8","c2-8vcpu-16gb","m3-4vcpu-32gb","g-8vcpu-32gb","so-4vcpu-32gb","m6-4vcpu-32gb","gd-8vcpu-32gb","so1_5-4vcpu-32gb","m-8vcpu-64gb","c-16","c2-16vcpu-32gb","m3-8vcpu-64gb","g-16vcpu-64gb","so-8vcpu-64gb","m6-8vcpu-64gb","gd-16vcpu-64gb","so1_5-8vcpu-64gb","m-16vcpu-128gb","c-32","c2-32vcpu-64gb","m3-16vcpu-128gb","m-24vcpu-192gb","g-32vcpu-128gb","so-16vcpu-128gb","m6-16vcpu-128gb","gd-32vcpu-128gb","m3-24vcpu-192gb","g-40vcpu-160gb","so1_5-16vcpu-128gb","m-32vcpu-256gb","gd-40vcpu-160gb","so-24vcpu-192gb","m6-24vcpu-192gb","m3-32vcpu-256gb","so1_5-24vcpu-192gb","so-32vcpu-256gb","m6-32vcpu-256gb","so1_5-32vcpu-256gb"]},"tags":["mc"],"vpc_uuid":"ef01391b-98c0-443f-94b2-0c4b94142c5a"}],"links":{},"meta":{"total":1}}

I only need to save as a variable the first ID the cURL command gives me, the ideal would be that when I enter :

echo $DROPLETID

I get :

01234678

As you can see in my first command, I tried to grep the ID, and sed it but it doesn’t work. Does anyone have any idea ?

Thanks a lot

Advertisement

Answer

You can use jq

DROPLETID="$(curl -X GET 
  -H "Content-Type: application/json" 
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" 
  "https://api.digitalocean.com/v2/droplets?tag_name=$TAG" | jq '.droplets[0].id')"

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement