Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

71%
+3 −0
Q&A What unexpected things can happen if a user runs commands expecting a text file on input lacking a file-final newline?

I tried to think of some for a while, but couldn't find any good ones. That said, there's plenty of programs I don't know. For reference: $ echo -en "hello\nworld" | tee 1.txt | bat -A ───────┬──...

posted 1y ago by matthewsnyder‭  ·  edited 1y ago by matthewsnyder‭

Answer
#3: Post edited by user avatar matthewsnyder‭ · 2023-07-09T00:40:17Z (over 1 year ago)
  • I tried to think of some for a while, but couldn't find any good ones. That said, there's plenty of programs I don't know. For reference:
  • ```
  • $ echo -en "hello
  • world" | tee 1.txt | batf -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world
  • ───────┴─────────────────────────────────────────
  • $ echo -e "hello
  • world" | tee 2.txt | batf -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world␊
  • ───────┴─────────────────────────────────────────
  • ```
  • * `wc` appears to count newlines, so `wc 1.txt` incorrectly returns 1 for line count (you've mentioned this already)
  • * `sort` and `uniq` behave sensibly
  • * Python `readlines()` returns a list where the last item doesn't have a line ending, but this wouldn't normally break anything since usually people call `strip()` on each line (or set `keepends=False`)
  • It's hard to think of an example which is not contrived.
  • From a programmer's perspective, when your program has some specific requirement for input, you would want to either detect it and fail, or automatically correct it. Both are quite easy for missing terminal newlines. Standards are nice, but when you have a few thousand users or more, you simply cannot assume that not a single one would flout them. I would be surprised if there are any real cases where this is a problem, but I'm curious to see what others will post.
  • I do know one common case: It can mess up your shell prompt to print such files:
  • ```
  • $ bash
  • $ cat 1.txt
  • hello
  • world$
  • ```
  • Of course, this is easily resolved by pressing `Enter` to get a new prompt. Note also how I had to open bash for this - my normal shell, fish, handles it:
  • ```
  • $ cat 1.txt
  • hello
  • world⏎
  • $
  • ```
  • As does zsh:
  • ```
  • $ cat 1.txt
  • hello
  • world%
  • $
  • ```
  • I tried to think of some for a while, but couldn't find any good ones. That said, there's plenty of programs I don't know. For reference:
  • ```
  • $ echo -en "hello
  • world" | tee 1.txt | bat -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world
  • ───────┴─────────────────────────────────────────
  • $ echo -e "hello
  • world" | tee 2.txt | bat -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world␊
  • ───────┴─────────────────────────────────────────
  • ```
  • * `wc` appears to count newlines, so `wc 1.txt` incorrectly returns 1 for line count (you've mentioned this already)
  • * `sort` and `uniq` behave sensibly
  • * Python `readlines()` returns a list where the last item doesn't have a line ending, but this wouldn't normally break anything since usually people call `strip()` on each line (or set `keepends=False`)
  • It's hard to think of an example which is not contrived.
  • From a programmer's perspective, when your program has some specific requirement for input, you would want to either detect it and fail, or automatically correct it. Both are quite easy for missing terminal newlines. Standards are nice, but when you have a few thousand users or more, you simply cannot assume that not a single one would flout them. I would be surprised if there are any real cases where this is a problem, but I'm curious to see what others will post.
  • I do know one common case: It can mess up your shell prompt to print such files:
  • ```
  • $ bash
  • $ cat 1.txt
  • hello
  • world$
  • ```
  • Of course, this is easily resolved by pressing `Enter` to get a new prompt. Note also how I had to open bash for this - my normal shell, fish, handles it:
  • ```
  • $ cat 1.txt
  • hello
  • world⏎
  • $
  • ```
  • As does zsh:
  • ```
  • $ cat 1.txt
  • hello
  • world%
  • $
  • ```
  • And of course, `cat` behaves a bit surprisingly (well, not really):
  • ```
  • $ cat * | bat -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ worldhello␊
  • 3 │ world␊
  • ───────┴─────────────────────────────────────────
  • ```
#2: Post edited by user avatar matthewsnyder‭ · 2023-07-09T00:36:27Z (over 1 year ago)
  • I tried to think of some for a while, but couldn't find any good ones. That said, there's plenty of programs I don't know. For reference:
  • ```
  • $ echo -en "hello\nworld" | tee 1.txt | batf -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world
  • ───────┴─────────────────────────────────────────
  • $ echo -e "hello\nworld" | tee 2.txt | batf -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world␊
  • ───────┴─────────────────────────────────────────
  • ```
  • * `wc` appears to count newlines, so `wc 1.txt` incorrectly returns 1 for line count (you've mentioned this already)
  • * `sort` and `uniq` behave sensibly
  • * Python `readlines()` returns a list where the last item doesn't have a line ending, but this wouldn't normally break anything since usually people call `strip()` on each line (or set `keepends=False`)
  • It's hard to think of an example which is not contrived.
  • I tried to think of some for a while, but couldn't find any good ones. That said, there's plenty of programs I don't know. For reference:
  • ```
  • $ echo -en "hello\nworld" | tee 1.txt | batf -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world
  • ───────┴─────────────────────────────────────────
  • $ echo -e "hello\nworld" | tee 2.txt | batf -A
  • ───────┬─────────────────────────────────────────
  • │ STDIN
  • │ Size: -
  • ───────┼─────────────────────────────────────────
  • 1 │ hello␊
  • 2 │ world␊
  • ───────┴─────────────────────────────────────────
  • ```
  • * `wc` appears to count newlines, so `wc 1.txt` incorrectly returns 1 for line count (you've mentioned this already)
  • * `sort` and `uniq` behave sensibly
  • * Python `readlines()` returns a list where the last item doesn't have a line ending, but this wouldn't normally break anything since usually people call `strip()` on each line (or set `keepends=False`)
  • It's hard to think of an example which is not contrived.
  • From a programmer's perspective, when your program has some specific requirement for input, you would want to either detect it and fail, or automatically correct it. Both are quite easy for missing terminal newlines. Standards are nice, but when you have a few thousand users or more, you simply cannot assume that not a single one would flout them. I would be surprised if there are any real cases where this is a problem, but I'm curious to see what others will post.
  • I do know one common case: It can mess up your shell prompt to print such files:
  • ```
  • $ bash
  • $ cat 1.txt
  • hello
  • world$
  • ```
  • Of course, this is easily resolved by pressing `Enter` to get a new prompt. Note also how I had to open bash for this - my normal shell, fish, handles it:
  • ```
  • $ cat 1.txt
  • hello
  • world⏎
  • $
  • ```
  • As does zsh:
  • ```
  • $ cat 1.txt
  • hello
  • world%
  • $
  • ```
#1: Initial revision by user avatar matthewsnyder‭ · 2023-07-09T00:27:31Z (over 1 year ago)
I tried to think of some for a while, but couldn't find any good ones. That said, there's plenty of programs I don't know. For reference:

```
$ echo -en "hello\nworld" | tee 1.txt | batf -A
───────┬─────────────────────────────────────────
       │ STDIN
       │ Size: -
───────┼─────────────────────────────────────────
   1   │ hello␊
   2   │ world
───────┴─────────────────────────────────────────

$ echo -e "hello\nworld" | tee 2.txt | batf -A
───────┬─────────────────────────────────────────
       │ STDIN
       │ Size: -
───────┼─────────────────────────────────────────
   1   │ hello␊
   2   │ world␊
───────┴─────────────────────────────────────────
```

* `wc` appears to count newlines, so `wc 1.txt` incorrectly returns 1 for line count (you've mentioned this already)
* `sort` and `uniq` behave sensibly
* Python `readlines()` returns a list where the last item doesn't have a line ending, but this wouldn't normally break anything since usually people call `strip()` on each line (or set `keepends=False`)

It's hard to think of an example which is not contrived.