mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-21 02:10:11 +08:00
commit
48e9af80c4
@ -0,0 +1,305 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (chensanle)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-13107-1.html)
|
||||
[#]: subject: (Tmux Command Examples To Manage Multiple Terminal Sessions)
|
||||
[#]: via: (https://www.ostechnix.com/tmux-command-examples-to-manage-multiple-terminal-sessions/)
|
||||
[#]: author: (sk https://www.ostechnix.com/author/sk/)
|
||||
|
||||
基于 Tmux 的多会话终端管理示例
|
||||
======
|
||||
|
||||

|
||||
|
||||
我们已经了解到如何通过 [GNU Screen][2] 进行多会话管理。今天,我们将要领略另一个著名的管理会话的命令行实用工具 **Tmux**。类似 GNU Screen,Tmux 是一个帮助我们在单一终端窗口中创建多个会话,同一时间内同时运行多个应用程序或进程的终端复用工具。Tmux 自由、开源并且跨平台,支持 Linux、OpenBSD、FreeBSD、NetBSD 以及 Mac OS X。本文将讨论 Tmux 在 Linux 系统下的高频用法。
|
||||
|
||||
### Linux 下安装 Tmux
|
||||
|
||||
Tmux 可以在绝大多数的 Linux 官方仓库下获取。
|
||||
|
||||
在 Arch Linux 或它的变种系统下,执行下列命令来安装:
|
||||
|
||||
```
|
||||
$ sudo pacman -S tmux
|
||||
```
|
||||
|
||||
Debian、Ubuntu 或 Linux Mint:
|
||||
|
||||
```
|
||||
$ sudo apt-get install tmux
|
||||
```
|
||||
|
||||
Fedora:
|
||||
```
|
||||
$ sudo dnf install tmux
|
||||
```
|
||||
|
||||
RHEL 和 CentOS:
|
||||
|
||||
```
|
||||
$ sudo yum install tmux
|
||||
```
|
||||
|
||||
SUSE/openSUSE:
|
||||
|
||||
```
|
||||
$ sudo zypper install tmux
|
||||
```
|
||||
|
||||
以上,我们已经完成 Tmux 的安装。之后我们继续看看一些 Tmux 示例。
|
||||
|
||||
### Tmux 命令示例: 多会话管理
|
||||
|
||||
Tmux 默认所有命令的前置命令都是 `Ctrl+b`,使用前牢记这个快捷键即可。
|
||||
|
||||
> **注意**:**Screen** 的前置命令都是 `Ctrl+a`.
|
||||
|
||||
#### 创建 Tmux 会话
|
||||
|
||||
在终端中运行如下命令创建 Tmux 会话并附着进入:
|
||||
|
||||
```
|
||||
tmux
|
||||
```
|
||||
|
||||
抑或,
|
||||
|
||||
```
|
||||
tmux new
|
||||
```
|
||||
|
||||
一旦进入 Tmux 会话,你将看到一个 **沉在底部的绿色的边栏**,如下图所示。
|
||||
|
||||
![][3]
|
||||
|
||||
*创建 Tmux 会话*
|
||||
|
||||
这个绿色的边栏能很容易提示你当前是否身处 Tmux 会话当中。
|
||||
|
||||
#### 退出 Tmux 会话
|
||||
|
||||
退出当前 Tmux 会话仅需要使用 `Ctrl+b` 和 `d`。无需同时触发这两个快捷键,依次按下 `Ctrl+b` 和 `d` 即可。
|
||||
|
||||
退出当前会话后,你将能看到如下输出:
|
||||
|
||||
```
|
||||
[detached (from session 0)]
|
||||
```
|
||||
|
||||
#### 创建有名会话
|
||||
|
||||
如果使用多个会话,你很可能会混淆运行在多个会话中的应用程序。这种情况下,我们需要会话并赋予名称。譬如需要 web 相关服务的会话,就创建一个名称为 “webserver”(或任意一个其他名称) 的 Tmux 会话。
|
||||
|
||||
```
|
||||
tmux new -s webserver
|
||||
```
|
||||
|
||||
这里是新的 Tmux 有名会话:
|
||||
|
||||
![][4]
|
||||
|
||||
*拥有自定义名称的 Tmux 会话*
|
||||
|
||||
如你所见上述截图,这个 Tmux 会话的名称已经被标注为 “webserver”。如此,你可以在多个会话中,轻易的区分应用程序的所在。
|
||||
|
||||
退出会话,轻按 `Ctrl+b` 和 `d`。
|
||||
|
||||
#### 查看 Tmux 会话清单
|
||||
|
||||
查看 Tmux 会话清单,执行:
|
||||
|
||||
```
|
||||
tmux ls
|
||||
```
|
||||
|
||||
示例输出:
|
||||
|
||||
![][5]
|
||||
|
||||
*列出 Tmux 会话*
|
||||
|
||||
如你所见,我们开启了两个 Tmux 会话。
|
||||
|
||||
#### 创建非附着会话
|
||||
|
||||
有时候,你可能想要简单创建会话,但是并不想自动切入该会话。
|
||||
|
||||
创建一个非附着会话,并赋予名称 “ostechnix”,运行:
|
||||
|
||||
```
|
||||
tmux new -s ostechnix -d
|
||||
```
|
||||
|
||||
上述命令将会创建一个名为 “ostechnix” 的会话,但是并不会附着进入。
|
||||
|
||||
你可以通过使用 `tmux ls` 命令验证:
|
||||
|
||||
![][6]
|
||||
|
||||
*创建非附着会话*
|
||||
|
||||
#### 附着进入 Tmux 会话
|
||||
|
||||
通过如下命令,你可以附着进入最后一个被创建的会话:
|
||||
|
||||
```
|
||||
tmux attach
|
||||
```
|
||||
|
||||
抑或,
|
||||
|
||||
```
|
||||
tmux a
|
||||
```
|
||||
|
||||
如果你想附着进入任意一个指定的有名会话,譬如 “ostechnix”,运行:
|
||||
|
||||
```
|
||||
tmux attach -t ostechnix
|
||||
```
|
||||
|
||||
或者,简写为:
|
||||
|
||||
```
|
||||
tmux a -t ostechnix
|
||||
```
|
||||
|
||||
#### 关闭 Tmux 会话
|
||||
|
||||
当你完成或者不再需要 Tmux 会话,你可以通过如下命令关闭:
|
||||
|
||||
```
|
||||
tmux kill-session -t ostechnix
|
||||
```
|
||||
|
||||
当身处该会话时,使用 `Ctrl+b` 以及 `x`。点击 `y` 来关闭会话。
|
||||
|
||||
可以通过 `tmux ls` 命令验证。
|
||||
|
||||
关闭所有 Tmux 服务下的所有会话,运行:
|
||||
|
||||
```
|
||||
tmux kill-server
|
||||
```
|
||||
|
||||
谨慎!这将终止所有 Tmux 会话,并不会产生任何警告,即便会话存在运行中的任务。
|
||||
|
||||
如果不存在活跃的 Tmux 会话,将看到如下输出:
|
||||
|
||||
```
|
||||
$ tmux ls
|
||||
no server running on /tmp/tmux-1000/default
|
||||
```
|
||||
|
||||
#### 切割 Tmux 窗口
|
||||
|
||||
切割窗口成多个小窗口,在 Tmux 中,这个叫做 “Tmux 窗格”。每个窗格中可以同时运行不同的程序,并同时与所有的窗格进行交互。每个窗格可以在不影响其他窗格的前提下可以调整大小、移动位置和控制关闭。我们可以以水平、垂直或者二者混合的方式切割屏幕。
|
||||
|
||||
##### 水平切割窗格
|
||||
|
||||
欲水平切割窗格,使用 `Ctrl+b` 和 `"`(半个双引号)。
|
||||
|
||||
![][7]
|
||||
|
||||
*水平切割 Tmux 窗格*
|
||||
|
||||
可以使用组合键进一步切割面板。
|
||||
|
||||
##### 垂直切割窗格
|
||||
|
||||
垂直切割面板,使用 `Ctrl+b` 和 `%`。
|
||||
|
||||
![][8]
|
||||
|
||||
*垂直切割 Tmux 窗格*
|
||||
|
||||
##### 水平、垂直混合切割窗格
|
||||
|
||||
我们也可以同时采用水平和垂直的方案切割窗格。看看如下截图:
|
||||
|
||||
![][9]
|
||||
|
||||
*切割 Tmux 窗格*
|
||||
|
||||
首先,我通过 `Ctrl+b` `"` 水平切割,之后通过 `Ctrl+b` `%` 垂直切割下方的窗格。
|
||||
|
||||
如你所见,每个窗格下我运行了不同的程序。
|
||||
|
||||
##### 切换窗格
|
||||
|
||||
通过 `Ctrl+b` 和方向键(上下左右)切换窗格。
|
||||
|
||||
##### 发送命令给所有窗格
|
||||
|
||||
之前的案例中,我们在每个窗格中运行了三个不同命令。其实,也可以发送相同的命令给所有窗格。
|
||||
|
||||
为此,使用 `Ctrl+b` 然后键入如下命令,之后按下回车:
|
||||
|
||||
```
|
||||
:setw synchronize-panes
|
||||
```
|
||||
|
||||
现在在任意窗格中键入任何命令。你将看到相同命令影响了所有窗格。
|
||||
|
||||
##### 交换窗格
|
||||
|
||||
使用 `Ctrl+b` 和 `o` 交换窗格。
|
||||
|
||||
##### 展示窗格号
|
||||
|
||||
使用 `Ctrl+b` 和 `q` 展示窗格号。
|
||||
|
||||
##### 终止窗格
|
||||
|
||||
要关闭窗格,直接键入 `exit` 并且按下回车键。或者,按下 `Ctrl+b` 和 `x`。你会看到确认信息。按下 `y` 关闭窗格。
|
||||
|
||||
![][10]
|
||||
|
||||
*关闭窗格*
|
||||
|
||||
##### 放大和缩小 Tmux 窗格
|
||||
|
||||
我们可以将 Tmux 窗格放大到当前终端窗口的全尺寸,以获得更好的文本可视性,并查看更多的内容。当你需要更多的空间或专注于某个特定的任务时,这很有用。在完成该任务后,你可以将 Tmux 窗格缩小(取消放大)到其正常位置。更多详情请看以下链接。
|
||||
|
||||
- [如何缩放 Tmux 窗格以提高文本可见度?](https://ostechnix.com/how-to-zoom-tmux-panes-for-better-text-visibility/)
|
||||
|
||||
#### 自动启动 Tmux 会话
|
||||
|
||||
当通过 SSH 与远程系统工作时,在 Tmux 会话中运行一个长期运行的进程总是一个好的做法。因为,它可以防止你在网络连接突然中断时失去对运行进程的控制。避免这个问题的一个方法是自动启动 Tmux 会话。更多详情,请参考以下链接。
|
||||
|
||||
- [通过 SSH 登录远程系统时自动启动 Tmux 会话](https://ostechnix.com/autostart-tmux-session-on-remote-system-when-logging-in-via-ssh/)
|
||||
|
||||
### 总结
|
||||
|
||||
这个阶段下,你已经获得了基本的 Tmux 技能来进行多会话管理,更多细节,参阅 man 页面。
|
||||
|
||||
```
|
||||
$ man tmux
|
||||
```
|
||||
|
||||
GNU Screen 和 Tmux 工具都能透过 SSH 很好的管理远程服务器。学习 Screen 和 Tmux 命令,像个行家一样,彻底通过这些工具管理远程服务器。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/tmux-command-examples-to-manage-multiple-terminal-sessions/
|
||||
|
||||
作者:[sk][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[chensanle](https://github.com/chensanle)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.ostechnix.com/author/sk/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.ostechnix.com/wp-content/uploads/2019/06/Tmux-720x340.png
|
||||
[2]: https://www.ostechnix.com/screen-command-examples-to-manage-multiple-terminal-sessions/
|
||||
[3]: https://www.ostechnix.com/wp-content/uploads/2019/06/Tmux-session.png
|
||||
[4]: https://www.ostechnix.com/wp-content/uploads/2019/06/Named-Tmux-session.png
|
||||
[5]: https://www.ostechnix.com/wp-content/uploads/2019/06/List-Tmux-sessions.png
|
||||
[6]: https://www.ostechnix.com/wp-content/uploads/2019/06/Create-detached-sessions.png
|
||||
[7]: https://www.ostechnix.com/wp-content/uploads/2019/06/Horizontal-split.png
|
||||
[8]: https://www.ostechnix.com/wp-content/uploads/2019/06/Vertical-split.png
|
||||
[9]: https://www.ostechnix.com/wp-content/uploads/2019/06/Split-Panes.png
|
||||
[10]: https://www.ostechnix.com/wp-content/uploads/2019/06/Kill-panes.png
|
@ -1,32 +1,32 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (Chao-zhi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-13104-1.html)
|
||||
[#]: subject: (Top Arch-based User Friendly Linux Distributions That are Easier to Install and Use Than Arch Linux Itself)
|
||||
[#]: via: (https://itsfoss.com/arch-based-linux-distros/)
|
||||
[#]: author: (Dimitrios Savvopoulos https://itsfoss.com/author/dimitrios/)
|
||||
|
||||
最好用的几个基于 Arch 的用户友好型 Linux 发行版,它们比 Arch Linux 更容易安装和使用
|
||||
9 个易用的基于 Arch 的用户友好型 Linux 发行版
|
||||
======
|
||||
|
||||
在 Linux 社区中,[Arch Linux][1] 有一群狂热的追随者。这个轻量级的发行版以 DIY 的态度提供了最前沿的更新。
|
||||
|
||||
但是,Arch 的目标用户是那些更有经验的用户。因此,它通常被认为是那些技术不够(或耐心不够)的人所无法触及的。
|
||||
|
||||
事实上,第一步,[安装 Arch Linux 就足以把很多人吓跑 ][2]。与大多数其他发行版不同,Arch Linux 没有一个易于使用的图形安装程序。安装过程中涉及到的磁盘分区,连接到互联网,挂载驱动器和创建文件系统等只用命令行工具来操作。
|
||||
事实上,只是最开始的步骤,[安装 Arch Linux 就足以把很多人吓跑][2]。与大多数其他发行版不同,Arch Linux 没有一个易于使用的图形安装程序。安装过程中涉及到的磁盘分区,连接到互联网,挂载驱动器和创建文件系统等只用命令行工具来操作。
|
||||
|
||||
对于那些不想经历复杂的安装和设置的人来说,有许多用户友好的基于 Arch 的发行版。
|
||||
|
||||
在本文中,我将向您展示一些 Arch 替代发行版。这些发行版附带了图形安装程序、图形包管理器和其他工具,它们比命令行工具更容易使用。
|
||||
在本文中,我将向你展示一些 Arch 替代发行版。这些发行版附带了图形安装程序、图形包管理器和其他工具,比它们的命令行版本更容易使用。
|
||||
|
||||
### 更容易设置和使用的基于 Arch 的 Linux 发行版
|
||||
|
||||
![][3]
|
||||

|
||||
|
||||
请注意,这不是一个排名列表。这些数字只是为了计数的目的。排第二的发行版不应该被认为比排第七的发行版好。
|
||||
|
||||
#### 1\。Manjaro Linux
|
||||
#### 1、Manjaro Linux
|
||||
|
||||
![][4]
|
||||
|
||||
@ -34,51 +34,51 @@
|
||||
|
||||
Manjaro 提供了 Arch Linux 的所有优点,同时注重用户友好性和可访问性。Manjaro 既适合新手,也适合有经验的 Linux 用户。
|
||||
|
||||
**对于新手**,我们提供了一个用户友好的安装程序,系统本身的设计可以完全“开箱即用”地工作在您[最喜爱的桌面环境 ][6](DE) 或窗口管理器中。
|
||||
**对于新手**,它提供了一个用户友好的安装程序,系统本身也设计成可以在你[最喜爱的桌面环境 ][6](DE)或窗口管理器中直接“开箱即用”。
|
||||
|
||||
**对于更有经验的用户,** Manjaro 还提供多种功能,以满足每个个人的口味和喜好。[Manjaro Architect][7] 提供了安装各种 Manjaro 风格的选项,并为那些想要完全自由地塑造系统的人提供了各种桌面环境、文件系统([最近推出的 ZFS][8]) 和引导程序选择。
|
||||
**对于更有经验的用户**,Manjaro 还提供多种功能,以满足每个个人的口味和喜好。[Manjaro Architect][7] 提供了安装各种 Manjaro 风格的选项,并为那些想要完全自由地塑造系统的人提供了各种桌面环境、文件系统([最近推出的 ZFS][8]) 和引导程序的选择。
|
||||
|
||||
Manjaro 也是一个滚动发行的前沿发行版。然而,与 Arch 不同的是,Manjaro 首先测试更新,然后将其提供给用户。稳定在这里也很重要。
|
||||
Manjaro 也是一个滚动发布的前沿发行版。然而,与 Arch 不同的是,Manjaro 首先测试更新,然后将其提供给用户。稳定在这里也很重要。
|
||||
|
||||
#### 2\。ArcoLinux
|
||||
#### 2、ArcoLinux
|
||||
|
||||
![][9]
|
||||
|
||||
[ArcoLinux][10]( 以前称为 ArchMerge) 是一个基于 Arch Linux 的发行版。开发团队提供了三种变体。ArcoLinux、ArcoLinuxD 和 ArcoLinuxB。
|
||||
[ArcoLinux][10](以前称为 ArchMerge)是一个基于 Arch Linux 的发行版。开发团队提供了三种变体。ArcoLinux、ArcoLinuxD 和 ArcoLinuxB。
|
||||
|
||||
ArcoLinux 是一个功能齐全的发行版,附带有 [Xfce desktop][11]、[Openbox][12] 和 [i3 window managers][13]。
|
||||
ArcoLinux 是一个功能齐全的发行版,附带有 [Xfce 桌面][11]、[Openbox][12] 和 [i3 窗口管理器][13]。
|
||||
|
||||
**ArcoLinuxD** 是一个最小的发行版,包含允许高级用户安装任何桌面和应用程序的脚本。
|
||||
**ArcoLinuxD** 是一个精简的发行版,它包含了一些脚本,可以让高级用户安装任何桌面和应用程序。
|
||||
|
||||
**ArcoLinuxB** 是一个让用户能够构建自定义发行版的项目,同时还开发了几个带有预配置桌面的社区版本,如 Awesome、bspwm、Budgie、Cinnamon、Deepin、GNOME、MATE 和 KDE Plasma。
|
||||
|
||||
ArcoLinux 还提供了各种视频教程,因为它非常注重学习和获取 Linux 技能。
|
||||
|
||||
#### 3\。Archlabs Linux
|
||||
#### 3、Archlabs Linux
|
||||
|
||||
![][14]
|
||||
|
||||
[ArchLabs Linux][15] 是一个轻量级的滚动版 Linux 发行版,基于最基础的 Arch Linux,带有 [Openbox][16] 窗口管理器。[ArchLabs][17] 在观感设计中受到 [BunsenLabs][18] 的影响和启发,主要考虑到中级到高级用户的需求。
|
||||
[ArchLabs Linux][15] 是一个轻量级的滚动版 Linux 发行版,基于最精简的 Arch Linux,带有 [Openbox][16] 窗口管理器。[ArchLabs][17] 在观感设计中受到 [BunsenLabs][18] 的影响和启发,主要考虑到中级到高级用户的需求。
|
||||
|
||||
#### 4\。Archman Linux
|
||||
#### 4、Archman Linux
|
||||
|
||||
![][19]
|
||||
|
||||
[Archman][20] 是一个独立的项目。Arch Linux 发行版对于没有多少 Linux 经验的用户来说通常不是理想的操作系统。大量的背景阅读是必要的事情,以尽可能减少挫折感的意义。Archman Linux 的开发人员正试图改变这种评价。
|
||||
[Archman][20] 是一个独立的项目。Arch Linux 发行版对于没有多少 Linux 经验的用户来说通常不是理想的操作系统。要想在最小的挫折感下让事情变得更有意义,必须要有相当的背景知识。Archman Linux 的开发人员正试图改变这种评价。
|
||||
|
||||
Archman 的开发基于对开发的理解,包括用户反馈和体验组件。根据我们团队过去的经验,将用户的反馈和要求融合在一起,确定路线图并完成构建工作。
|
||||
Archman 的开发是基于对开发的理解,包括用户反馈和体验组件。根据团队过去的经验,将用户的反馈和要求融合在一起,确定路线图并完成构建工作。
|
||||
|
||||
#### 5\。EndeavourOS
|
||||
#### 5、EndeavourOS
|
||||
|
||||
![][21]
|
||||
|
||||
当流行的基于 Arch 的发行版 [Antergos 在 2019 停止 ][22] 时,它留下了一个友好且非常有用的社区。Antergos 项目结束了,因为系统对于开发人员来说太难维护了。
|
||||
当流行的基于 Arch 的发行版 [Antergos 在 2019 结束][22] 时,它留下了一个友好且非常有用的社区。Antergos 项目结束的原因是因为该系统对于开发人员来说太难维护了。
|
||||
|
||||
在发布后的几天内,一些有经验的用户通过创建一个新的发行版来填补 Antergos 留下的空白,从而维护了以前的社区。这就是 [EndeavourOS][23] 的诞生。
|
||||
在宣布结束后的几天内,一些有经验的用户通过创建一个新的发行版来填补 Antergos 留下的空白,从而维护了以前的社区。这就是 [EndeavourOS][23] 的诞生。
|
||||
|
||||
[EndeavourOS][24] 是轻量级的,并且附带了最少数量的预装应用程序。一块近乎空白的画布,随时可以个性化。
|
||||
|
||||
#### 6\。RebornOS
|
||||
#### 6、RebornOS
|
||||
|
||||
![][25]
|
||||
|
||||
@ -86,45 +86,45 @@ Archman 的开发基于对开发的理解,包括用户反馈和体验组件。
|
||||
|
||||
RebornOS 还声称支持 [Anbox][27],它可以在桌面 Linux 上运行 Android 应用程序。它还提供了一个简单的内核管理器 GUI 工具。
|
||||
|
||||
再加上 [Pacman][28],[AUR][29],以及定制版本的 Cnchi 图形安装程序,Arch Linux 终于可以让最没有经验的用户也能够使用了。
|
||||
再加上 [Pacman][28]、[AUR][29],以及定制版本的 Cnchi 图形安装程序,Arch Linux 终于可以让最没有经验的用户也能够使用了。
|
||||
|
||||
#### 7\。Chakra Linux
|
||||
#### 7、Chakra Linux
|
||||
|
||||
![][30]
|
||||
|
||||
一个社区开发的 GNU/Linux 发行版,它的亮点在 KDE 和 Qt 技术。[Chakra Linux][31] 不在特定日期安排发布,而是使用“半滚动发布”系统。
|
||||
|
||||
这意味着 Chakra Linux 的核心包被冻结,只更新以修复任何安全问题。这些软件包是在最新版本经过彻底测试后更新的,然后再转移到永久存储库(大约每六个月更新一次)。
|
||||
这意味着 Chakra Linux 的核心包被冻结,只在修复安全问题时才会更新。这些软件包是在最新版本经过彻底测试后更新的,然后再转移到永久软件库(大约每六个月更新一次)。
|
||||
|
||||
除官方存储库外,用户还可以安装 Chakra 社区存储库 (CCR) 的软件包,该库为官方存储库中未包含的软件提供用户制作的 PKGINFOs 和 [PKGBUILD][32] 脚本,这是受 Arch 用户存储库的启发而出现的。
|
||||
除官方软件库外,用户还可以安装 Chakra 社区软件库 (CCR) 的软件包,该库为官方存储库中未包含的软件提供用户制作的 PKGINFOs 和 [PKGBUILD][32] 脚本,其灵感来自于 Arch 用户软件库(AUR)。
|
||||
|
||||
#### 8\。Artix Linux
|
||||
#### 8、Artix Linux
|
||||
|
||||
![Artix Mate Edition][33]
|
||||
|
||||
[Artix Linux][34] 也是一个基于 Arch Linux 的滚动发行版,它使用 [OpenRC][35]、[runit][36] 或 [s6][37] 作为初始化工具而不是 [systemd][38]。
|
||||
|
||||
Artix Linux 有自己的软件包存储库,但作为一个基于 pacman 的发行版,它可以使用 Arch Linux 存储库或任何其他衍生发行版的软件包,甚至可以使用显式依赖于 systemd 的软件包。也可以使用 [Arch User Repository][29](AUR)。
|
||||
Artix Linux 有自己的软件库,但作为一个基于 `pacman` 的发行版,它可以使用 Arch Linux 软件库或任何其他衍生发行版的软件包,甚至可以使用明确依赖于 systemd 的软件包。也可以使用 [Arch 用户软件库][29](AUR)。
|
||||
|
||||
#### 10\。BlackArch Linux
|
||||
#### 9、BlackArch Linux
|
||||
|
||||
![][39]
|
||||
|
||||
BlackArch 是一个基于 Arch Linux 的[渗透测试发行版 ][40],它提供了大量的网络安全工具。它是专门为渗透测试人员和安全研究人员创建的。该存储库包含 2400 多个[黑客和渗透测试工具 ][41],可以单独安装,也可以分组安装。BlackArch Linux 兼容现有的 Arch Linux 包。
|
||||
BlackArch 是一个基于 Arch Linux 的 [渗透测试发行版][40],它提供了大量的网络安全工具。它是专门为渗透测试人员和安全研究人员创建的。该软件库包含 2400 多个[黑客和渗透测试工具 ][41],可以单独安装,也可以分组安装。BlackArch Linux 兼容现有的 Arch Linux 包。
|
||||
|
||||
### 想要真正的原版 Arch Linux 吗?可以使用图形化 Arch 安装程序简化安装
|
||||
|
||||
如果您想使用原版的 Arch Linux,但又被它困难的安装所难倒。幸运的是,您可以下载一个带有图形安装程序的 Arch Linux ISO。
|
||||
如果你想使用原版的 Arch Linux,但又被它困难的安装所难倒。幸运的是,你可以下载一个带有图形安装程序的 Arch Linux ISO。
|
||||
|
||||
Arch 安装程序基本上是 Arch Linux ISO 的一个相对容易使用的基于文本的安装程序。它比原版的 Arch 安装容易得多。
|
||||
Arch 安装程序基本上是 Arch Linux ISO 的一个相对容易使用的基于文本的安装程序。它比裸奔的 Arch 安装容易得多。
|
||||
|
||||
#### Anarchy Installer
|
||||
|
||||
![][42]
|
||||
|
||||
[Anarchy installer][43] 打算为新手和有经验的 Linux 用户提供一种简单而无痛苦的方式来安装 ArchLinux。在需要的时候安装,在需要的地方安装,并且以您想要的方式安装。这就是 Anarchy 的哲学。
|
||||
[Anarchy installer][43] 打算为新手和有经验的 Linux 用户提供一种简单而无痛苦的方式来安装 ArchLinux。在需要的时候安装,在需要的地方安装,并且以你想要的方式安装。这就是 Anarchy 的哲学。
|
||||
|
||||
启动安装程序后,将显示一个简单的 [TUI menu][44],列出所有可用的安装程序选项。
|
||||
启动安装程序后,将显示一个简单的 [TUI 菜单][44],列出所有可用的安装程序选项。
|
||||
|
||||
#### Zen Installer
|
||||
|
||||
@ -132,13 +132,13 @@ Arch 安装程序基本上是 Arch Linux ISO 的一个相对容易使用的基
|
||||
|
||||
[Zen Installer][46] 为安装 Arch Linux 提供了一个完整的图形(点击式)环境。它支持安装多个桌面环境 、AUR 以及 Arch Linux 的所有功能和灵活性,并且易于图形化安装。
|
||||
|
||||
ISO 将引导实时环境,然后在您连接到 internet 后下载最新稳定版本的安装程序。因此,您将始终获得最新的安装程序和更新的功能。
|
||||
ISO 将引导一个临场环境,然后在你连接到互联网后下载最新稳定版本的安装程序。因此,你将始终获得最新的安装程序和更新的功能。
|
||||
|
||||
### 总结
|
||||
|
||||
对于许多用户来说,基于 Arch 的发行版会是一个很好的无障碍选择,但是像 Anarchy 这样的图形化安装程序至少离原版的 Arch Linux 更近了一步。
|
||||
对于许多用户来说,基于 Arch 的发行版会是一个很好的无忧选择,而像 Anarchy 这样的图形化安装程序至少离原版的 Arch Linux 更近了一步。
|
||||
|
||||
在我看来,[Arch Linux 的真正魅力在于它的安装过程 ][2] 对于 Linux 爱好者来说,这是一个学习的机会,而不是麻烦。Arch Linux 及其衍生产品有很多东西需要你去折腾,但是在折腾的过程中你就会进入到开源软件的世界,这里是神奇的新世界。下次上课见!
|
||||
在我看来,[Arch Linux 的真正魅力在于它的安装过程][2],对于 Linux 爱好者来说,这是一个学习的机会,而不是麻烦。Arch Linux 及其衍生产品有很多东西需要你去折腾,但是在折腾的过程中你就会进入到开源软件的世界,这里是神奇的新世界。下次再见!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -147,7 +147,7 @@ via: https://itsfoss.com/arch-based-linux-distros/
|
||||
作者:[Dimitrios Savvopoulos][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[Chao-zhi](https://github.com/Chao-zhi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
@ -0,0 +1,168 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (robsean)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-13106-1.html)
|
||||
[#]: subject: (How to Run a Shell Script in Linux [Essentials Explained for Beginners])
|
||||
[#]: via: (https://itsfoss.com/run-shell-script-linux/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
基础:如何在 Linux 中运行一个 Shell 脚本
|
||||
======
|
||||
|
||||

|
||||
|
||||
在 Linux 中有两种运行 shell 脚本的方法。你可以使用:
|
||||
|
||||
```
|
||||
bash script.sh
|
||||
```
|
||||
|
||||
或者,你可以像这样执行 shell 脚本:
|
||||
|
||||
```
|
||||
./script.sh
|
||||
```
|
||||
|
||||
这可能很简单,但没太多解释。不要担心,我将使用示例来进行必要的解释,以便你能理解为什么在运行一个 shell 脚本时要使用给定的特定语法格式。
|
||||
|
||||
我将使用这一行 shell 脚本来使需要解释的事情变地尽可能简单:
|
||||
|
||||
```
|
||||
abhishek@itsfoss:~/Scripts$ cat hello.sh
|
||||
|
||||
echo "Hello World!"
|
||||
```
|
||||
|
||||
### 方法 1:通过将文件作为参数传递给 shell 以运行 shell 脚本
|
||||
|
||||
第一种方法涉及将脚本文件的名称作为参数传递给 shell 。
|
||||
|
||||
考虑到 bash 是默认 shell,你可以像这样运行一个脚本:
|
||||
|
||||
```
|
||||
bash hello.sh
|
||||
```
|
||||
|
||||
你知道这种方法的优点吗?**你的脚本不需要执行权限**。对于简单的任务非常方便快速。
|
||||
|
||||
![在 Linux 中运行一个 Shell 脚本][1]
|
||||
|
||||
如果你还不熟悉,我建议你 [阅读我的 Linux 文件权限详细指南][2] 。
|
||||
|
||||
记住,将其作为参数传递的需要是一个 shell 脚本。一个 shell 脚本是由命令组成的。如果你使用一个普通的文本文件,它将会抱怨错误的命令。
|
||||
|
||||
![运行一个文本文件为脚本][3]
|
||||
|
||||
在这种方法中,**你要明确地具体指定你想使用 bash 作为脚本的解释器** 。
|
||||
|
||||
shell 只是一个程序,并且 bash 只是 Shell 的一种实现。还有其它的 shell 程序,像 ksh 、[zsh][4] 等等。如果你安装有其它的 shell ,你也可以使用它们来代替 bash 。
|
||||
|
||||
例如,我已安装了 zsh ,并使用它来运行相同的脚本:
|
||||
|
||||
![使用 Zsh 来执行 Shell 脚本][5]
|
||||
|
||||
### 方法 2:通过具体指定 shell 脚本的路径来执行脚本
|
||||
|
||||
另外一种运行一个 shell 脚本的方法是通过提供它的路径。但是要这样做之前,你的文件必须是可执行的。否则,当你尝试执行脚本时,你将会得到 “权限被拒绝” 的错误。
|
||||
|
||||
因此,你首先需要确保你的脚本有可执行权限。你可以 [使用 chmod 命令][8] 来给予你自己脚本的这种权限,像这样:
|
||||
|
||||
```
|
||||
chmod u+x script.sh
|
||||
```
|
||||
|
||||
使你的脚本是可执行之后,你只需输入文件的名称及其绝对路径或相对路径。大多数情况下,你都在同一个目录中,因此你可以像这样使用它:
|
||||
|
||||
```
|
||||
./script.sh
|
||||
```
|
||||
|
||||
如果你与你的脚本不在同一个目录中,你可以具体指定脚本的绝对路径或相对路径:
|
||||
|
||||
![在其它的目录中运行 Shell 脚本][9]
|
||||
|
||||
在脚本前的这个 `./` 是非常重要的(当你与脚本在同一个目录中)。
|
||||
|
||||
![][10]
|
||||
|
||||
为什么当你在同一个目录下,却不能使用脚本名称?这是因为你的 Linux 系统会在 `PATH` 环境变量中指定的几个目录中查找可执行的文件来运行。
|
||||
|
||||
这里是我的系统的 `PATH` 环境变量的值:
|
||||
|
||||
```
|
||||
abhishek@itsfoss:~$ echo $PATH
|
||||
/home/abhishek/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
|
||||
```
|
||||
|
||||
这意味着在下面目录中具有可执行权限的任意文件都可以在系统的任何位置运行:
|
||||
|
||||
* `/home/abhishek/.local/bin`
|
||||
* `/usr/local/sbin`
|
||||
* `/usr/local/bin`
|
||||
* `/usr/sbin`
|
||||
* `/usr/bin`
|
||||
* `/sbin`
|
||||
* `/bin`
|
||||
* `/usr/games`
|
||||
* `/usr/local/games`
|
||||
* `/snap/bin`
|
||||
|
||||
Linux 命令(像 `ls`、`cat` 等)的二进制文件或可执行文件都位于这些目录中的其中一个。这就是为什么你可以在你系统的任何位置通过使用命令的名称来运作这些命令的原因。看看,`ls` 命令就是位于 `/usr/bin` 目录中。
|
||||
|
||||
![][11]
|
||||
|
||||
当你使用脚本而不具体指定其绝对路径或相对路径时,系统将不能在 `PATH` 环境变量中找到提及的脚本。
|
||||
|
||||
> 为什么大多数 shell 脚本在其头部包含 #! /bin/bash ?
|
||||
>
|
||||
> 记得我提过 shell 只是一个程序,并且有 shell 程序的不同实现。
|
||||
>
|
||||
> 当你使用 `#! /bin/bash` 时,你是具体指定 bash 作为解释器来运行脚本。如果你不这样做,并且以 `./script.sh` 的方式运行一个脚本,它通常会在你正在运行的 shell 中运行。
|
||||
>
|
||||
> 有问题吗?可能会有。看看,大多数的 shell 语法是大多数种类的 shell 中通用的,但是有一些语法可能会有所不同。
|
||||
>
|
||||
> 例如,在 bash 和 zsh 中数组的行为是不同的。在 zsh 中,数组索引是从 1 开始的,而不是从 0 开始。
|
||||
>
|
||||
>![Bash Vs Zsh][12]
|
||||
>
|
||||
> 使用 `#! /bin/bash` 来标识该脚本是 bash 脚本,并且应该使用 bash 作为脚本的解释器来运行,而不受在系统上正在使用的 shell 的影响。如果你使用 zsh 的特殊语法,你可以通过在脚本的第一行添加 `#! /bin/zsh` 的方式来标识其是 zsh 脚本。
|
||||
>
|
||||
> 在 `#!` 和 `/bin/bash` 之间的空格是没有影响的。你也可以使用 `#!/bin/bash` 。
|
||||
|
||||
### 它有帮助吗?
|
||||
|
||||
我希望这篇文章能够增加你的 Linux 知识。如果你还有问题或建议,请留下评论。
|
||||
|
||||
专家用户可能依然会挑出我遗漏的东西。但这种初级题材的问题是,要找到信息的平衡点,避免细节过多或过少,并不容易。
|
||||
|
||||
如果你对学习 bash 脚本感兴趣,在我们专注于系统管理的网站 [Linux Handbook][14] 上,我们有一个 [完整的 Bash 初学者系列][13] 。如果你想要,你也可以 [购买带有附加练习的电子书][15] ,以支持 Linux Handbook。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/run-shell-script-linux/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[robsean](https://github.com/robsean)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?resize=741%2C329&ssl=1
|
||||
[2]: https://linuxhandbook.com/linux-file-permissions/
|
||||
[3]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?resize=741%2C329&ssl=1
|
||||
[4]: https://www.zsh.org
|
||||
[5]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?resize=741%2C253&ssl=1
|
||||
[6]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/09/run-multiple-commands-in-linux.png?fit=800%2C450&ssl=1
|
||||
[7]: https://itsfoss.com/run-multiple-commands-linux/
|
||||
[8]: https://linuxhandbook.com/chmod-command/
|
||||
[9]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?resize=795%2C272&ssl=1
|
||||
[10]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?resize=800%2C450&ssl=1
|
||||
[11]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?resize=795%2C272&ssl=1
|
||||
[12]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?resize=795%2C386&ssl=1
|
||||
[13]: https://linuxhandbook.com/tag/bash-beginner/
|
||||
[14]: https://linuxhandbook.com
|
||||
[15]: https://www.buymeacoffee.com/linuxhandbook
|
@ -0,0 +1,368 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Configure multi-tenancy with Kubernetes namespaces)
|
||||
[#]: via: (https://opensource.com/article/21/2/kubernetes-namespaces)
|
||||
[#]: author: (Mike Calizo https://opensource.com/users/mcalizo)
|
||||
|
||||
Configure multi-tenancy with Kubernetes namespaces
|
||||
======
|
||||
Namespaces provide basic building blocks of access control for
|
||||
applications, users, or groups of users.
|
||||
![shapes of people symbols][1]
|
||||
|
||||
Most enterprises want a multi-tenancy platform to run their cloud-native applications because it helps manage resources, costs, and operational efficiency and control [cloud waste][2].
|
||||
|
||||
[Kubernetes][3] is the leading open source platform for managing containerized workloads and services. It gained this reputation because of its flexibility in allowing operators and developers to establish automation with declarative configuration. But there is a catch: Because Kubernetes grows rapidly, the old problem of velocity becomes an issue. The bigger your adoption, the more issues and resource waste you discover.
|
||||
|
||||
### An example of scale
|
||||
|
||||
Imagine your company started small with its Kubernetes adoption by deploying a variety of internal applications. It has multiple project streams running with multiple developers dedicated to each project stream.
|
||||
|
||||
In a scenario like this, you need to make sure your cluster administrator has full control over the cluster to manage its resources and implement cluster policy and security standards. In a way, the admin is herding the cluster's users to use best practices. A namespace is very useful in this instance because it enables different teams to share a single cluster where computing resources are subdivided into multiple teams.
|
||||
|
||||
While namespaces are your first step to Kubernetes multi-tenancy, they are not good enough on their own. There are a number of Kubernetes primitives you need to consider so that you can administer your cluster properly and put it into a production-ready implementation.
|
||||
|
||||
The Kubernetes primitives for multi-tenancy are:
|
||||
|
||||
1. **RBAC:** Role-based access control for Kubernetes
|
||||
2. **Network policies:** To isolate traffic between namespaces
|
||||
3. **Resource quotas:** To control fair access to cluster resources
|
||||
|
||||
|
||||
|
||||
This article explores how to use Kubernetes namespaces and some basic RBAC configurations to partition a single Kubernetes cluster and take advantage of this built-in Kubernetes tooling.
|
||||
|
||||
### What is a Kubernetes namespace?
|
||||
|
||||
Before digging into how to use namespaces to prepare your Kubernetes cluster to become multi-tenant-ready, you need to know what namespaces are.
|
||||
|
||||
A [namespace][4] is a Kubernetes object that partitions a Kubernetes cluster into multiple virtual clusters. This is done with the aid of [Kubernetes names and IDs][5]. Namespaces use the Kubernetes name object, which means that each object inside a namespace gets a unique name and ID across the cluster to allow virtual partitioning.
|
||||
|
||||
### How namespaces help in multi-tenancy
|
||||
|
||||
Namespaces are one of the Kubernetes primitives you can use to partition your cluster into multiple virtual clusters to allow multi-tenancy. Each namespace is isolated from every other user's, team's, or application's namespace. This isolation is essential in multi-tenancy so that updates and changes in applications, users, and teams are contained within the specific namespace. (Note that namespace does not provide network segmentation.)
|
||||
|
||||
Before moving ahead, verify the default namespace in a working Kubernetes cluster:
|
||||
|
||||
|
||||
```
|
||||
[root@master ~]# kubectl get namespace
|
||||
NAME STATUS AGE
|
||||
default Active 3d
|
||||
kube-node-lease Active 3d
|
||||
kube-public Active 3d
|
||||
kube-system Active 3d
|
||||
```
|
||||
|
||||
Then create your first namespace, called **test**:
|
||||
|
||||
|
||||
```
|
||||
[root@master ~]# kubectl create namespace test
|
||||
namespace/test created
|
||||
```
|
||||
|
||||
Verify the newly created namespace:
|
||||
|
||||
|
||||
```
|
||||
[root@master ~]# kubectl get namespace
|
||||
NAME STATUS AGE
|
||||
default Active 3d
|
||||
kube-node-lease Active 3d
|
||||
kube-public Active 3d
|
||||
kube-system Active 3d
|
||||
test Active 10s
|
||||
[root@master ~]#
|
||||
```
|
||||
|
||||
Describe the newly created namespace:
|
||||
|
||||
|
||||
```
|
||||
[root@master ~]# kubectl describe namespace test
|
||||
Name: test
|
||||
Labels: <none>
|
||||
Annotations: <none>
|
||||
Status: Active
|
||||
No resource quota.
|
||||
No LimitRange resource.
|
||||
```
|
||||
|
||||
To delete a namespace:
|
||||
|
||||
|
||||
```
|
||||
[root@master ~]# kubectl delete namespace test
|
||||
namespace "test" deleted
|
||||
```
|
||||
|
||||
Your new namespace is active, but it doesn't have any labels, annotations, or quota-limit ranges defined. However, now that you know how to create and describe and delete a namespace, I'll show how you can use a namespace to virtually partition a Kubernetes cluster.
|
||||
|
||||
### Partitioning clusters using namespace and RBAC
|
||||
|
||||
Deploy the following simple application to learn how to partition a cluster using namespace and isolate an application and its related objects from "other" users.
|
||||
|
||||
First, verify the namespace you will use. For simplicity, use the **test** namespace you created above:
|
||||
|
||||
|
||||
```
|
||||
[root@master ~]# kubectl get namespaces
|
||||
NAME STATUS AGE
|
||||
default Active 3d
|
||||
kube-node-lease Active 3d
|
||||
kube-public Active 3d
|
||||
kube-system Active 3d
|
||||
test Active 3h
|
||||
```
|
||||
|
||||
Then deploy a simple application called **test-app** inside the test namespace by using the following configuration:
|
||||
|
||||
|
||||
```
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: test-app ⇒ name of the application
|
||||
namespace: test ⇒ the namespace where the app runs
|
||||
labels:
|
||||
app: test-app ⇒ labels for the app
|
||||
spec:
|
||||
containers:
|
||||
- name: test-app
|
||||
image: nginx:1.14.2 ⇒ the image we used for the app.
|
||||
ports:
|
||||
- containerPort: 80
|
||||
```
|
||||
|
||||
Deploy it:
|
||||
|
||||
|
||||
```
|
||||
$ kubectl create -f test-app.yaml
|
||||
pod/test-app created
|
||||
```
|
||||
|
||||
Then verify the application pod was created:
|
||||
|
||||
|
||||
```
|
||||
$ kubectl get pods -n test
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
test-app 1/1 Running 0 18s
|
||||
```
|
||||
|
||||
Now that the running application is inside the **test** namespace, test a use case where:
|
||||
|
||||
* **auth-user** can edit and view all the objects inside the test namespace
|
||||
* **un-auth-user** can only view the namespace
|
||||
|
||||
|
||||
|
||||
I pre-created the users for you to test. If you want to know how I created the users inside Kubernetes, view the commands [here][6].
|
||||
|
||||
|
||||
```
|
||||
$ kubectl config view -o jsonpath='{.users[*].name}'
|
||||
auth-user
|
||||
kubernetes-admin
|
||||
un-auth-user
|
||||
```
|
||||
|
||||
With this set up, create a Kubernetes [Role and RoleBindings][7] to isolate the target namespace **test** to allow **auth-user** to view and edit objects inside the namespace and not allow **un-auth-user** to access or view the objects inside the **test** namespace.
|
||||
|
||||
Start by creating a ClusterRole and a Role. These objects are a list of verbs (action) permitted on specific resources and namespaces.
|
||||
|
||||
Create a ClusterRole:
|
||||
|
||||
|
||||
```
|
||||
$ cat clusterrole.yaml
|
||||
apiVersion: rbac.authorization.k8s.io/v1beta1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: list-deployments
|
||||
namespace: test
|
||||
rules:
|
||||
- apiGroups: [ apps ]
|
||||
resources: [ deployments ]
|
||||
verbs: [ get, list ]
|
||||
```
|
||||
|
||||
Create a Role:
|
||||
|
||||
|
||||
```
|
||||
$ cat role.yaml
|
||||
apiVersion: rbac.authorization.k8s.io/v1beta1
|
||||
kind: Role
|
||||
metadata:
|
||||
name: list-deployments
|
||||
namespace: test
|
||||
rules:
|
||||
- apiGroups: [ apps ]
|
||||
resources: [ deployments ]
|
||||
verbs: [ get, list ]
|
||||
```
|
||||
|
||||
Apply the Role:
|
||||
|
||||
|
||||
```
|
||||
$ kubectl create -f role.yaml
|
||||
roles.rbac.authorization.k8s.io "list-deployments" created
|
||||
```
|
||||
|
||||
Use the same command to create a ClusterRole:
|
||||
|
||||
|
||||
```
|
||||
$ kubectl create -f clusterrole.yaml
|
||||
|
||||
$ kubectl get role -n test
|
||||
NAME CREATED AT
|
||||
list-deployments 2021-01-18T00:54:00Z
|
||||
```
|
||||
|
||||
Verify the Roles:
|
||||
|
||||
|
||||
```
|
||||
$ kubectl describe roles -n test
|
||||
Name: list-deployments
|
||||
Labels: <none>
|
||||
Annotations: <none>
|
||||
PolicyRule:
|
||||
Resources Non-Resource URLs Resource Names Verbs
|
||||
--------- ----------------- -------------- -----
|
||||
deployments.apps [] [] [get list]
|
||||
```
|
||||
|
||||
Remember that you must create RoleBindings by namespace, not by user. This means you need to create two role bindings for user **auth-user**.
|
||||
|
||||
Here are the sample RoleBinding YAML files to permit **auth-user** to edit and view.
|
||||
|
||||
**To edit:**
|
||||
|
||||
|
||||
```
|
||||
$ cat rolebinding-auth-edit.yaml
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: auth-user-edit
|
||||
namespace: test
|
||||
subjects:
|
||||
\- kind: User
|
||||
name: auth-user
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
roleRef:
|
||||
kind: ClusterRole
|
||||
name: edit
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
```
|
||||
|
||||
**To view:**
|
||||
|
||||
|
||||
```
|
||||
$ cat rolebinding-auth-view.yaml
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: auth-user-view
|
||||
namespace: test
|
||||
subjects:
|
||||
\- kind: User
|
||||
name: auth-user
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
roleRef:
|
||||
kind: ClusterRole
|
||||
name: view
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
```
|
||||
|
||||
Create these YAML files:
|
||||
|
||||
|
||||
```
|
||||
$ kubectl create rolebinding-auth-view.yaml
|
||||
$ kubectl create rolebinding-auth-edit.yaml
|
||||
```
|
||||
|
||||
Verify if the RoleBindings were successfully created:
|
||||
|
||||
|
||||
```
|
||||
$ kubectl get rolebindings -n test
|
||||
NAME ROLE AGE
|
||||
auth-user-edit ClusterRole/edit 48m
|
||||
auth-user-view ClusterRole/view 47m
|
||||
```
|
||||
|
||||
With the requirements set up, test the cluster partitioning:
|
||||
|
||||
|
||||
```
|
||||
[root@master]$ sudo su un-auth-user
|
||||
[un-auth-user@master ~]$ kubect get pods -n test
|
||||
[un-auth-user@master ~]$ kubectl get pods -n test
|
||||
Error from server (Forbidden): pods is forbidden: User "un-auth-user" cannot list resource "pods" in API group "" in the namespace "test"
|
||||
```
|
||||
|
||||
Log in as **auth-user**:
|
||||
|
||||
|
||||
```
|
||||
[root@master ]# sudo su auth-user
|
||||
[auth-user@master auth-user]$ kubectl get pods -n test
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
test-app 1/1 Running 0 3h8m
|
||||
[auth-user@master un-auth-user]$
|
||||
|
||||
[auth-user@master auth-user]$ kubectl edit pods/test-app -n test
|
||||
Edit cancelled, no changes made.
|
||||
```
|
||||
|
||||
You can view and edit the objects inside the **test** namespace. How about viewing the cluster nodes?
|
||||
|
||||
|
||||
```
|
||||
[auth-user@master auth-user]$ kubectl get nodes
|
||||
Error from server (Forbidden): nodes is forbidden: User "auth-user" cannot list resource "nodes" in API group "" at the cluster scope
|
||||
[auth-user@master auth-user]$
|
||||
```
|
||||
|
||||
You can't because the role bindings for user **auth-user** dictate they have access to view or edit objects only inside the **test** namespace.
|
||||
|
||||
### Enable access control with namespaces
|
||||
|
||||
Namespaces provide basic building blocks of access control using RBAC and isolation for applications, users, or groups of users. But using namespaces alone as your multi-tenancy solution is not enough in an enterprise implementation. It is recommended that you use other Kubernetes multi-tenancy primitives to attain further isolation and implement proper security.
|
||||
|
||||
Namespaces can provide some basic isolation in your Kubernetes cluster; therefore, it is important to consider them upfront, especially when planning a multi-tenant cluster. Namespaces also allow you to logically segregate and assign resources to individual users, teams, or applications.
|
||||
|
||||
By using namespaces, you can increase resource efficiencies by enabling a single cluster to be used for a diverse set of workloads.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/2/kubernetes-namespaces
|
||||
|
||||
作者:[Mike Calizo][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/mcalizo
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/Open%20Pharma.png?itok=GP7zqNZE (shapes of people symbols)
|
||||
[2]: https://devops.com/the-cloud-is-booming-but-so-is-cloud-waste/
|
||||
[3]: https://opensource.com/resources/what-is-kubernetes
|
||||
[4]: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
|
||||
[5]: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/
|
||||
[6]: https://www.adaltas.com/en/2019/08/07/users-rbac-kubernetes/
|
||||
[7]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
|
@ -0,0 +1,370 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Draw Mandelbrot fractals with GIMP scripting)
|
||||
[#]: via: (https://opensource.com/article/21/2/gimp-mandelbrot)
|
||||
[#]: author: (Cristiano L. Fontana https://opensource.com/users/cristianofontana)
|
||||
|
||||
Draw Mandelbrot fractals with GIMP scripting
|
||||
======
|
||||
Create complex mathematical images with GIMP's Script-Fu language.
|
||||
![Painting art on a computer screen][1]
|
||||
|
||||
The GNU Image Manipulation Program ([GIMP][2]) is my go-to solution for image editing. Its toolset is very powerful and convenient, except for doing [fractals][3], which is one thing you cannot draw by hand easily. These are fascinating mathematical constructs that have the characteristic of being [self-similar][4]. In other words, if they are magnified in some areas, they will look remarkably similar to the unmagnified picture. Besides being interesting, they also make very pretty pictures!
|
||||
|
||||
![Portion of a Mandelbrot fractal using GIMPs Coldfire palette][5]
|
||||
|
||||
Portion of a Mandelbrot fractal using GIMP's Coldfire palette (Cristiano Fontana, [CC BY-SA 4.0][6])
|
||||
|
||||
GIMP can be automated with [Script-Fu][7] to do [batch processing of images][8] or create complicated procedures that are not practical to do by hand; drawing fractals falls in the latter category. This tutorial will show how to draw a representation of the [Mandelbrot fractal][9] using GIMP and Script-Fu.
|
||||
|
||||
![Mandelbrot set drawn using GIMP's Firecode palette][10]
|
||||
|
||||
Portion of a Mandelbrot fractal using GIMP's Firecode palette. (Cristiano Fontana, [CC BY-SA 4.0][6])
|
||||
|
||||
![Rotated and magnified portion of the Mandelbrot set using Firecode.][11]
|
||||
|
||||
Rotated and magnified portion of the Mandelbrot set using the Firecode palette. (Cristiano Fontana, [CC BY-SA 4.0][6])
|
||||
|
||||
In this tutorial, you will write a script that creates a layer in an image and draws a representation of the Mandelbrot set with a colored environment around it.
|
||||
|
||||
### What is the Mandelbrot set?
|
||||
|
||||
Do not panic! I will not go into too much detail here. For the more math-savvy, the Mandelbrot set is defined as the set of [complex numbers][12] _a_ for which the succession
|
||||
|
||||
_zn+1 = zn2 + a_
|
||||
|
||||
does not diverge when starting from _z₀ = 0_.
|
||||
|
||||
In reality, the Mandelbrot set is the fancy-looking black blob in the pictures; the nice-looking colors are outside the set. They represent how many iterations are required for the magnitude of the succession of numbers to pass a threshold value. In other words, the color scale shows how many steps are required for the succession to pass an upper-limit value.
|
||||
|
||||
### GIMP's Script-Fu
|
||||
|
||||
[Script-Fu][7] is the scripting language built into GIMP. It is an implementation of the [Scheme programming language][13].
|
||||
|
||||
If you want to get more acquainted with Scheme, GIMP's documentation offers an [in-depth tutorial][14]. I also wrote an article about [batch processing images][8] using Script-Fu. Finally, the Help menu offers a Procedure Browser with very extensive documentation with all of Script-Fu's functions described in detail.
|
||||
|
||||
![GIMP Procedure Browser][15]
|
||||
|
||||
(Cristiano Fontana, [CC BY-SA 4.0][6])
|
||||
|
||||
Scheme is a Lisp-like language, so a major characteristic is that it uses a [prefix notation][16] and a [lot of parentheses][17]. Functions and operators are applied to a list of operands by prefixing them:
|
||||
|
||||
|
||||
```
|
||||
(function-name operand operand ...)
|
||||
|
||||
(+ 2 3)
|
||||
↳ Returns 5
|
||||
|
||||
(list 1 2 3 5)
|
||||
↳ Returns a list containing 1, 2, 3, and 5
|
||||
```
|
||||
|
||||
### Write the script
|
||||
|
||||
You can write your first script and save it to the **Scripts** folder found in the preferences window under **Folders → Scripts**. Mine is at `$HOME/.config/GIMP/2.10/scripts`. Write a file called `mandelbrot.scm` with:
|
||||
|
||||
|
||||
```
|
||||
; Complex numbers implementation
|
||||
(define (make-rectangular x y) (cons x y))
|
||||
(define (real-part z) (car z))
|
||||
(define (imag-part z) (cdr z))
|
||||
|
||||
(define (magnitude z)
|
||||
(let ((x (real-part z))
|
||||
(y (imag-part z)))
|
||||
(sqrt (+ (* x x) (* y y)))))
|
||||
|
||||
(define (add-c a b)
|
||||
(make-rectangular (+ (real-part a) (real-part b))
|
||||
(+ (imag-part a) (imag-part b))))
|
||||
|
||||
(define (mul-c a b)
|
||||
(let ((ax (real-part a))
|
||||
(ay (imag-part a))
|
||||
(bx (real-part b))
|
||||
(by (imag-part b)))
|
||||
(make-rectangular (- (* ax bx) (* ay by))
|
||||
(+ (* ax by) (* ay bx)))))
|
||||
|
||||
; Definition of the function creating the layer and drawing the fractal
|
||||
(define (script-fu-mandelbrot image palette-name threshold domain-width domain-height offset-x offset-y)
|
||||
(define num-colors (car (gimp-palette-get-info palette-name)))
|
||||
(define colors (cadr (gimp-palette-get-colors palette-name)))
|
||||
|
||||
(define width (car (gimp-image-width image)))
|
||||
(define height (car (gimp-image-height image)))
|
||||
|
||||
(define new-layer (car (gimp-layer-new image
|
||||
width height
|
||||
RGB-IMAGE
|
||||
"Mandelbrot layer"
|
||||
100
|
||||
LAYER-MODE-NORMAL)))
|
||||
|
||||
(gimp-image-add-layer image new-layer 0)
|
||||
(define drawable new-layer)
|
||||
(define bytes-per-pixel (car (gimp-drawable-bpp drawable)))
|
||||
|
||||
; Fractal drawing section.
|
||||
; Code from: <https://rosettacode.org/wiki/Mandelbrot\_set\#Racket>
|
||||
(define (iterations a z i)
|
||||
(let ((z′ (add-c (mul-c z z) a)))
|
||||
(if (or (= i num-colors) (> (magnitude z′) threshold))
|
||||
i
|
||||
(iterations a z′ (+ i 1)))))
|
||||
|
||||
(define (iter->color i)
|
||||
(if (>= i num-colors)
|
||||
(list->vector '(0 0 0))
|
||||
(list->vector (vector-ref colors i))))
|
||||
|
||||
(define z0 (make-rectangular 0 0))
|
||||
|
||||
(define (loop x end-x y end-y)
|
||||
(let* ((real-x (- (* domain-width (/ x width)) offset-x))
|
||||
(real-y (- (* domain-height (/ y height)) offset-y))
|
||||
(a (make-rectangular real-x real-y))
|
||||
(i (iterations a z0 0))
|
||||
(color (iter->color i)))
|
||||
(cond ((and (< x end-x) (< y end-y)) (gimp-drawable-set-pixel drawable x y bytes-per-pixel color)
|
||||
(loop (+ x 1) end-x y end-y))
|
||||
((and (>= x end-x) (< y end-y)) (gimp-progress-update (/ y end-y))
|
||||
(loop 0 end-x (+ y 1) end-y)))))
|
||||
(loop 0 width 0 height)
|
||||
|
||||
; These functions refresh the GIMP UI, otherwise the modified pixels would be evident
|
||||
(gimp-drawable-update drawable 0 0 width height)
|
||||
(gimp-displays-flush)
|
||||
)
|
||||
|
||||
(script-fu-register
|
||||
"script-fu-mandelbrot" ; Function name
|
||||
"Create a Mandelbrot layer" ; Menu label
|
||||
; Description
|
||||
"Draws a Mandelbrot fractal on a new layer. For the coloring it uses the palette identified by the name provided as a string. The image boundaries are defined by its domain width and height, which correspond to the image width and height respectively. Finally the image is offset in order to center the desired feature."
|
||||
"Cristiano Fontana" ; Author
|
||||
"2021, C.Fontana. GNU GPL v. 3" ; Copyright
|
||||
"27th Jan. 2021" ; Creation date
|
||||
"RGB" ; Image type that the script works on
|
||||
;Parameter Displayed Default
|
||||
;type label values
|
||||
SF-IMAGE "Image" 0
|
||||
SF-STRING "Color palette name" "Firecode"
|
||||
SF-ADJUSTMENT "Threshold value" '(4 0 10 0.01 0.1 2 0)
|
||||
SF-ADJUSTMENT "Domain width" '(3 0 10 0.1 1 4 0)
|
||||
SF-ADJUSTMENT "Domain height" '(3 0 10 0.1 1 4 0)
|
||||
SF-ADJUSTMENT "X offset" '(2.25 -20 20 0.1 1 4 0)
|
||||
SF-ADJUSTMENT "Y offset" '(1.50 -20 20 0.1 1 4 0)
|
||||
)
|
||||
(script-fu-menu-register "script-fu-mandelbrot" "<Image>/Layer/")
|
||||
```
|
||||
|
||||
I will go through the script to show you what it does.
|
||||
|
||||
### Get ready to draw the fractal
|
||||
|
||||
Since this image is all about complex numbers, I wrote a quick and dirty implementation of complex numbers in Script-Fu. I defined the complex numbers as [pairs][18] of real numbers. Then I added the few functions needed for the script. I used [Racket's documentation][19] as inspiration for function names and roles:
|
||||
|
||||
|
||||
```
|
||||
(define (make-rectangular x y) (cons x y))
|
||||
(define (real-part z) (car z))
|
||||
(define (imag-part z) (cdr z))
|
||||
|
||||
(define (magnitude z)
|
||||
(let ((x (real-part z))
|
||||
(y (imag-part z)))
|
||||
(sqrt (+ (* x x) (* y y)))))
|
||||
|
||||
(define (add-c a b)
|
||||
(make-rectangular (+ (real-part a) (real-part b))
|
||||
(+ (imag-part a) (imag-part b))))
|
||||
|
||||
(define (mul-c a b)
|
||||
(let ((ax (real-part a))
|
||||
(ay (imag-part a))
|
||||
(bx (real-part b))
|
||||
(by (imag-part b)))
|
||||
(make-rectangular (- (* ax bx) (* ay by))
|
||||
(+ (* ax by) (* ay bx)))))
|
||||
```
|
||||
|
||||
### Draw the fractal
|
||||
|
||||
The new function is called `script-fu-mandelbrot`. The best practice for writing a new function is to call it `script-fu-something` so that it can be identified in the Procedure Browser easily. The function requires a few parameters: an `image` to which it will add a layer with the fractal, the `palette-name` identifying the color palette to be used, the `threshold` value to stop the iteration, the `domain-width` and `domain-height` that identify the image boundaries, and the `offset-x` and `offset-y` to center the image to the desired feature. The script also needs some other parameters that it can deduce from the GIMP interface:
|
||||
|
||||
|
||||
```
|
||||
(define (script-fu-mandelbrot image palette-name threshold domain-width domain-height offset-x offset-y)
|
||||
(define num-colors (car (gimp-palette-get-info palette-name)))
|
||||
(define colors (cadr (gimp-palette-get-colors palette-name)))
|
||||
|
||||
(define width (car (gimp-image-width image)))
|
||||
(define height (car (gimp-image-height image)))
|
||||
|
||||
...
|
||||
```
|
||||
|
||||
Then it creates a new layer and identifies it as the script's `drawable`. A "drawable" is the element you want to draw on:
|
||||
|
||||
|
||||
```
|
||||
(define new-layer (car (gimp-layer-new image
|
||||
width height
|
||||
RGB-IMAGE
|
||||
"Mandelbrot layer"
|
||||
100
|
||||
LAYER-MODE-NORMAL)))
|
||||
|
||||
(gimp-image-add-layer image new-layer 0)
|
||||
(define drawable new-layer)
|
||||
(define bytes-per-pixel (car (gimp-drawable-bpp drawable)))
|
||||
```
|
||||
|
||||
For the code determining the pixels' color, I used the [Racket][20] example on the [Rosetta Code][21] website. It is not the most optimized algorithm, but it is simple to understand. Even a non-mathematician like me can understand it. The `iterations` function determines how many steps the succession requires to pass the threshold value. To cap the iterations, I am using the number of colors in the palette. In other words, if the threshold is too high or the succession does not grow, the calculation stops at the `num-colors` value. The `iter->color` function transforms the number of iterations into a color using the provided palette. If the iteration number is equal to `num-colors`, it uses black because this means that the succession is probably bound and that pixel is in the Mandelbrot set:
|
||||
|
||||
|
||||
```
|
||||
; Fractal drawing section.
|
||||
; Code from: <https://rosettacode.org/wiki/Mandelbrot\_set\#Racket>
|
||||
(define (iterations a z i)
|
||||
(let ((z′ (add-c (mul-c z z) a)))
|
||||
(if (or (= i num-colors) (> (magnitude z′) threshold))
|
||||
i
|
||||
(iterations a z′ (+ i 1)))))
|
||||
|
||||
(define (iter->color i)
|
||||
(if (>= i num-colors)
|
||||
(list->vector '(0 0 0))
|
||||
(list->vector (vector-ref colors i))))
|
||||
```
|
||||
|
||||
Because I have the feeling that Scheme users do not like to use loops, I implemented the function looping over the pixels as a recursive function. The `loop` function reads the starting coordinates and their upper boundaries. At each pixel, it defines some temporary variables with the `let*` function: `real-x` and `real-y` are the real coordinates of the pixel in the complex plane, according to the parameters; the `a` variable is the starting point for the succession; the `i` is the number of iterations; and finally `color` is the pixel color. Each pixel is colored with the `gimp-drawable-set-pixel` function that is an internal GIMP procedure. The peculiarity is that it is not undoable, and it does not trigger the image to refresh. Therefore, the image will not be updated during the operation. To play nice with the user, at the end of each row of pixels, it calls the `gimp-progress-update` function, which updates a progress bar in the user interface:
|
||||
|
||||
|
||||
```
|
||||
(define z0 (make-rectangular 0 0))
|
||||
|
||||
(define (loop x end-x y end-y)
|
||||
(let* ((real-x (- (* domain-width (/ x width)) offset-x))
|
||||
(real-y (- (* domain-height (/ y height)) offset-y))
|
||||
(a (make-rectangular real-x real-y))
|
||||
(i (iterations a z0 0))
|
||||
(color (iter->color i)))
|
||||
(cond ((and (< x end-x) (< y end-y)) (gimp-drawable-set-pixel drawable x y bytes-per-pixel color)
|
||||
(loop (+ x 1) end-x y end-y))
|
||||
((and (>= x end-x) (< y end-y)) (gimp-progress-update (/ y end-y))
|
||||
(loop 0 end-x (+ y 1) end-y)))))
|
||||
(loop 0 width 0 height)
|
||||
```
|
||||
|
||||
At the calculation's end, the function needs to inform GIMP that it modified the `drawable`, and it should refresh the interface because the image is not "automagically" updated during the script's execution:
|
||||
|
||||
|
||||
```
|
||||
(gimp-drawable-update drawable 0 0 width height)
|
||||
(gimp-displays-flush)
|
||||
```
|
||||
|
||||
### Interact with the user interface
|
||||
|
||||
To use the `script-fu-mandelbrot` function in the graphical user interface (GUI), the script needs to inform GIMP. The `script-fu-register` function informs GIMP about the parameters required by the script and provides some documentation:
|
||||
|
||||
|
||||
```
|
||||
(script-fu-register
|
||||
"script-fu-mandelbrot" ; Function name
|
||||
"Create a Mandelbrot layer" ; Menu label
|
||||
; Description
|
||||
"Draws a Mandelbrot fractal on a new layer. For the coloring it uses the palette identified by the name provided as a string. The image boundaries are defined by its domain width and height, which correspond to the image width and height respectively. Finally the image is offset in order to center the desired feature."
|
||||
"Cristiano Fontana" ; Author
|
||||
"2021, C.Fontana. GNU GPL v. 3" ; Copyright
|
||||
"27th Jan. 2021" ; Creation date
|
||||
"RGB" ; Image type that the script works on
|
||||
;Parameter Displayed Default
|
||||
;type label values
|
||||
SF-IMAGE "Image" 0
|
||||
SF-STRING "Color palette name" "Firecode"
|
||||
SF-ADJUSTMENT "Threshold value" '(4 0 10 0.01 0.1 2 0)
|
||||
SF-ADJUSTMENT "Domain width" '(3 0 10 0.1 1 4 0)
|
||||
SF-ADJUSTMENT "Domain height" '(3 0 10 0.1 1 4 0)
|
||||
SF-ADJUSTMENT "X offset" '(2.25 -20 20 0.1 1 4 0)
|
||||
SF-ADJUSTMENT "Y offset" '(1.50 -20 20 0.1 1 4 0)
|
||||
)
|
||||
```
|
||||
|
||||
Then the script tells GIMP to put the new function in the Layer menu with the label "Create a Mandelbrot layer":
|
||||
|
||||
|
||||
```
|
||||
`(script-fu-menu-register "script-fu-mandelbrot" "<Image>/Layer/")`
|
||||
```
|
||||
|
||||
Having registered the function, you can visualize it in the Procedure Browser.
|
||||
|
||||
![script-fu-mandelbrot function][22]
|
||||
|
||||
(Cristiano Fontana, [CC BY-SA 4.0][6])
|
||||
|
||||
### Run the script
|
||||
|
||||
Now that the function is ready and registered, you can draw the Mandelbrot fractal! First, create a square image and run the script from the Layers menu.
|
||||
|
||||
![script running][23]
|
||||
|
||||
(Cristiano Fontana, [CC BY-SA 4.0][6])
|
||||
|
||||
The default values are a good starting set to obtain the following image. The first time you run the script, create a very small image (e.g., 60x60 pixels) because this implementation is slow! It took several hours for my computer to create the following image in full 1920x1920 pixels. As I mentioned earlier, this is not the most optimized algorithm; rather, it was the easiest for me to understand.
|
||||
|
||||
![Mandelbrot set drawn using GIMP's Firecode palette][10]
|
||||
|
||||
Portion of a Mandelbrot fractal using GIMP's Firecode palette. (Cristiano Fontana, [CC BY-SA 4.0][6])
|
||||
|
||||
### Learn more
|
||||
|
||||
This tutorial showed how to use GIMP's built-in scripting features to draw an image created with an algorithm. These images show GIMP's powerful set of tools that can be used for artistic applications and mathematical images.
|
||||
|
||||
If you want to move forward, I suggest you look at the official documentation and its [tutorial][14]. As an exercise, try modifying this script to draw a [Julia set][24], and please share the resulting image in the comments.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/2/gimp-mandelbrot
|
||||
|
||||
作者:[Cristiano L. Fontana][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/cristianofontana
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/painting_computer_screen_art_design_creative.png?itok=LVAeQx3_ (Painting art on a computer screen)
|
||||
[2]: https://www.gimp.org/
|
||||
[3]: https://en.wikipedia.org/wiki/Fractal
|
||||
[4]: https://en.wikipedia.org/wiki/Self-similarity
|
||||
[5]: https://opensource.com/sites/default/files/uploads/mandelbrot_portion.png (Portion of a Mandelbrot fractal using GIMPs Coldfire palette)
|
||||
[6]: https://creativecommons.org/licenses/by-sa/4.0/
|
||||
[7]: https://docs.gimp.org/en/gimp-concepts-script-fu.html
|
||||
[8]: https://opensource.com/article/21/1/gimp-scripting
|
||||
[9]: https://en.wikipedia.org/wiki/Mandelbrot_set
|
||||
[10]: https://opensource.com/sites/default/files/uploads/mandelbrot.png (Mandelbrot set drawn using GIMP's Firecode palette)
|
||||
[11]: https://opensource.com/sites/default/files/uploads/mandelbrot_portion2.png (Rotated and magnified portion of the Mandelbrot set using Firecode.)
|
||||
[12]: https://en.wikipedia.org/wiki/Complex_number
|
||||
[13]: https://en.wikipedia.org/wiki/Scheme_(programming_language)
|
||||
[14]: https://docs.gimp.org/en/gimp-using-script-fu-tutorial.html
|
||||
[15]: https://opensource.com/sites/default/files/uploads/procedure_browser_0.png (GIMP Procedure Browser)
|
||||
[16]: https://en.wikipedia.org/wiki/Polish_notation
|
||||
[17]: https://xkcd.com/297/
|
||||
[18]: https://www.gnu.org/software/guile/manual/html_node/Pairs.html
|
||||
[19]: https://docs.racket-lang.org/reference/generic-numbers.html?q=make-rectangular#%28part._.Complex_.Numbers%29
|
||||
[20]: https://racket-lang.org/
|
||||
[21]: https://rosettacode.org/wiki/Mandelbrot_set#Racket
|
||||
[22]: https://opensource.com/sites/default/files/uploads/mandelbrot_documentation.png (script-fu-mandelbrot function)
|
||||
[23]: https://opensource.com/sites/default/files/uploads/script_working.png (script running)
|
||||
[24]: https://en.wikipedia.org/wiki/Julia_set
|
@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (scvoet)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
@ -0,0 +1,86 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Manage your budget on Linux with this open source finance tool)
|
||||
[#]: via: (https://opensource.com/article/21/2/linux-skrooge)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
Manage your budget on Linux with this open source finance tool
|
||||
======
|
||||
Make managing your finances easier with Skrooge, an open source
|
||||
budgeting tool.
|
||||
![2 cents penny money currency][1]
|
||||
|
||||
In 2021, there are more reasons why people love Linux than ever before. In this series, I'll share 21 different reasons to use Linux. This article is about personal financial management.
|
||||
|
||||
Personal finances can be difficult to manage. It can be frustrating and even scary when you don't have enough money to get by without financial assistance, and it can be surprisingly overwhelming when you do have the money you need but no clear notion of where it all goes each month. To make matters worse, we're often told to "make a budget" as if declaring the amount of money you can spend each month will somehow manifest the money you need. The bottom line is that making a budget is hard, and not meeting your financial goals is discouraging. But it's still important, and Linux has several tools that can help make the task manageable.
|
||||
|
||||
### Money management
|
||||
|
||||
As with anything else in life, we all have our own ways of keeping track of our money. I used to take a simple and direct approach: My paycheck was deposited into an account, and I'd withdraw some percentage in cash. Once the cash was gone from my wallet, I had to wait until the next payday to spend anything. It only took one day of missing out on lunch to learn that I had to take my goals seriously, and I adjusted my spending behavior accordingly. For the simple lifestyle I had at the time, it was an effective means of keeping myself honest with my income, but it didn't translate well to online business transactions, long-term utility contracts, investments, and so on.
|
||||
|
||||
As I continue to refine the way I track my finances, I've learned that personal accounting is always an evolving process. We each have unique financial circumstances, which inform what kind of solution we can or should use to track our income and debt. If you're out of work, then your budgeting goal is likely to spend as little as possible. If you're working but paying off a student loan, then your goal probably favors sending money to the bank. And if you're working but planning for retirement, then you're probably trying to save as much as you can.
|
||||
|
||||
The thing to remember about a budget is that it's meant to compare your financial reality with your financial _goals_. You can't avoid some expenses, but after those, you get to set your own priorities. If you don't hit your goals, you can adjust your own behavior or rewrite your goals so that they better reflect reality. Adapting your financial plan doesn't mean you've failed. It just means that your initial projection wasn't accurate. During hard times, you may not be able to hit any budget goals, but if you keep up with your budget, you'll learn a lot about what it takes financially to maintain your current lifestyle (whatever it may be). Over time, you can learn to adjust settings you may never have realized were available to you. For instance, people are moving to rural towns for the lower cost of living now that remote work is a widely accepted option. It's pretty stunning to see how such a lifestyle shift can alter your budget reports.
|
||||
|
||||
The point is that budgeting is an often undervalued activity, and in no small part because it's daunting. It's important to realize that you can budget, no matter your level of expertise or interest in finances. Whether you [just use a LibreOffice spreadsheet][2], or try a dedicated financial application, you can set goals, track your own behavior, and learn a lot of valuable lessons that could eventually pay dividends.
|
||||
|
||||
### Open source accounting
|
||||
|
||||
There are several dedicated [personal finance applications for Linux][3], including [HomeBank][4], [Money Manager EX][5], [GNUCash][6], [KMyMoney][7], and [Skrooge][8]. All of these applications are essentially ledgers, a place you can retreat to at the end of each month (or whenever you look at your accounts), import data from your bank, and review how your expenditures align with whatever budget you've set for yourself.
|
||||
|
||||
![Skrooge interface with financial data displayed][9]
|
||||
|
||||
Skrooge
|
||||
|
||||
I use Skrooge as my personal budget tracker. It's an easy application to set up, even with multiple bank accounts. Skrooge, as with most open source finance apps, can import multiple file formats, so my workflow goes something like this:
|
||||
|
||||
1. Log in to my banks.
|
||||
2. Export the month's bank statement as QIF files.
|
||||
3. Open Skrooge.
|
||||
4. Import the QIF files. Each gets assigned to their appropriate accounts automatically.
|
||||
5. Review my expenditures compared to the budget goals I've set for myself. If I've gone over, then I dock next month's goals (so that I'll ideally spend less to make up the difference). If I've come in under my goal, then I move the excess to December's budget (so I'll have more to spend at the end of the year).
|
||||
|
||||
|
||||
|
||||
I only track a subset of the household budget in Skrooge. Skrooge makes that process easy through a dynamic database that allows me to categorize multiple transactions at once with custom tags. This makes it easy for me to extract my personal expenditures from general household and utility expenses, and I can leverage these categories when reviewing the autogenerated reports Skrooge provides.
|
||||
|
||||
![Skrooge budget pie chart][10]
|
||||
|
||||
Skrooge budget pie chart
|
||||
|
||||
Most importantly, the popular Linux financial apps allow me to manage my budget the way that works best for me. For instance, my partner prefers to use a LibreOffice spreadsheet, but with very little effort, I can extract a CSV file from the household budget, import it into Skrooge, and use an updated set of data. There's no lock-in, no incompatibility. The system is flexible and agile, allowing us to adapt our budget and our method of tracking expenses as we learn more about effective budgeting and about what life has in store.
|
||||
|
||||
### Open choice
|
||||
|
||||
Money markets worldwide differ, and the way we each interact with them also defines what tools we can use. Ultimately, your choice of what to use for your finances is a decision you must make based on your own requirements. And one thing open source does particularly well is provide its users the freedom of choice.
|
||||
|
||||
When setting my own financial goals, I appreciate that I can use whatever application fits in best with my style of personal computing. I get to retain control of how I process the data in my life, even when it's data I don't necessarily enjoy having to process. Linux and its amazing set of applications make it just a little less of a chore.
|
||||
|
||||
Try some financial apps on Linux and see if you can inspire yourself to set some goals and save money!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/2/linux-skrooge
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/Medical%20Costs%20Transparency_1.jpg?itok=CkZ_J88m (2 cents penny money currency)
|
||||
[2]: https://opensource.com/article/20/3/libreoffice-templates
|
||||
[3]: https://opensource.com/life/17/10/personal-finance-tools-linux
|
||||
[4]: http://homebank.free.fr/en/index.php
|
||||
[5]: https://www.moneymanagerex.org/download
|
||||
[6]: https://opensource.com/article/20/2/gnucash
|
||||
[7]: https://kmymoney.org/download.html
|
||||
[8]: https://apps.kde.org/en/skrooge
|
||||
[9]: https://opensource.com/sites/default/files/skrooge.jpg
|
||||
[10]: https://opensource.com/sites/default/files/skrooge-pie_0.jpg
|
@ -1,287 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( chensanle )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Tmux Command Examples To Manage Multiple Terminal Sessions)
|
||||
[#]: via: (https://www.ostechnix.com/tmux-command-examples-to-manage-multiple-terminal-sessions/)
|
||||
[#]: author: (sk https://www.ostechnix.com/author/sk/)
|
||||
|
||||
基于 Tmux 的多会话终端管理示例
|
||||
======
|
||||
|
||||
![tmux 命令示例][1]
|
||||
|
||||
我们已经了解到如何通过 [**GNU Screen**][2] 进行多会话管理。今天,我们将要领略另一个著名的命令行实用工具 **“Tmux”** 来管理会话。类似 GNU Screen,tmux 是一个帮助我们在单一终端窗口中创建多个会话,同一时间内同时运行多个应用程序或进程的终端复用工具。Tmux 免费、开源并且跨平台,支持 Linux、OpenBSD、FreeBSD、NetBSD 以及 Mac OS X。本文将讨论 Tmux 在 Linux 系统下的高频用法。
|
||||
|
||||
### Linux 下安装 Tmux
|
||||
|
||||
Tmux 可以在绝大多数的 Linux 官方仓库下获取。
|
||||
|
||||
在 Arch Linux 或它的变种系统下,执行下列命令来安装:
|
||||
|
||||
```
|
||||
$ sudo pacman -S tmux
|
||||
```
|
||||
|
||||
Debian、Ubuntu 或 Linux Mint:
|
||||
|
||||
```
|
||||
$ sudo apt-get install tmux
|
||||
```
|
||||
|
||||
Fedora:
|
||||
```
|
||||
$ sudo dnf install tmux
|
||||
```
|
||||
|
||||
RHEL 和 CentOS:
|
||||
```
|
||||
$ sudo yum install tmux
|
||||
```
|
||||
|
||||
SUSE/openSUSE:
|
||||
```
|
||||
$ sudo zypper install tmux
|
||||
```
|
||||
|
||||
以上,我们已经完成 Tmux 的安装。之后我们继续看看其他的 Tmux 示例。
|
||||
|
||||
### Tmux 命令示例: 多会话管理
|
||||
|
||||
Tmux 默认所有命令的前置命令都是 **Ctrl+b**,使用前牢记这个快捷键即可。
|
||||
* * *
|
||||
|
||||
**注意** **Screen** 的前置命令都是 **Ctrl+a**.
|
||||
|
||||
* * *
|
||||
|
||||
##### 创建 Tmux 会话
|
||||
|
||||
在终端中运行如下命令创建 Tmux 会话并附着进入:
|
||||
|
||||
```
|
||||
tmux
|
||||
```
|
||||
|
||||
抑或,
|
||||
|
||||
```
|
||||
tmux new
|
||||
```
|
||||
|
||||
Once you are inside the Tmux session, you will see a **green bar at the bottom** as shown in the screenshot below.
|
||||
一旦进入 Tmux 会话,你将看到一个 **绿色的栏目沉在底部** 如下图所示。
|
||||
|
||||
![][3]
|
||||
|
||||
创建 Tmux 会话
|
||||
|
||||
这个绿色的栏目能很容易提示,当前你是否身处 Tmux 会话当中。
|
||||
|
||||
##### 退出 Tmux 会话
|
||||
|
||||
退出当前 Tmux 会话仅需要使用 **Ctrl+b** 和 **d**。无需同时触发这两个快捷键,依次按下 “Ctrl+b” 和 “d” 即可。
|
||||
|
||||
退出当前会话后,你将能看到如下输出。
|
||||
```
|
||||
[detached (from session 0)]
|
||||
```
|
||||
|
||||
##### 创建有名会话
|
||||
|
||||
如果使用多个会话,你很可能会混淆运行在多个会话中的应用程序。这种情况下,我们需要会话并赋予名称。譬如需要 web 相关服务的会话,就创建一个名称为 **“webserver”**(或任意一个其他名称) 的 Tmux 会话。
|
||||
|
||||
```
|
||||
tmux new -s webserver
|
||||
```
|
||||
|
||||
这里是 Tmux 的有名会话
|
||||
|
||||
![][4]
|
||||
|
||||
拥有自定义名称的 Tmux 会话
|
||||
|
||||
当你能够看到上述截图,这个 Tmux 会话的名称已经被标注为 **webserver**。如此,你可以在多个会话中,轻易的区分应用程序的所在。
|
||||
|
||||
退出会话,轻按 **Ctrl+b** 和 **d**。
|
||||
|
||||
##### 查看 Tmux 会话清单
|
||||
|
||||
查看 Tmux 会话清单,执行:
|
||||
|
||||
```
|
||||
tmux ls
|
||||
```
|
||||
|
||||
示例输出:
|
||||
|
||||
![][5]
|
||||
|
||||
如你所见,我们开启了两个 Tmux 会话。
|
||||
|
||||
##### 创建非附着会话
|
||||
|
||||
有时候,你可能想要简单创建会话,但是并不想自动切入该会话。
|
||||
|
||||
创建一个非附着会话,并赋予名称 **“ostechnix”**,运行:
|
||||
|
||||
```
|
||||
tmux new -s ostechnix -d
|
||||
```
|
||||
|
||||
上述命令将会创建一个名为 “ostechnix” 的会话,但是并不会附着进入。
|
||||
|
||||
你可以通过使用 “tmux ls” 命令验证:
|
||||
|
||||
![][6]
|
||||
|
||||
##### 进入 Tmux 会话
|
||||
|
||||
通过如下命令,你可以进入最后一个被创建的会话:
|
||||
|
||||
```
|
||||
tmux attach
|
||||
```
|
||||
|
||||
Or,
|
||||
|
||||
```
|
||||
tmux a
|
||||
```
|
||||
|
||||
如果你想进入任意一个指定的有名会话,譬如 “ostechnix”,运行:
|
||||
|
||||
```
|
||||
tmux attach -t ostechnix
|
||||
```
|
||||
|
||||
或者,简写为:
|
||||
|
||||
```
|
||||
tmux a -t ostechnix
|
||||
```
|
||||
|
||||
##### 关闭 Tmux 会话
|
||||
|
||||
当你完成或者不再需要 Tmux 会话,你可以通过如下命令关闭:
|
||||
|
||||
```
|
||||
tmux kill-session -t ostechnix
|
||||
```
|
||||
|
||||
当身处该会话时,使用 **Ctrl+b** 以及 **x**。点击 “y” 来关闭会话
|
||||
|
||||
可以通过 “tmux ls” 命令验证。
|
||||
|
||||
关闭所有 Tmux 服务下的所有会话,运行:
|
||||
|
||||
```
|
||||
tmux kill-server
|
||||
```
|
||||
|
||||
谨慎!这将终止所有 Tmux 会话,并不会产生任何警告,即便会话存在运行中的任务。
|
||||
|
||||
如果不存在活跃的 Tmux 会话,将看到如下输出:
|
||||
|
||||
```
|
||||
$ tmux ls
|
||||
no server running on /tmp/tmux-1000/default
|
||||
```
|
||||
|
||||
##### 分离 Tmux 窗口
|
||||
|
||||
分割窗口成多个小窗口,在 Tmux 中,这个叫做 “Tmux panes”(Tmux 窗格)。每个窗格中可以同时运行多个不同的程序。每个窗格可以在不影响其他窗格的前提下可以调整大小、移动位置和控制关闭。我们可以水平、垂直或者二者混合的方式切割屏幕。
|
||||
|
||||
**水平切割窗格**
|
||||
|
||||
欲水平切割窗格,使用 **Ctrl+b** 和 **“**(半个双引号)。
|
||||
|
||||
![][7]
|
||||
|
||||
使用组合键进一步切割面板。
|
||||
|
||||
**垂直切割窗格**
|
||||
|
||||
垂直切割面板,使用 **Ctrl+b** 和 **%**
|
||||
|
||||
![][8]
|
||||
|
||||
垂直切割 Tmux 窗格
|
||||
|
||||
**水平、垂直混合切割窗格**
|
||||
|
||||
我们也可以同时采用水平和垂直的方案切割窗格。看看如下截图:
|
||||
|
||||
![][9]
|
||||
|
||||
分割 Tmux 窗格
|
||||
|
||||
受限,我通过 **Ctrl+b “** 水平分割,之后通过 **Ctrl+b** 垂直分割下方的窗格。
|
||||
|
||||
如你所见,每个窗格下我运行了不同的程序。
|
||||
|
||||
**切换窗格**
|
||||
|
||||
通过 **Ctrl+b** 和 **方向键(上下左右)** 切换窗格。
|
||||
|
||||
**发送命令给所有窗格**
|
||||
|
||||
之前的案例中,我们在每个窗格中运行了三个不同命令。其实,也可以发送相同的命令给所有窗格。
|
||||
|
||||
为此,使用 **Ctrl+b** 然后键入如下命令,之后按压 ENTER:
|
||||
|
||||
```
|
||||
:setw synchronize-panes
|
||||
```
|
||||
|
||||
现在在任意窗格中键入任何命令。你讲看到相同命令影响了所有窗格。
|
||||
|
||||
**Swap panes**
|
||||
**交换窗格**
|
||||
|
||||
使用 **Ctrl+b** 和 **o** 交换窗格。
|
||||
|
||||
**展示窗格数量**
|
||||
|
||||
使用 **Ctrl+b** 和 **q** 展示窗格数量。
|
||||
|
||||
**终止窗格**
|
||||
|
||||
要关闭窗格,直接键入 **exit** 并且使用 ENTER 键。或者,按压 **Ctrl+b** 和 **x**。你会看到确认信息。按压 **”y“** 关闭窗格。
|
||||
|
||||
![][10]
|
||||
|
||||
关闭窗格。
|
||||
|
||||
这个阶段下,你已经获得了基本的 Tmux 技能来进行多会话管理,更多细节,参阅 man 页面。
|
||||
|
||||
```
|
||||
$ man tmux
|
||||
```
|
||||
|
||||
GNU Screen 和 Tmux 工具都能透过 SSH 很好的管理远程服务器。学习 Screen 和 Tmux 命令,像个行家一样,彻底通过这些工具管理远程服务器。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/tmux-command-examples-to-manage-multiple-terminal-sessions/
|
||||
|
||||
作者:[sk][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.ostechnix.com/author/sk/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.ostechnix.com/wp-content/uploads/2019/06/Tmux-720x340.png
|
||||
[2]: https://www.ostechnix.com/screen-command-examples-to-manage-multiple-terminal-sessions/
|
||||
[3]: https://www.ostechnix.com/wp-content/uploads/2019/06/Tmux-session.png
|
||||
[4]: https://www.ostechnix.com/wp-content/uploads/2019/06/Named-Tmux-session.png
|
||||
[5]: https://www.ostechnix.com/wp-content/uploads/2019/06/List-Tmux-sessions.png
|
||||
[6]: https://www.ostechnix.com/wp-content/uploads/2019/06/Create-detached-sessions.png
|
||||
[7]: https://www.ostechnix.com/wp-content/uploads/2019/06/Horizontal-split.png
|
||||
[8]: https://www.ostechnix.com/wp-content/uploads/2019/06/Vertical-split.png
|
||||
[9]: https://www.ostechnix.com/wp-content/uploads/2019/06/Split-Panes.png
|
||||
[10]: https://www.ostechnix.com/wp-content/uploads/2019/06/Kill-panes.png
|
@ -1,174 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (robsean)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to Run a Shell Script in Linux [Essentials Explained for Beginners])
|
||||
[#]: via: (https://itsfoss.com/run-shell-script-linux/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
如何在 Linux 中运行一个 Shell 脚本 [初学者必知]
|
||||
======
|
||||
|
||||
在 linux 中有两种运行 shell 脚本的方法。你可以使用:
|
||||
|
||||
```
|
||||
bash script.sh
|
||||
```
|
||||
|
||||
或者,你可以像这样执行 shell 脚本:
|
||||
|
||||
```
|
||||
./script.sh
|
||||
```
|
||||
|
||||
这可能很简单,它解释不了多少。不要担心,我将使用示例来进行必要的解释,以便你能理解为什么在运行一个 shell 脚本时要使用给定的特定语法格式。
|
||||
|
||||
我将使用这一行 shell 脚本来使需要解释的事情变地尽可能简单:
|
||||
|
||||
```
|
||||
abhishek@itsfoss:~/Scripts$ cat hello.sh
|
||||
|
||||
echo "Hello World!"
|
||||
```
|
||||
|
||||
### 方法 1: 通过将文件作为参数传递给 shell 以运行 shell 脚本
|
||||
|
||||
第一种方法涉及将脚本文件的名称作为参数传递给 shell 。
|
||||
|
||||
考虑到 bash 是默认脚本,你可以像这样运行一个脚本:
|
||||
|
||||
```
|
||||
bash hello.sh
|
||||
```
|
||||
|
||||
你知道这种方法的优点码?**你的脚本没有所需要执行权限**。非常方便快速和简单的任务。
|
||||
|
||||
![在 Linux 中运行一个 Shell 脚本][1]
|
||||
|
||||
如果你还不熟悉,我建议你 [阅读我的 Linux 文件权限详细指南][2] 。
|
||||
|
||||
记住,它需要成为一个 shell 脚本,以便你能将其作为参数传递。一个 shell 脚本是由命令组成的。如果你使用一个普通的文本文件,它将会抱怨错误的命令。
|
||||
|
||||
![运行一个文本文件为脚本][3]
|
||||
|
||||
在这种方法中,**你要明确地具体指定你想使用 bash 作为脚本的解释器** 。
|
||||
|
||||
Shell 只是一个程序,并且 bash 只是 Shell 的一个实施。这里有其它的 shell 程序,像 ksh ,[zsh][4] ,等等。如果你安装有其它的 shell ,你也可以使用它们来代替 bash 。
|
||||
|
||||
例如,我已安装了 zsh ,并使用它来运行相同的脚本:
|
||||
|
||||
![使用 Zsh 来执行 Shell 脚本][5]
|
||||
|
||||
**建议阅读:**
|
||||
|
||||
![][6]
|
||||
|
||||
#### [如何在 Linux 终端中一次运行多个 Linux 命令 [初学者必知提示]][7]
|
||||
|
||||
### 方法 2: 通过具体指定 shell 脚本的路径来执行脚本
|
||||
|
||||
另外一种运行一个 shell 脚本的方法是通过提供它的路径。但是为使这变地可能,你的文件必须是可执行的。否则,当你尝试执行脚本时,你将会得到 “拒绝访问” 错误。
|
||||
|
||||
因此,你首先需要确保你的脚本有可执行权限。你可以 [使用 chmod 命令][8] 来给予你自己脚本的这种权限,像这样:
|
||||
|
||||
```
|
||||
chmod u+x script.sh
|
||||
```
|
||||
|
||||
在你的脚本是可执行的后,你需要的全部工作是输入文件的名称及其绝对路径或相对路径。大多数情况下,你都在同一个目录中,因此你可以像这样使用它:
|
||||
|
||||
```
|
||||
./script.sh
|
||||
```
|
||||
|
||||
如果你不与你的脚本在同一个目录中,你可以具体指定脚本的绝对路径或相对路径:
|
||||
|
||||
![在其它的目录中运行 Shell 脚本][9]
|
||||
|
||||
#### 在脚本前的这个 ./ 是非常重要的。(当你与脚本在同一个目录中)
|
||||
|
||||
![][10]
|
||||
|
||||
为什么当你在同一个目录下,却不能使用脚本名称?这是因为你的 Linux 系统会在 PATH 变量中查找具体指定的几个选定目录中的可执行的文件来运行。
|
||||
|
||||
这里是我的系统的 PATH 变量的值:
|
||||
|
||||
```
|
||||
abhishek@itsfoss:~$ echo $PATH
|
||||
/home/abhishek/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
|
||||
```
|
||||
|
||||
这意味着在下面目录中具有可执行权限的任意文件都可以在系统的任何位置运行:
|
||||
|
||||
* /home/abhishek/.local/bin
|
||||
* /usr/local/sbin
|
||||
* /usr/local/bin
|
||||
* /usr/sbin
|
||||
* /usr/bin
|
||||
* /sbin
|
||||
* /bin
|
||||
* /usr/games
|
||||
* /usr/local/games
|
||||
* /snap/bin
|
||||
|
||||
|
||||
|
||||
Linux 命令(像 ls ,cat 等)的二进制文件或可执行文件都位于这些目录中的其中一个目录。这就是为什么你可以在你系统的任何位置通过使用命令的名称来运作这些命令的原因。看看,ls 命令就是位于 /usr/bin 目录中。
|
||||
|
||||
![][11]
|
||||
|
||||
当你使用脚本而不具体指定其绝对路径或相对路径时,系统将不能在 PATH 变量中找到涉及的脚本。
|
||||
|
||||
#### 为什么大多数 shell 脚本在其头部包含 #! /bin/bash ?
|
||||
|
||||
记得我提过 shell 只是一个程序,并且有不同实现的 shell 程序。
|
||||
|
||||
当你使用 #! /bin/bash 时,你是具体指定 bash 作为解释器来运行脚本。如果你不这样做,并且以 ./script.sh 的方式运行一个脚本,它通常会在你正在运行的 shell 中运行。
|
||||
|
||||
有问题吗?可能会有。看看,大多数的 shell 语法是大多数种类的 shell 中通用的,但是有一些语法可能会有所不同。
|
||||
|
||||
例如,在 bash和 zsh 中数组的行为是不同的。在 zsh 中,数组索引是从 1 开始的,而不是从 0 开始。
|
||||
|
||||
![Bash Vs Zsh][12]
|
||||
|
||||
使用 #! /bin/bash 标示该级别是 bash 脚本,并且应该使用bash 作为脚本的解释器来运行,而不受在系统上正在使用的 shell 的影响。如果你使用 zsh 的特殊语法,你可以通过在脚本的第一行添加 #! /bin/zsh 的方式来标示其是 zsh 脚本
|
||||
|
||||
在 #! 和 /bin/bash 之间的空格是没有影响的。你也可以使用 #!/bin/bash 。
|
||||
|
||||
### 它有帮助吗?
|
||||
|
||||
我希望这篇文章能够增加你的 Linux 知识。如果你还有问题或建议,请留下评论。
|
||||
|
||||
专家用户可能依然会挑出我丢失的东西。但是这类初学者话题的问题不容易找到信息和避免过多或过少的细节之间的平衡。
|
||||
|
||||
如果你对学习 bash 脚本感兴趣,在我们的以系统管理为中心的网站 [Linux Handbook][14] 上,我们有一个 [完整的 Bash 初学者系列][13] 。如果你想要,你也可以 [购买附加练习题的电子书][15] ,以支持 Linux Handbook。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/run-shell-script-linux/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[robsean](https://github.com/robsean)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?resize=741%2C329&ssl=1
|
||||
[2]: https://linuxhandbook.com/linux-file-permissions/
|
||||
[3]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?resize=741%2C329&ssl=1
|
||||
[4]: https://www.zsh.org
|
||||
[5]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?resize=741%2C253&ssl=1
|
||||
[6]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/09/run-multiple-commands-in-linux.png?fit=800%2C450&ssl=1
|
||||
[7]: https://itsfoss.com/run-multiple-commands-linux/
|
||||
[8]: https://linuxhandbook.com/chmod-command/
|
||||
[9]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?resize=795%2C272&ssl=1
|
||||
[10]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?resize=800%2C450&ssl=1
|
||||
[11]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?resize=795%2C272&ssl=1
|
||||
[12]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?resize=795%2C386&ssl=1
|
||||
[13]: https://linuxhandbook.com/tag/bash-beginner/
|
||||
[14]: https://linuxhandbook.com
|
||||
[15]: https://www.buymeacoffee.com/linuxhandbook
|
Loading…
Reference in New Issue
Block a user