title: How can I print text to stdout from a psql query?
body: <p>I have the following (massaged) psql script invoked from a bash shell:</p>
  
  <pre><code>foo=$( psql -q -t -R $'\x01' -F $'\x02' \
                  --variable="title=something" \
                  --variable="severity=level9" \
                  --pset='format=unaligned' \
                  &lt;&lt;'EOF'
  SET standard_conforming_strings=on;
  SET myvars.title = :title;
  SET myvars.severity = :severity;
  DO $$
  BEGIN
          IF EXISTS ( SELECT 1 from my_database
                  WHERE title=current_setting('myvars.title') \
                  AND severity=current_setting('myvars.severity') )
          THEN
                  RAISE NOTICE 'Found existing entry';
          ELSE
                  RAISE NOTICE 'Did not find existing entry';
          END IF;
  END;
  $$;
  EOF
   )
  </code></pre>
  
  <p>I was hoping to capture in the bash variable "foo" some indication of whether or not the query succeeded. I thought I could add some kind of print/echo/return/whatever statement after each of the RAISE NOTICE statements to output 0 or 1 from the <code>psql</code> statement so it could be captured in <code>foo</code> for later evaluation in the bash script, e.g.:</p>
  
  <pre><code>foo=$( psql -q -t -R $'\x01' -F $'\x02' \
                  --variable="title=something" \
                  --variable="severity=level9" \
                  --pset='format=unaligned' \
                  &lt;&lt;'EOF'
  SET standard_conforming_strings=on;
  SET myvars.title = :title;
  SET myvars.severity = :severity;
  DO $$
  BEGIN
          IF EXISTS ( SELECT 1 from my_database
                  WHERE title=current_setting('myvars.title') \
                  AND severity=current_setting('myvars.severity') )
          THEN
                  RAISE NOTICE 'Found existing entry';
                  magical_incantation 1;
          ELSE
                  RAISE NOTICE 'Did not find existing entry';
                  magical_incantation 0;
          END IF;
  END;
  $$;
  EOF
   )
  </code></pre>
  
  <p>I've been reading <code>psql</code> documentation for about the past 5 hours and cannot figure out the command/syntax of my imaginary <code>magical_incantation</code> above.</p>
  
  <p>I am new to psql and tweaking someone else's code. I cannot change the main structure of the script, I just need some way to print a value.</p>
  
  <p>Can anyone point me in the right direction?</p>