工具与资源github.com
用 JavaScript 写 Shell 脚本
JavaScript for Shell Scripting
2021 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
google/zx 是一个用 JavaScript 写 shell 脚本的工具库:它提供一个 `$` 模板标签函数,把命令交给子进程执行并返回 Promise,从而可以 `await $`ls`` 这样在 JS 里串联命令、处理输出,同时默认带上一批常用依赖(fs、chalk、node-fetch 等)。作者是 Google 的 Anton Medvedev,2021 年发布后在 HN 上引发大量讨论,2024 年再次被翻出来。
为什么经久不衰:它把「shell 脚本的便利」和「JavaScript 的生态与数据结构」拼在一起,这类跨界工具总会周期性被重新发现;同时它引发的安全与语法争议也让讨论常读常新。
HN 评论精选
批评 $ 插值不做转义、直接拼接命令字符串,是典型的注入漏洞。
我希望 $ 插值至少会做转义,而不是盲目拼接 shell 脚本吧?算了吧,我还在期待什么。看这段 substitute 源码:它只是把参数直接拼进字符串。唉,又一个代码注入面。模板字符串语法本来就是为这类场景设计的,人们难道没从 20 年的 SQL 注入里学到教训吗?
pwdisswordfish8查看英文原文
I hope the $ interpolator at least performs escaping instead of blindly concatenating shell scripts? Ah, who am I kidding. https://github.com/google/zx/blob/5ba6b775c4c589ecf81a41dfc91a6a5df00449b9/index.mjs#L30-L41 function substitute(arg) { if (arg instanceof ProcessOutput) { return arg.stdout.replace(/\n$/, '') } return arg } // […] let cmd = pieces[0], i = 0 for (; i < args.length; i++) cmd += substitute(args[i]) + pieces[i + 1] for (++i; i < pieces.length; i++) cmd += pieces[i] Sigh… yet another code injection vector. And to think the whole template literal syntax was specifically designed with this in mind. Have people not learned from 20 years of SQL injection?
真正的卖点是开箱即用的常用库,省去在脚本里堆依赖。
说实话,比 `await $`cmd`` 语法更吸引我的是它默认导入了一堆顺手的库。我莫名不喜欢在 bash 脚本顶部写一堆 require(或者为脚本里只用一次的工具把 package.json 塞满),但像 promisified 的 fs、os、chalk、readline、node-fetch 这些确实经常需要。不过真希望他们带上 shelljs,没有它挺奇怪的。也希望他们为没有扩展名(只有 shebang)的 .mjs 文件加上语法高亮支持。
rattray查看英文原文
Honestly the more appealing aspect than the "await $`cmd`" syntax is that it imports a bunch of handy libs by default. For some reason there's just something that rubs me the wrong way about having a bunch of requires at the top of a bash script (or cluttering my package.json with handy tools I use once in a script). But you're gonna want things like (promisified) fs, os, chalk, readline, node-fetch, etc quite a lot of the time. Definitely wish they'd included shelljs though; almost strange not to. I hope they add (or already have) syntax highlighting as .mjs without the file extension (just the shebang) for GitHub, VSCode, etc.
质疑 async-await 在此处的价值,且 `await $`exit 1`` 这种写法很别扭。
点子不错,但如果像第一个例子那样 100% 的调用前面都要加 await,那 async-await 还有什么意义?只是多打字而没有任何收益。我不理解 async-await 为何如此流行,尤其在 JS 里,它的语法和缺乏运行前检查结合起来让我觉得既混乱又容易出错。还有这个,认真的吗?`await $`exit 1``
alpaca128查看英文原文
Neat idea, but what's the point of async-await when you just put await before 100% of calls like in the first example? Now you've got more to type for no gain. I don't get this popularity of async-await, especially in JS where I find its combination of syntax and absence of pre-run checks overly confusing and error-prone. And this, seriously? await $`exit 1`
用 Atwood 定律调侃 JS 不断侵入新领域。
十多年过去,Atwood 定律依然准确:「任何可以用 JavaScript 写的应用,最终都会被用 JavaScript 写出来。」
cltsang查看英文原文
Atwood's law is still accurate after more than a decade: "Any application that can be written in JavaScript, will eventually be written in JavaScript." - Jeff Atwood source: https://blog.codinghorror.com/the-principle-of-least-power/
历次讨论
同类还值得读
直接在浏览器中启动 Linux 内核
Boot a linux kernel right inside your browser.
Fabrice Bellard 的 JSLinux 页面,用 JavaScript 在浏览器里启动一个 Linux 内核,并提供一个可交互的终端环境。
工具与资源上首页 5 次 · 2011–2025bellard.orgHacker News 未公开功能与行为清单(2018)
A List of Hacker News's Undocumented Features and Behaviors (2018)
数据科学家 Max Woolf 整理的 Hacker News 未公开功能与行为清单,涵盖投票机制、排名算法、账号惩罚等社区运作细节。
工具与资源上首页 5 次 · 2019–2025github.comMapSCII:在终端中用盲文和 ASCII 渲染地图
MapSCII – Braille and ASCII map renderer for the console
MapSCII 是一个在命令行终端里渲染地图的开源工具,用盲文字符和 ASCII 字符把地理数据画成可交互浏览的字符地图。
工具与资源上首页 5 次 · 2017–2025github.com顶尖大学的免费在线课程
Free Online Courses from Top Universities
Open Culture 整理的一份免费在线课程合集,汇集了全球顶尖大学(如 MIT、斯坦福、耶鲁等)向公众开放的网络课程资源,涵盖音频、视频等多种形式。
工具与资源上首页 5 次 · 2010–2022openculture.com淘气字符串:容易引发用户输入问题的字符串清单
Naughty Strings: A list of strings likely to cause issues as user-input data
一个 GitHub 仓库,收录了大量容易让程序出问题的用户输入字符串,如特殊字符、emoji、SQL 注入片段、系统命令等,供开发者用于测试输入处理逻辑。
工具与资源上首页 4 次 · 2015–2020github.com