英文:
shell script check if command eval result is a specific value
问题
以下是翻译好的代码部分:
我有一个小片段,是shell脚本的一部分,用于调用API并获取响应。我希望通过检查返回的字符串是否为空的方括号字符串 [] 来检查是否存在空的API响应 - 如果是,就中断并终止执行。
目前,代码如下:
cmd = '在这里格式化的某个curl api请求'
eval $cmd
而这是我目前尝试的:
cmd = '在这里格式化的某个curl api请求'
if
eval $cmd == '[]' then echo '来自API的空响应' | break
else continue
fi
有关实现此目标的建议吗?
英文:
I have a small snippet here that is part of a shell-script used to call an API and retreive a response. I wish to check for an empty API response by seeing if the returned string is an empty square-brackets string [] - and if it is, do a break and abort execution.
Currently, the code looks like this:
cmd = 'some curl api request being formatted here'
eval $cmd
and this is what I'm currently trying
cmd = 'some curl api request being formatted here'
if
eval $cmd == '[]' then echo 'Empty response from API' | break
else continue
fi
Any suggestions for achieving this?
答案1
得分: 1
# 调用curl并保存响应
_response="$(_response)"
# 检查响应
if [ "$_response" = '[]' ]; then
# 空响应
printf 'API返回空响应\n'
break
fi
# 继续...
英文:
# Call curl and save response
_response="$(curl ...)"
# Check response
if [ "$_response" = '[]' ]; then
# Empty response
printf 'Empty response from API\n'
break
fi
# Continue ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论