title: How to use bash return code in conditional?
body: <p>I have a small piece of code which checks IP address validity :</p>
  
  <pre><code>function valid_ip()
  {
      local  ip=$1
      local  stat=1
  
      if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
          OIFS=$IFS
          IFS='.'
          ip=($ip)
          IFS=$OIFS
          if [[ ${ip[0]} -le 255 &amp;&amp; ${ip[1]} -le 255 \
              &amp;&amp; ${ip[2]} -le 255 &amp;&amp; ${ip[3]} -le 255 ]]; then
              stat=1
          else
              stat=0
          fi
      fi
      return $stat
  }
  </code></pre>
  
  <p>But I am having problems with its usage in bash conditionals. I have tried many techniques to test its return value but most of them fail on me. </p>
  
  <pre><code>if [[ !$(valid_ip $IP) ]]; then
  
  if [[ $(valid_ip IP) -eq 1 ]]; then
  </code></pre>
  
  <p>etc. etc. Can anyone suggest what should I do here ? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Following your suggestions I have used something like : </p>
  
  <pre><code>  if valid_ip "$IP" ; then
        ... do stuff
    else
        perr "IP: \"$IP\" is not a valid IP address"
    fi
  </code></pre>
  
  <p>and I get errors like </p>
  
  <blockquote>
    <p>IP: "10.9.205.228" is not a valid IP address</p>
  </blockquote>