(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=41512899

shell 用作控制系统操作的用户界面,包括管理数据库、REST API、文件和电子邮件。 然而,shell 的主要功能不是执行这些任务本身,而是执行专门为其设计的其他程序。 这种误解源于认为 shell 负责处理核心功能,而实际上,它只是充当用户和这些专用软件组件之间的管道。 与流行的 UNIX shell 实现相比,由单个组织开发的 PowerShell 由于该组织提供的一致的内部 API,提供了更简化的方法。 另一方面,开源 UNIX 工具缺乏这种一致性,因为它们依赖于社区贡献。 UNIX shell 实用程序通常配备内置命令,但它们在实现上有所不同,这使得互操作性具有挑战性。 例如,许多工具假设用户将通过管道处理数据并相应地转换输出。 一个例子是具有内置排序功能的“ps”实用程序,与其他工具的实现相比,其行为可能截然不同。 尽管如此,明文仍然是大多数 UNIX 工具中的通用语言,确保遵守默认值时的兼容性。 一种特定的用例涉及处理 netstat 输出。 在这里,我们演示如何利用 GNU AWK 解析并显示列中排序的网络连接信息,同时保持可读性。 或者,可以将输出发送到“column -t”,它自动处理行对齐。 尽管最初不如专用解决方案那么简单,但一旦熟悉了 GNU AWk,操作输出就会变得直观。 此外,链接其他实用程序(如 grep、sort 和 uniq)仍然可以进行进一步的定制。 最后,值得注意的是,考虑到它们的历史稳定性以及开发人员对对其一致性的广泛依赖的认识,对已建立的 UNIX 工具中的输出格式进行更改通常在生产环境中几乎不会带来风险。 最终,掌握这些工具主要在于理解可用的命令集、复杂的语法,并有效地应用这些知识。

相关文章

原文














































































































































































































































































































> What's the point of the shell, if not to manage your databases, your REST APIs, files, and mail? Is it something you use for playing games on, or just for fun?

To call other programs to do those things. Why on earth would I want my shell to directly manage any of those things?

I think you're forgetting something: *nix tools are built by a community, PowerShell is built by a company. Much like Apple, Microsoft can insist on and guarantee that their internal API is consistent. *nix tooling cannot (nor would it ever try to) do the same.

> It means that "ps" has a built-in sort command, as do most other UNIX standard utilities, but they all do it differently.

I haven't done an exhaustive search, but I doubt that most *nix tooling has a built-in sort. Generally speaking, they're built on the assumption that you'll pipe output as necessary to other tools.

> This also means that you just "need to know" how to convince each and every command to output machine-readable formats that other tools on the pipeline can pick up safely.

No, you don't, because plaintext output is the lingua franca of *nix tooling. If you build a tool intended for public consumption and it _doesn't_ output in plaintext by default, you're doing it wrong.

Here's a one-liner with GNU awk; you can elide the first `printf` if you don't want headers. Similarly, you can change the output formatting however you want. Or, you could skip that altogether, and pipe the output to `column -t` to let it handle alignment.

    netstat -nA inet | gawk -F':' 'NR > 2 { split($2, a, / /); pc[a[1]]++ } END { printf "%-5s     %s\n", "PORT", "COUNT"; PROCINFO["sorted_in"]="@val_num_desc"; c=0; for(i in pc) if (c++ < 10) { printf "%-5s     %-5s\n", i, pc[i] } }'
Example output:
    PORT      COUNT
    6808      16
    3300      8
    6800      6
    6802      2
    6804      2
    6806      2
    60190     1
    34362     1
    34872     1
    38716     1

Obviously this is not as immediately straight-forward for the specific task, though if you already know awk, it kind of is:
    Set the field separator to `:`
    Skip the first two lines (because they're informational headers)
    Split the 2nd column on space to skip the foreign IP
    Store that result in variable `a`
    Create and increment array `pc` keyed on the port
    When done, do the following
    Print a header
    Sort numerically, descending
    Initialize a counter at 0
    For every element in the pc array, until count hits 10, print the value and key
You can also chain together various `grep`, `sort`, and `uniq` calls as a sibling comment did. And if your distro doesn't include GNU awk, then you probably _would_ have to do this.

You may look at this and scoff, but really, what is the difference? With yours, I have to learn a bunch of commands, predicates, options, and syntax. With mine, I have to... learn a bunch of commands, predicates, options, and syntax (or just awk ;-)).

> This kind of thing is a challenge with UNIX tools

It's only a challenge if you don't know how to use the tools.

> Any change to the output format of netstat breaks scripts in fun and create ways

The last release of `netstat` was in 2014. *nix tools aren't like JavaScript land; they tend to be extremely stable. Even if they _do_ get releases, if you're using a safe distro in prod (i.e. Debian, RedHat), you're not going to get a surprise update. Finally, the authors and maintainers of such tools are painfully aware that tons of scripts around the world depend on them being consistent, and as such, are highly unlikely to break that.

> Silently. In production.

If you aren't thoroughly testing and validating changes in prod, that's not the fault of the tooling.

























联系我们 contact @ memedata.com