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.94.2) (envelope-from ) id 1sopjl-00E8x2-WC for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2024 19:44:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sopjk-00Fpll-Od for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2024 19:44:16 +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.94.2) (envelope-from ) id 1sopjk-00Fpld-CD for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2024 19:44:16 +0000 Received: from mail-oo1-xc29.google.com ([2607:f8b0:4864:20::c29]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sopjg-000rna-QP for pgsql-general@postgresql.org; Thu, 12 Sep 2024 19:44:15 +0000 Received: by mail-oo1-xc29.google.com with SMTP id 006d021491bc7-5da6865312eso687815eaf.3 for ; Thu, 12 Sep 2024 12:44:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726170252; x=1726775052; darn=postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=6/EPuYHODCO4mX+xyJZsYC4rrTO5RhuPfSlY8XdjTt8=; b=YRrHJVhystlledzptRrJJ9v2yF/0WSRJzjlDZ/ynwKsxdBQJ4r+jK2u4+lgjLKIs63 M8Now0muJJLulslucRTtQu983oiNajDIsch4aMQ0xB3B8HfEA4b+gmhi04tQNU8NUgXG Mkws8mpfmmiVldtFwaFx0nKSNp1vjYgNQ08dwy65ZiB2aiktmPH3oix31YCDUK98j4qv D29LMebKTUp8XwLBUjRfTSH6v2zk/qOLgfLmmRteb1/kft/tzAmrPHk7umKj9B8RsfXW UlJ+t/g9DDkuYEXBcn5X9ItARSiHFWcxMlpjmNmdEwL6bVlcOhwj3QH0uELuYoX4cvf7 zCNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726170252; x=1726775052; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6/EPuYHODCO4mX+xyJZsYC4rrTO5RhuPfSlY8XdjTt8=; b=lX/yw0de5HKCsjDhhWTdBCUkPtebUtfpnctadZ6GJ4XiaXaDy8oGdzR/UGl9huYwFF JmHwUfk5F8yAmVi6sB25at5r4/FIvIp7P2tg3PQ9z/FziZZgyXHf6z7JgIZ0x1k2lVpd gKWgqkYh4RRW1O95i+nb0QIrVH0nkxrgvk4r+zyBs2JTHBvsz7tvAeGeuARI8Mpj+HCS njSPqJE3baFAV3EiHpfI7bQjUg+zOHsVplspSA0T62sgdN0oqjyKOIaBt1F3tN9MwSI8 8S38YCbpSURDheGtu1b1DVJ+CTt7yKmdFIprO/lt22+L7L7g36Ekyg7H5MReEfiL6A2a uEog== X-Gm-Message-State: AOJu0YyPDsFz6EFBAKW5K8KZAgv4Oe3GhSqempj9CWW3aHzAom+Y5xlZ stNPwiExK8erRzfREC0iZlCNTRy/NHP5gplp1EV48Tu06/WOmS6UrRBFIvVZ7PTqUwkzZsTSod+ li+SAQe6x9PIRin+qyV3hi9K4DQ/iww== X-Google-Smtp-Source: AGHT+IEFsq4b/0OhF+CzsZy1uiuHy74fY2ZF18oVl01QC3gfv/psN8A0fvZjYtX8Og4KWTAktukXViINkkiaEhMuF7g= X-Received: by 2002:a05:6820:162a:b0:5ba:ec8b:44b5 with SMTP id 006d021491bc7-5e201422254mr2067486eaf.3.1726170252306; Thu, 12 Sep 2024 12:44:12 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ron Johnson Date: Thu, 12 Sep 2024 15:43:59 -0400 Message-ID: Subject: Re: Better way to process records in bash? To: pgsql-general Content-Type: multipart/alternative; boundary="000000000000b5f6970621f154b5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000b5f6970621f154b5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Sep 12, 2024 at 3:30=E2=80=AFPM Christoph Moench-Tegeder wrote: > ## Ron Johnson (ronljohnsonjr@gmail.com): > > > I need to process table records in a bash script. Currently, I read th= em > > using a while loop and redirection. The table isn't that big (30ish > > thousand rows), and performance is adequate, but am always looking for > > "better". > > Use python, or any other sane language, and don't do bash in python. > Anything non-trivial is unreasonably hard to get really right when > using shell. > > Take this example (even when allowing some fuzz with the variables): > > Here's the current code: > > declare f1 f3 f8 > > while IFS=3D'|' read f1 f3 f8; do > > something f8 f3 f1 > > done < <(psql -XAt -c "select f1, f3, f8 from some.table_name;") > > What happens when f3 > a) is NULL? > b) contains a '|' character? > c) contains spaces? (this one depends on how exactly you're handling > the shell variables). > This might be fine if your data is only NOT NULL numbers, but that's > already quite a limitation on your data. > Good points. Shouldn't be relevant in my situation, but defensive programming never hurts. --=20 Death to America, and butter sauce. Iraq lobster! --000000000000b5f6970621f154b5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, Sep 12, 2024 at 3:30=E2=80=AFPM C= hristoph Moench-Tegeder <cmt@burgg= raben.net> wrote:
## Ron Johnson (ronljohnsonjr@gmail.com):

> I need to process table records in a bash script.=C2=A0 Currently, I r= ead them
> using a while loop and redirection.=C2=A0 The table isn't that big= (30ish
> thousand rows), and performance is adequate, but am always looking for=
> "better".

Use python, or any other sane language, and don't do bash in python. Anything non-trivial is unreasonably hard to get really right when
using shell.

Take this example (even when allowing some fuzz with the variables):
> Here's the current code:
> declare f1 f3 f8
> while IFS=3D'|' read f1 f3 f8; do
>=C2=A0 =C2=A0 =C2=A0something f8 f3 f1
> done < <(psql -XAt -c "select f1, f3, f8 from some.table_na= me;")

What happens when f3
=C2=A0a) is NULL?
=C2=A0b) contains a '|' character?
=C2=A0c) contains spaces? (this one depends on how exactly you're handl= ing
=C2=A0 =C2=A0 the shell variables).
This might be fine if your data is only NOT NULL numbers, but that's already quite a limitation on your data.

Good points.=C2=A0 Shouldn't be relevant in my=C2=A0situation, but de= fensive=C2=A0programming never hurts.

--
Death to America, and butter sauce.
Iraq lob= ster!
--000000000000b5f6970621f154b5--