public inbox for [email protected]  
help / color / mirror / Atom feed
From: Ron Johnson <[email protected]>
To: pgsql-general <[email protected]>
Subject: psql: print values and return the COUNT(*) value to bash?
Date: Mon, 29 Dec 2025 02:21:41 -0500
Message-ID: <CANzqJaBX-SZZcHn71xgqtSKx6ekZGdHPSgCmP2co5=-KDu4Q-Q@mail.gmail.com> (raw)

Currently I do this in a bash script:
Tbl=table_1
Fld=field_1
Sql="SELECT CURRENT_TIMESTAMP - MIN($Fld) AS days_ago
          , MIN($Fld) as oldest_date
          , COUNT(*) AS cnt
     FROM $Tbl;"
psql -h <host> <db> -Xc "${Sql}"
 days_ago | oldest_date | cnt
----------+-------------+-----
          |             |   0
(1 row)

More stuff happens after this.  I want to print out these four lines, but
also test the COUNT(*) value and bypass other bash commands if the count ==
0.

Is there any relatively simple way to do this (psql print and return
count(*))?

-- 
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!


view thread (3+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: psql: print values and return the COUNT(*) value to bash?
  In-Reply-To: <CANzqJaBX-SZZcHn71xgqtSKx6ekZGdHPSgCmP2co5=-KDu4Q-Q@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox