系统与基础设施sdomi.pl
用 Bash 从零写一个 Minecraft 服务器
My thoughts on writing a Minecraft server from scratch in Bash
2022 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇博客记录作者用 Bash 从零实现 Minecraft 服务器的思路与折腾,是一场极端约束下的思想实验。评论指出它的实现范围不止网络部分,连游戏状态管理等也由作者自己覆盖;项目代码还因把 tmpfs 当项目目录并遇到系统崩溃而丢失。
为什么经久不衰:它把“用最不合适的工具做最难的事”的黑客趣味推到极致,既展示 Bash 的能力边界,也让人看到经典项目为何仍能激发折腾精神。
HN 评论精选
纯 Bash 也能处理二进制数据;作者用自己的磁盘驱动器项目证明可行,只是效率很低。
你可以在纯 Bash 里读写二进制,包括空字节,甚至不用子 shell,更不必说 dd、xxd 这类外部进程。我写过一个带 RS232 接口的老式便携磁盘驱动器驱动/客户端;磁盘驱动器当然要处理任意数据,包括 0x00。它完全用原生 Bash,没有外部工具,也没有子 shell;只在启动时调用 stty 和 mkfifo 打开串口。读空字节的窍门不太高效:一次读一个字符,靠 read 的 errorlevel 区分“什么也没读到”和“读到了 0x00”。在这个场景还行,因为磁盘又慢又小,整盘最多 200k;今天让 Bash 逐字节读 200k 不算什么,只因为硬件太强。但这是可行的,而且文章开头也说是思想实验。类似地,你不需要 xxd 来做原始二进制和文本编码的互转。
Brian_K_White查看英文原文
You can read and write binary, including null, in pure bash, without even subshells, let alone external processes like dd and xxd. This is a driver/client for a vintage portable disk drive that has an rs232 interface. Any disk drive obviously has to handle arbitrary data including binary including 0x00. It's entirely in native bash with no external tools and not even any subshells. It does have to call stty and mkfifo once at startup, but that's just startup like to open the serial port, not part of the work. https://github.com/bkw777/pdd.sh The gimmick for reading nulls is not exactly efficient. Basically you read one character at a time and look at the errorlevel from read to detect the difference between "got nothing" and "got 0x00" It's fine for this case because the drive is so slow and small that even the entire disk is only 200k max. Making bash read one byte at a time for 200k is still nothing today but only because hardware is insane today. But it's possible. And the beginning of the post does say "as a thought experiment". Similarly, you don't need xxd to convert back & forth between raw binary and text encoding. My own similar thought experiment included the idea that, if I'm not going to use any external process I can possibly avoid and only use internal bash features, on the flip side of that, I will squeez bash for all it's worth, allow every bashism possible.
项目代码因把 tmpfs 当项目目录并遇系统崩溃而丢失,增添戏剧性。
> 代码目前丢失了,因为我大量把 tmpfs 当项目目录,然后系统崩溃了。 仿佛这个项目本身还不够有挑战,还要再加点俄罗斯轮盘赌!
zymhan查看英文原文
> The code is currently lost, due to my extensive use of tmpfs as a project directory, and a system crash. As if the project wasn't enough of a challenge on it's own, let's throw in some russian roulette!
实现范围远超预期,几乎连游戏状态管理都自己写。
我原本以为这会是“网络代码用 Bash 写;实际游戏状态管理就用现成的 X”。但没有,看起来他们把所有部分都覆盖了。哇。
Waterluvian查看英文原文
I was expecting this to just be "the netcode was written in Bash. For the actual game state management, we just use existing X." But no, it looks like they've covered ALL of that. Wow.
历次讨论
同类还值得读
TTY 揭秘(2008)
The TTY demystified (2008)
经典文章深入剖析了 Unix/Linux 中 TTY 子系统的工作原理,包括线路规程、终端驱动、信号与控制字符等机制。
系统与基础设施上首页 11 次 · 2009–2025linusakesson.net编辑精选为什么 GNU grep 这么快
Why GNU grep is Fast
FreeBSD 邮件列表上一篇广为流传的帖子,解释 GNU grep 为什么快:核心在于 Boyer-Moore 字符串搜索算法配合精心优化的内部循环和 I/O,让程序在搜索时“…
系统与基础设施上首页 9 次 · 2010–2022lists.freebsd.orgXplain:为普通人讲解 X11
Xplain – Explaining X11 for the rest of us
《Xplain》是作者(HN 用户 Jasper_)写的一系列 X11 长文,用他在浏览器中重建的 X11 绘图模型做交互演示,讲解 X11 的绘图模型与窗口系统设计。
系统与基础设施上首页 6 次 · 2013–2020magcius.github.io编辑精选旋风教程:为 Linux 创建极小 ELF 可执行文件(1999)
A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux (1999)
breadbox 写于 1999 年的经典教程,主题是如何为 Linux 手工构造并裁剪出体积极小的 ELF 可执行文件。
系统与基础设施上首页 6 次 · 2007–2025muppetlabs.comPostgreSQL 内部原理
The internals of PostgreSQL
《The Internals of PostgreSQL》是一本免费在线书籍,系统讲解 PostgreSQL 的内部实现,包括查询处理、并发控制、WAL、缓冲区管理等核心机制。
系统与基础设施上首页 5 次 · 2017–2025interdb.jp编辑精选