Bug 230507 - sed 'q' command preventing -i from creating backup
Summary: sed 'q' command preventing -i from creating backup
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 11.2-STABLE
Hardware: Any Any
: --- Affects Some People
Assignee: Mark Johnston
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-08-10 15:21 UTC by Tim Chase
Modified: 2018-09-05 00:32 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tim Chase 2018-08-10 15:21:00 UTC
To reproduce

    $ jot 5 > a
    $ wc -c a # it's 10 bytes
      10
    $ sed -e '/2/d;q' a # this is the expected output
    1
    $ sed -i.bak '/2/d;q' a # demonstrate issue
    $ wc -c a # it's still 10 bytes
      10
    $ ls a*  # there's no a.bak file
    a

I've not yet traced down the source of the issue, but there seems to be some odd interaction between `q` and `-i`.

Original thread on Twitter where the issue was noticed:

https://twitter.com/mulle_nat/status/1027907848423067650
Comment 1 Tim Chase 2018-08-11 12:58:15 UTC
I've reduced the test-case to just the `q` and it still works:

    $ sed 2q a
    1
    2
    $ sed -i.bak 2q a
 
This leaves `a` unmodified, without creating a .bak file.
Comment 2 Yuri Pankov 2018-08-19 03:22:02 UTC
What's really wrong here is that `sed -i.ext '...;q'` forgets to remove the temporary files it has created:

$ ls '.!'*
ls: .!*: No such file or directory
$ sed -i.bak 'q' a
$ ls '.!'*
.!64330!a
Comment 3 Yuri Pankov 2018-08-19 03:53:00 UTC
The fix actually *seems* to be pretty simple:

https://reviews.freebsd.org/D16798
Comment 4 Mark Johnston freebsd_committer 2018-08-29 17:28:54 UTC
I missed the PR in the commit, thanks Yuri: https://svnweb.freebsd.org/changeset/base/338375
Comment 5 commit-hook freebsd_committer 2018-09-05 00:31:27 UTC
A commit references this bug:

Author: markj
Date: Wed Sep  5 00:30:36 UTC 2018
New revision: 338462
URL: https://svnweb.freebsd.org/changeset/base/338462

Log:
  MFC r338375:
  sed: Fix -i option behavior with 'q' command.

  PR:	230507

Changes:
_U  stable/11/
  stable/11/sys/kern/imgact_elf.c
  stable/11/usr.bin/sed/extern.h
  stable/11/usr.bin/sed/main.c
  stable/11/usr.bin/sed/process.c
  stable/11/usr.bin/sed/tests/sed2_test.sh