Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1va7Zm-003u7i-0i for pgsql-general@arkaria.postgresql.org; Mon, 29 Dec 2025 07:21:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1va7Zk-00FmRQ-2z for pgsql-general@arkaria.postgresql.org; Mon, 29 Dec 2025 07:21:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1va7Zk-00FmRI-1v for pgsql-general@lists.postgresql.org; Mon, 29 Dec 2025 07:21:57 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1va7Zi-003MTc-24 for pgsql-general@postgresql.org; Mon, 29 Dec 2025 07:21:56 +0000 Received: by mail-oi1-x236.google.com with SMTP id 5614622812f47-457c1148a5bso1738948b6e.1 for ; Sun, 28 Dec 2025 23:21:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766992912; x=1767597712; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=A1Kh8kr0Y+gl/Z8GTsanCKoaiIxRI7bSa0iKMBVyUkk=; b=TlkLk04uuG5qsw84UL9PR8Y7CuKckqJf5K79+1lfskl6zOGuwXb3u+l7IwslWZ3UeG LOnLOiY/ZIv7Yzc0NcJ6uLN/ia+wXluxvcEfXzAddE8BH609cLlPTmCb7Sz0NjjiWQfF vXR984TpzpxilbMyH7HyHPQqLPl/9rIg2I3FFbVI4rGJGtwVkDLffWqj01UpeCftihNd dI1jtT5HEVCm5teWo+Ag8o5g2Ss2AwWc4q3wvQtT3zgYjpru1Rho9QDUqxfN9Ck5BzMu XxRN+Jou4EE0rfKwf5/vOUhq/9FInCx+MlX+3ERVLz8HpCh4Dsce/zcf6OEFw5SHpngB kNgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766992912; x=1767597712; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=A1Kh8kr0Y+gl/Z8GTsanCKoaiIxRI7bSa0iKMBVyUkk=; b=HsZ2uWPDyLCS9KX/1bsX8yJw9WL4yU9a1bg6VFSpRqm1OOx7lJCbs/Vc2bvLDPHGpH aN926duX4Y1FBwQ1YRE87pqOAfsF85mbYtQ43lDWZ6CxSE6slHiG/UuLXZGxptEV0eC3 Q0n3Te6/6+hHhONxmFGVl4RKoPzZ/b0HX7w09Glfxco1ljNLY6v5ZY5G9EGo9/zbEpHZ I6P+uYP2XENaFu9eo7kdEJUAjE3dCfyRee8y5Kamd06iSuds3CnbUIhNGZhRJzi5NJMm vEHGhaEa6+X9+6AN76WYTT8M8iKKEqjGzb2pLc0t2gtvxw1glCVQ4gLk2GaDEcj/jHSe JuJA== X-Gm-Message-State: AOJu0YwcQXdz7BSP+3jCVmZWoPU+rDIfbPAVgVSkK+1qXsAiWzGhQZD+ mq2I6KcuZFnyqRFiyiu1WF7OMwN3phh+2614ksVtrDeFU2iPdDjGRAPkDRMnA+f/vRARFHwNbCx nCbb7T3JLNpaJQd42WsJhzuJ66khdfD0Ycw8e X-Gm-Gg: AY/fxX6EGutVUbe+NLCbS6HuYkc+nSCZr7Bp8FSjLunUS0Gr+FLZmbkRHrnVJT8Hdqy 6w+80BUvuFC2z4GB9Cr1pvn5RuyfpCZilBjCCpUeZTjdia6/ILQZzYIK9Ze3DTuIBGx9FHceJ8d 3JOV7m7nIl/THewP37/bvK9komhYcJOpRwRahQm2P5LkVvYLTU9p2U4JnNZpKcKd3YkVS2k+wA7 xeRBhyYKkAdisuezkpfUdOZeiRPaPgh3yoLrLU9DcNZF/6pyfQRsPAmdtJ+4M3/oeAb+iuthhE8 Mk/aZCk= X-Google-Smtp-Source: AGHT+IEA578un+jesDQT4fytacf3uf77YgiDxXpL9dEGqWZlO/ocnl4rkKbhqrME/yoed57vGq2zqtIUlFReLjS85pI= X-Received: by 2002:a05:6808:3198:b0:450:b947:1dab with SMTP id 5614622812f47-457b221dd99mr11599223b6e.33.1766992912436; Sun, 28 Dec 2025 23:21:52 -0800 (PST) MIME-Version: 1.0 From: Ron Johnson Date: Mon, 29 Dec 2025 02:21:41 -0500 X-Gm-Features: AQt7F2p_ybp0WH-QVS-clSI2h8_mbSefMOeHVbxyKsLcSJVGP5X8IRJqIo2OsoE Message-ID: Subject: psql: print values and return the COUNT(*) value to bash? To: pgsql-general Content-Type: multipart/alternative; boundary="000000000000ddd2870647121868" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ddd2870647121868 Content-Type: text/plain; charset="UTF-8" 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 -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 , and butter sauce. Don't boil me, I'm still alive. lobster! --000000000000ddd2870647121868 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Currently I do this in a bash script:
Tbl=3Dtable_1
Fld=3Dfield_1
Sql=3D"SELECT = CURRENT_TIMESTAMP - MIN($Fld) AS days_ago
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 , MIN($Fld) as oldest_date
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 , = COUNT(*) AS cnt=C2=A0
=C2=A0 =C2= =A0 =C2=A0FROM $Tbl;"
psql -h <host> <db> -Xc "${S= ql}"
=C2=A0days= _ago | oldest_date | cnt
--------= --+-------------+-----
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 0
(1 row)
=
More stuff happens after this.=C2=A0 I want to print out the= se four lines, but also test the COUNT(*) v= alue and bypass other bash commands if the count =3D=3D 0.

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

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