Re: psql: print values and return the COUNT(*) value to bash?

Поиск
Список
Период
Сортировка
От Vincent Veyron
Тема Re: psql: print values and return the COUNT(*) value to bash?
Дата
Msg-id 20251229174023.106f1c1f2212c7372615673c@wanadoo.fr
обсуждение исходный текст
Ответ на Re: psql: print values and return the COUNT(*) value to bash?  (hubert depesz lubaczewski <depesz@depesz.com>)
Ответы Re: psql: print values and return the COUNT(*) value to bash?
Список pgsql-general
On Mon, 29 Dec 2025 11:11:58 +0100
hubert depesz lubaczewski <depesz@depesz.com> wrote:

Hi Ron, Hubert,

>
> Example:
>
> =$ cat z.sh
> #!/usr/bin/env bash
>
> table=pg_stat_all_tables
> field=last_autovacuum
>
> returned="$( psql -d depesz_explain -v "t=$table" -v "f=$field" -X << _SQL_
> select current_timestamp - min(:"f") as days_ago
>           , min(:"f") as oldest_date
>           , count(*) as cnt
>      from :"t"
> _SQL_
> )"
> row_count="$( tail -n1 <<< "${returned}" | tr -cd 0-9 )"
> echo "Rowcount = $row_count"
> echo "Full output:"
> echo "${returned}"
>
> # vim: set filetype=bash shiftwidth=4 expandtab smarttab softtabstop=4 tabstop=4 textwidth=132 :
>
> =$ bash z.sh
> Rowcount = 1
> Full output:
>         days_ago        |          oldest_date          | cnt
> ------------------------+-------------------------------+-----
>  7 days 03:48:07.348247 | 2025-12-22 07:22:32.593814+01 | 212
> (1 row)
>

IIUC, Ron wants the 212 number?

Maybe use the -t switch, and 'cut' :

#!/usr/bin/env bash

table=pg_stat_all_tables
field=last_autovacuum

returned="$( psql -t -d vv -v "t=$table" -v "f=$field" -X << _SQL_
select current_timestamp - min(:"f") as days_ago
          , min(:"f") as oldest_date
          , count(*) as cnt
     from :"t"
_SQL_
)"
row_count="$( tail -n1 <<< "${returned}" | cut -d "|" -f 3)"
echo "Rowcount = $row_count"
echo "Full output:"
echo "${returned}"



--
                Bien à vous, Vincent Veyron

https://compta.libremen.com
Logiciel libre de comptabilité générale et analytique en partie double



В списке pgsql-general по дате отправления: