title: Bash: Check json response and write to a file if string exists
body: <p>I curl an endpoint for a json response and write the response to a file. 
  So far I've got a script that:</p>
  
  <p>1). does the curl if the file does not exist and
  2). else sets a variable</p>
  
  <pre><code>#!/bin/bash
  instance="server1"
  curl=$(curl -sk https://my-app-api.com | python -m json.tool)
  json_response_file="/tmp/file"
  
  if [ ! -f ${json_response_file} ] ; then
    ${curl} &gt; ${json_response_file}
    instance_info=$(cat ${json_response_file})
   else
    instance_info=$(cat ${json_response_file})
  fi
  </code></pre>
  
  <p>The problem is, the file may exist with a bad response or is empty. 
  Possibly using bash until, I'd like to </p>
  
  <p>(1). check (using JQ) that a field in the curl response contains $instance and only then write the file. </p>
  
  <p>(2). retry the curl XX number of times until the response contains $instance</p>
  
  <p>(3). write the file once the response contains $instance</p>
  
  <p>(4). set the variable instance_info=$(cat ${json_response_file}) when the above is done correctly.  </p>
  
  <p>I started like this... then got stuck...</p>
  
  <pre><code>until [[ $(/usr/bin/jq --raw-output '.server' &lt;&lt;&lt; ${curl}) = $instance ]]
  do
  </code></pre>