From owner-freebsd-questions@freebsd.org Sat Mar 6 04:33:56 2021 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 98D81574428 for ; Sat, 6 Mar 2021 04:33:56 +0000 (UTC) (envelope-from parv.0zero9@gmail.com) Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DssDz4T9Hz4kWm for ; Sat, 6 Mar 2021 04:33:55 +0000 (UTC) (envelope-from parv.0zero9@gmail.com) Received: by mail-wm1-x32c.google.com with SMTP id t5-20020a1c77050000b029010e62cea9deso473237wmi.0 for ; Fri, 05 Mar 2021 20:33:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yswET/lO5z1m6ygCsHh4CNeRySVDruedG/BEI02orPY=; b=MKjLBYFrMHVNe0QhLxETzwr5idKe7u7MtMIJ46Qbf526n6QRv8DsryNcsKgxHPA0AS DbIheTwUZmq9/wSsr8gdQpTUDmKfSfiSwdSTAfs6oiGACG+vOBms9wUE9GHpyqnzfiQO WD1aAekGV96r/UpSbpyeyZzsZEJ24Xq/YvjWDN4W+nWsGvP5m0lJBUQXeQKerhPtKSUF n8d1rqthNjyZzwGvMGHJTWTu0gPdQgQLHDbl2uMhDrBrNPw2TNNQqqXq2IlLXLbR1gV9 Dr9VHSEDenkUGybcP+OGkNwO73SToFy3MiUQHvi3JKGGAoqwZlWAdkVgrTC4uSCH5iyB e6Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yswET/lO5z1m6ygCsHh4CNeRySVDruedG/BEI02orPY=; b=s59dC0xjz3jTC4mxoUAcCWQB/nO6wkKaoL2VxRMYm27vAEZgG7CmxrUCzwTJsnZqm/ Eaq52k5Er6i1EbtsNIXjHqmweuXReUITvfK9wCRkyiu0yoes2iVlHAYCBbglRGgPqOtt lCRiXGSc8NLJ9Rae79zrPbVBk0Va+w1hzYLKqzL/fLinRBeDbLtoi0dINDdx6pgciBW9 RsOP/JI3zdxspL01EIbWLWC4x2WP699ImYWPSynO4y26IqsDjQE4bJ4sYMi7uuATLK8i 4BmgdoOLJ9q/oQg8I1RBjcZNbs9qqzTtte0AEL0iOC73y9ujTeX9mtsH1X44msfPgEp2 NQCA== X-Gm-Message-State: AOAM5336ughvA0q6GpFFSNngQkOOUpA39dJ8x2G5YDb54UsYSVXBNFWt QGpdaWnjgEffKjP43CBlW92L2AtKx2GsafxdLyQRrSVu X-Google-Smtp-Source: ABdhPJxiTAyqcjohMJZ+8H1zkwMFPwEY5TUk5AbvfON2/C4xOyiOmdRrTrEBaNoAIzedbilYsAINl/4voXrvj0m9EFE= X-Received: by 2002:a1c:cc08:: with SMTP id h8mr11955476wmb.188.1615005234191; Fri, 05 Mar 2021 20:33:54 -0800 (PST) MIME-Version: 1.0 References: <9178f6c5-631a-c2c2-c6b1-8def94a3397b@dreamchaser.org> <20210305142352297368817@bob.proulx.com> <606b8bfa-4fe5-883a-d7ac-99f41655f728@dreamchaser.org> In-Reply-To: <606b8bfa-4fe5-883a-d7ac-99f41655f728@dreamchaser.org> From: "parv/freebsd" Date: Fri, 5 Mar 2021 18:33:14 -1000 Message-ID: Subject: Re: sed -i empty argument compatibility issue To: Gary Aitken Cc: freebsd-questions@freebsd.org X-Rspamd-Queue-Id: 4DssDz4T9Hz4kWm X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=MKjLBYFr; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of parv0zero9@gmail.com designates 2a00:1450:4864:20::32c as permitted sender) smtp.mailfrom=parv0zero9@gmail.com X-Spamd-Result: default: False [-4.00 / 15.00]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; RBL_DBL_DONT_QUERY_IPS(0.00)[2a00:1450:4864:20::32c:from]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; TAGGED_FROM(0.00)[freebsd]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; SPAMHAUS_ZRD(0.00)[2a00:1450:4864:20::32c:from:127.0.2.255]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::32c:from]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; MAILMAN_DEST(0.00)[freebsd-questions] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.34 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Mar 2021 04:33:56 -0000 On Fri, Mar 5, 2021 at 5:56 PM Gary Aitken wrote: > On 3/5/21 2:43 PM, Bob Proulx wrote: > > Gary Aitken wrote: > >> I'm trying to come up with a fix for a script in a port which > >> invokes sed. The port comes from a linux environment, and the > >> offending line looks like this: ... > In order to port this then _something_ must be changed. As to what > > must be changed that is up to you and judegement and the larger view > > of everything in your environment. But off the top of my head these > > different possibilities come to mind. > > > > 1. Edit the script and change this to that. This makes it run on > > FreeBSD sed. Of course then this does not work the other direction. > > > > -COMMAND sed -i "/^# /d" "${outfile}" +COMMAND sed -i "" "/^# /d" > > "${outfile}" > > > > 2. Use perl. Change that edit command to be this. This should work > > the same everywhere that has perl. It does create a dependency upon > > perl that might not have been present before. > > > > perl -i -lpe 'next if /^# /' "${outfile}" > I like it! > > 3. Avoid -i and use a temporary file, just like in the old days. > > However this introduces the need for temporary file handling. But > > maybe that isn't important here. > > > > sed "/^# /d" "${outfile}" > "$tmpfile" && mv "$tmpfile" "${outfile}" > This won't do, when ... > 4. Introduce a "sed" wrapper in PATH that intercepts the call to the > > real sed, detects this problematic usage case, and then DTRT does > > the right thing with it. This wrapper could have all of the correct > > temporary file handling needed to make this work. > ... #!/bin/sh # Set sed path & -i option based on "uname -a" output. SED= host_type=$( uname -a ) case "${host_type}" in *Linux* ) SED="/usr/bin/sed -i''" ;; *FreeBSD* ) SED="/usr/bin/sed -i ''" ;; esac # Set $@ to contain original arguments prefixed by sed with -i option. set -- ${SED} $@ # Now run the sed with the arguments given. $@ > 6. Compile GNU sed locally and install it in PATH earlier. This > > would probably be a generally good option globally. However it's > > possible you might run into portability problems in the reverse > > case. > If GNU sed could be installed, this would be most agreeable. I prefer to use the absolute path to remove ambiguity about the kind of sed being used, especially in the above case. - parv Thanks for the list. That's two more than I came up with. I'm > inclined to #2 (which I hadn't thought of) or #3. > --