admin avatar

一款优雅强大的开源终端工具-Nushell ,在Mac,Linux系统下的安装以及Nushell的常用命令。

🕢 by admin

一款开源非常优秀强大的终端神器 -Nushell ,附上Mac os和Linux系统下的安装教程。

Nushell 项目。这个项目的目标是采用 Unix 的 shell 哲学,其中管道将简单的命令连接在一起,并将其带入现代开发风格。因此,Nushell 不是 shell 或编程语言,而是通过将丰富的编程语言和功能齐全的 shell 结合到一个包中来连接两者。

Nu 从许多熟悉的领域中汲取灵感:传统 shell(如 bash)、基于对象的 shell(如 PowerShell)、渐进式语言(如 TypeScript)、函数式编程、系统编程等等。但是,Nu 并没有试图成为万事通,而是将精力集中在做好几件事上:

  • 成为具有现代感的灵活的跨平台外壳
  • 作为一种适用于数据结构的现代编程语言来解决问题
  • 提供明确的错误信息和干净的 IDE 支持

Linux系统下安装 Nushell 有两种方法,一种就是直接Nushell官方仓库下载二进制,然后手动配置环境变量。

第二种就是使用cargo 安装,这里介绍第二种方法,首先我们需要先安装cargo ,cargo 是Rust 语言开发环境,安装好Rust 即可直接使用命令安装Rust 的程式,类似于golang 的go get命令安装golang程式一样。。

安装cargo

curl https://sh.rustup.rs -sSf | sh

安装Nushell

cargo install nu

然后输入nu即可进入nu终端工具,如果想要退出道默认的bash终端,执行exit命令即可。

Mac os安装Nushell

brew install nushell

修改用户的shell类型为nu

sudo chsh -s /usr/local/bin/nu username

关闭终端,重新打开终端即可享受Nushell的强大和优雅了。

下面Nushell的常用命令

当您运行这样的命令时,您会注意到的第一件事ls是返回的不是文本块,而是一个结构化表格。

Bash:
1
2
3
4
5
6
7
8
9
10
11
> ls
╭────┬───────────────────────┬──────┬───────────┬─────────────╮
#  │         name          │ type │   size    │  modified   │
├────┼───────────────────────┼──────┼───────────┼─────────────┤
0 │ 404.html              │ file │     429 B │ 3 days ago  │
1 │ CONTRIBUTING.md       │ file │     955 B │ 8 mins ago  │
2 │ Gemfile               │ file │   1.1 KiB │ 3 days ago  │
3 │ Gemfile.lock          │ file │   6.9 KiB │ 3 days ago  │
4 │ LICENSE               │ file │   1.1 KiB │ 3 days ago  │
5 │ README.md             │ file │     213 B │ 3 days ago  │
...

该表不仅仅是以不同的方式显示目录。就像电子表格中的表格一样,该表格允许我们以更交互的方式处理数据。

我们要做的第一件事是按大小对表格进行排序。为此,我们将从中获取输出ls并将其提供给一个命令,该命令可以根据列的内容对表进行排序。

Bash:
1
2
3
4
5
6
7
8
9
10
11
> ls | sort-by size | reverse
╭────┬───────────────────────┬──────┬───────────┬─────────────╮
#  │         name          │ type │   size    │  modified   │
├────┼───────────────────────┼──────┼───────────┼─────────────┤
0 │ Gemfile.lock          │ file │   6.9 KiB │ 3 days ago  │
1 │ SUMMARY.md            │ file │   3.7 KiB │ 3 days ago  │
2 │ Gemfile               │ file │   1.1 KiB │ 3 days ago  │
3 │ LICENSE               │ file │   1.1 KiB │ 3 days ago  │
4 │ CONTRIBUTING.md       │ file │     955 B │ 9 mins ago  │
5 │ books.md              │ file │     687 B │ 3 days ago  │
...

您可以看到,为了完成这项工作,我们没有将命令行参数传递给ls. 相反,我们使用sort-byNu 提供的命令对ls命令的输出进行排序。为了在顶部查看最大的文件,我们还使用了reverse.

Nu 提供了许多可用于表的命令。例如,我们可以过滤ls表的内容,使其只显示超过 1 KB 的文件:

Bash:
1
2
3
4
5
6
7
8
9
> ls | where size > 1kb
╭───┬───────────────────┬──────┬─────────┬────────────╮
# │       name        │ type │  size   │  modified  │
├───┼───────────────────┼──────┼─────────┼────────────┤
0 │ Gemfile           │ file │ 1.1 KiB │ 3 days ago │
1 │ Gemfile.lock      │ file │ 6.9 KiB │ 3 days ago │
2 │ LICENSE           │ file │ 1.1 KiB │ 3 days ago │
3 │ SUMMARY.md        │ file │ 3.7 KiB │ 3 days ago │
╰───┴───────────────────┴──────┴─────────┴────────────╯

就像在 Unix 哲学中一样,能够让命令相互对话为我们提供了在许多不同组合中混合搭配的方法。让我们看一个不同的命令:

Bash:
1
2
3
4
5
6
7
8
9
> ps
╭─────┬──────┬──────────────────────┬─────────┬───────┬───────────┬──────────╮
#  │ pid  │         name         │ status  │  cpu  │    mem    │ virtual  │
├─────┼──────┼──────────────────────┼─────────┼───────┼───────────┼──────────┤
07570 │ nu                   │ Running │  1.96 │  23.2 MiB │ 32.8 GiB │
13533 │ remindd              │ Sleep   │  0.00 │ 103.6 MiB │ 32.3 GiB │
23495 │ TVCacheExtension     │ Sleep   │  0.00 │  11.9 MiB │ 32.2 GiB │
33490 │ MusicCacheExtension  │ Sleep   │  0.00 │  12.9 MiB │ 32.2 GiB │
...

ps如果您使用过 Linux ,您可能会熟悉该命令。通过它,我们可以获得系统当前运行的所有进程的列表,它们的状态是什么,它们的名称是什么。我们还可以看到进程的 CPU 负载。

如果我们想显示正在使用 CPU 的进程怎么办?就像我们之前对ls命令所做的一样,我们也可以使用ps命令返回给我们的表:

Bash:
1
2
3
4
5
6
7
> ps | where cpu > 5
╭───┬──────┬────────────────┬─────────┬────────┬───────────┬──────────╮
# │ pid  │      name      │ status  │  cpu   │    mem    │ virtual  │
├───┼──────┼────────────────┼─────────┼────────┼───────────┼──────────┤
01583 │ Terminal       │ Running │  20.69 │ 127.8 MiB │ 33.0 GiB │
1579 │ photoanalysisd │ Running │ 139.50 │  99.9 MiB │ 32.3 GiB │
╰───┴──────┴────────────────┴─────────┴────────┴───────────┴──────────╯

到目前为止,我们一直在使用lsps列出文件和进程。Nu 还提供了其他可以创建有用信息表的命令。接下来,让我们探索datesys

运行date now为我们提供了有关当前日期和时间的信息:

Bash:
1
2
> date now
2022-03-07 14:14:51.684619600 -08:00

要将日期作为表格,我们可以将其输入date to-table

Bash:
1
2
3
4
5
6
> date now | date to-table
╭───┬──────┬───────┬─────┬──────┬────────┬────────┬──────────╮
# │ year │ month │ day │ hour │ minute │ second │ timezone │
├───┼──────┼───────┼─────┼──────┼────────┼────────┼──────────┤
020223714453 │ -08:00   │
╰───┴──────┴───────┴─────┴──────┴────────┴────────┴──────────╯

Runningsys提供有关 Nu 正在运行的系统的信息:

Bash:
1
2
3
4
5
6
7
8
9
> sys
╭───────┬───────────────────╮
│ host  │ {record 6 fields}
│ cpu   │ [table 4 rows]
│ disks │ [table 3 rows]
│ mem   │ {record 4 fields}
│ temp  │ [table 1 row]
│ net   │ [table 4 rows]
╰───────┴───────────────────╯

这与我们之前看到的表格有点不同。该sys命令为我们提供了一个表格,其中包含单元格中的结构化表格,而不是简单的值。要查看此数据,我们需要获取要查看的列:

Bash:
1
2
3
4
5
6
7
8
9
> sys | get host
╭────────────────┬────────────────────────╮
│ name           │ Debian GNU/Linux       │
│ os version     │ 11
│ kernel version │ 5.10.92-v8+            │
│ hostname       │ lifeless               │
│ uptime         │ 19day 21hr 34min 45sec │
│ sessions       │ [table 1 row]
╰────────────────┴────────────────────────╯

get命令让我们跳转到表的一列的内容。在这里,我们正在查看“主机”列,其中包含有关运行 Nu 的主机的信息。操作系统名称、主机名、CPU 等。让我们获取系统上用户的名称:

Bash:
1
2
3
4
> sys | get host.sessions.name
╭───┬────╮
0 │ jt │
╰───┴────╯

现在,系统上只有一个名为“jt”的用户。您会注意到我们可以传递列路径(host.sessions.name部分)而不仅仅是列的名称。Nu 将采用列路径并转到表中相应的数据位。

您可能已经注意到其他不同之处。我们没有数据表,只有一个元素:字符串“jt”。Nu 适用于数据表和字符串。字符串是在 Nu 之外使用命令的重要部分。

让我们看看字符串在 Nu 之外是如何工作的。我们将采用之前的示例并运行外部echo命令(^告诉 Nu 不要使用内置echo命令):

Bash:
1
2
> sys | get host.sessions.name | each { |it| ^echo $it }
jt

如果这看起来与我们之前的非常相似,那么您的眼光很敏锐!它很相似,但有一个重要的区别:我们调用^echo了我们之前看到的值。这允许我们将数据从 Nu 传递到echo(或 Nu 之外的任何命令,git例如)。

#获得帮助

可以使用命令发现任何 Nu 内置命令的帮助文本help。要查看所有命令,请运行help commands. 您还可以通过执行搜索主题help -f <topic>

Bash:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
> help path
Explore and manipulate paths.

There are three ways to represent a path:

* As a path literal, e.g., '/home/viking/spam.txt'
* As a structured path: a table with 'parent', 'stem', and 'extension' (and
* 'prefix' on Windows) columns. This format is produced by the 'path parse'
  subcommand.
* As an inner list of path parts, e.g., '[[ / home viking spam.txt ]]'.
  Splitting into parts is done by the `path split` command.

All subcommands accept all three variants as an input. Furthermore, the 'path
join' subcommand can be used to join the structured path or path parts back into
the path literal.

Usage:
  > path

Subcommands:
  path basename - Get the final component of a path
  path dirname - Get the parent directory of a path
  path exists - Check whether a path exists
  path expand - Try to expand a path to its absolute form
  path join - Join a structured path or a list of path parts.
  path parse - Convert a path into structured data.
  path relative-to - Get a path as relative to another path.
  path split - Split a path into parts by a separator.
  path type - Get the type of the object a path refers to (e.g., file, dir, symlink)

Flags:
  -h, --help
      Display this help message

玩得愉快。

https://www.nushell.sh/book/

https://github.com/nushell/nushell

💘 相关文章

写一条评论