Это именно то, что я хотел, я уверен, что @they могли бы сделать это намного элегантнее, но это делает то, что мне нужно.
#!/bin/bash
# Set groupid also read and write API variables
group="80000"
read_api="read_api"
write_api="write_api"
# get all devicesid and groupid and create groups.json file
curl https://api_url.com/device_groups -u "$read_api": > groups.json
# Move all devices into predefined groupid
jq -r --arg groupid $group '
.data[].relationships.devices.data[].id |
@sh "https://api_url.com/device_groups/\($groupid)/devices/\(.)"' groups.json |
xargs curl -X POST -u "$write_api":
# Wait for the groupid to propagate
sleep 60;
# Move all devices into original groupid
jq -r '
.data[] |
"https://api_url.com/device_groups/\(.id)/devices/\(.relationships.devices.data[].id)"' groups.json |
xargs curl -X POST -u "$write_api":
Команда jq
jq -r '
.data.id as $groupid |
.data.relationships.devices.data[].id |
@sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file
... будет извлекать идентификатор группы, а затем все значения id
из массива data
. Для каждого значения id
будет создан URL-адрес, который будет выводиться.
Чтобы использовать эти сгенерированные URL-адреса в вызовахcurl
:
jq -r '
.data.id as $groupid |
.data.relationships.devices.data[].id |
@sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file |
xargs -I {} curl -X POST -u api_key: {}
Учитывая документ JSON в вопросе, будут выполнены следующие команды:
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506044
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506034
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506037
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506038
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506046
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506043
curl -X POST -u api_key: https://api_url.com/group/85684/devices/658669
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506036
curl -X POST -u api_key: https://api_url.com/group/85684/devices/502256
Вы также можете использовать xargs
, чтобы вызывать curl
как можно меньше раз с этими URL-адресами:
jq -r '
.data.id as $groupid |
.data.relationships.devices.data[].id |
@sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file |
xargs curl -X POST -u api_key:
Это сгенерирует одну команду
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506044 https://api_url.com/group/85684/devices/506034 https://api_url.com/group/85684/devices/506037 https://api_url.com/group/85684/devices/506038 https://api_url.com/group/85684/devices/506046 https://api_url.com/group/85684/devices/506043 https://api_url.com/group/85684/devices/658669 https://api_url.com/group/85684/devices/506036 https://api_url.com/group/85684/devices/502256
Вы можете указать идентификатор группы из командной строки вместо того, чтобы использовать идентификатор, найденный в данных, например (с помощью переменнойsome_groupid
):
jq -r --arg groupid "$some_groupid" '
.data.relationships.devices.data[].id |
@sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file