shell脚本检查eval命令结果是否是特定值

huangapple go评论46阅读模式
英文:

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 ...

huangapple
  • 本文由 发表于 2023年2月6日 20:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定