Я пытаюсь прочитать URL-адрес с помощью команды curl и ожидаю завершения команды с кодом возврата 0 или 1 в зависимости от ответа Curl после анализа ответа JSON.
Я пытался проанализировать ответ в формате json, но мне удалось заставить его работать в условии if else.
URL — localhost:8080/health< /p>
ответ URL
Код: Выделить всё
{
"db": {
"status": "healthy"
},
"scheduler": {
"fetch": "2024-03-12T04:32:53.060917+00:00",
"status": "healthy"
}
}
одна строка cmd для возврата с кодом завершения 0, если Scheduler.status исправен, иначе 1< /strong>
Примечание. Я не ищу ответ скручивания как 0 или 1, а ищу команду для выхода с 0 или 1.
Цель. Если мой статус планировщика неработоспособен, мой процесс завершится и выйдет из приложения.
Я могу проанализировать ответное сообщение, но не могу правильно применить условие к ответу, вот что до сих пор я пробовал,
cmd 1:
Код: Выделить всё
if ((status=$(curl -s 'https://localhost:8080/health' | python -c "import sys, json; print (json.load(sys.stdin) ['scheduler'] ['status'])"))='healthy'); then exit 0; else 1;
Код: Выделить всё
zsh: parse error near `='healthy''
Код: Выделить всё
curl -s 'https://localhost:8080/health' | python -c "import sys, json; print (json.load(sys.stdin) ['scheduler'] ['status'])"cmd 2:
Код: Выделить всё
/bin/sh -c "status=$(curl -kf https://localhost:8080/health --no- progress-meter | grep -Eo "scheduler [^}]" | grep -Eo '[^{]$' | grep -Eo "status [^}]" | grep -Eo "[^:]$" | tr -d \"' | tr -d '\r' | tr -d '\ '); if [ $status=='unhealthy']; then exit 1; fi;0"
Код: Выделить всё
curl -kf https://localhost:8080/health --no- progress-meter | grep -Eo "scheduler [^}]" | grep -Eo '[^{]$' | grep -Eo "status [^}]" | grep -Eo "[^:]$" | tr -d \"' | tr -d '\r' | tr -d '\ 'I tried all this short of, but no luck not sure if there is any workaround with a single cmd.
great if there is any option using shell without python else with python would also work
Please note - No Jq or any other tool installation is required, just with existing commands
Источник: https://stackoverflow.com/questions/781 ... e-handling
Мобильная версия