mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
b6bfc99135
@ -1,8 +1,8 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-12959-1.html)
|
||||
[#]: subject: (9 things to do in your first 10 minutes on a Linux server)
|
||||
[#]: via: (https://opensource.com/article/20/12/linux-server)
|
||||
[#]: author: (Gaurav Kamathe https://opensource.com/users/gkamathe)
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
> 在将新配置的服务器投入工作之前,请确保你知道你正在使用什么。
|
||||
|
||||
![软件的部件、模块、容器][1] 。
|
||||
![](https://img.linux.net.cn/data/attachment/album/202012/27/112042zuz1lpudkypeppeu.jpg)
|
||||
|
||||
当我在 Linux 上测试软件时(这是我工作中的一个常规部分),我需要使用多台运行 Linux 的不同架构的服务器。我整备机器,安装所需的软件包,运行我的测试,收集结果,并将机器返回到仓库中,以便其他人可以使用它进行测试。
|
||||
|
||||
@ -33,7 +33,7 @@ uptime
|
||||
|
||||
### 2、有人登录在上面吗?
|
||||
|
||||
一旦我知道这台机器符合我的测试需求,我需要确保没有其他人同时登录该系统运行他们自己的测试。虽然可能性很小,但考虑到整备系统会帮我处理好这个问题,偶尔检查一下还是有好处的 —— 尤其是当我第一次登录服务器的时候。我还会检查是否有其他用户(除了 root)可以访问系统。
|
||||
一旦我知道这台机器符合我的测试需求,我需要确保没有其他人同时登录该系统运行他们自己的测试。虽然考虑到整备系统会帮我处理好这个问题,这种可能性很小,但偶尔检查一下还是有好处的 —— 尤其是当我第一次登录服务器的时候。我还会检查是否有其他用户(除了 root)可以访问系统。
|
||||
|
||||
使用下面的命令来查找这些信息。最后一条命令是查找 `/etc/passwd` 文件中具有 shell 访问权限的用户;它会跳过文件中没有 shell 访问权限或 shell 设置为 `nologin` 的其他服务:
|
||||
|
||||
@ -45,7 +45,7 @@ grep sh$ /etc/passwd
|
||||
|
||||
### 3、物理机还是虚拟机
|
||||
|
||||
现在我有了属于自己的机器,我需要确定它是一台物理机还是一台虚拟机(VM)。如果是我自己整备的这台机器,我可以确定有我要求的东西。但是,如果你使用的是一台不是你自己整备的机器,你应该检查该机器是物理机还是虚拟机。
|
||||
现在我有了属于自己的机器,我需要确定它是一台物理机还是一台虚拟机(VM)。如果是我自己整备的这台机器,我可以确定这是我要求的东西。但是,如果你使用的是一台不是你自己整备的机器,你应该检查该机器是物理机还是虚拟机。
|
||||
|
||||
使用以下命令来识别这些信息。如果是物理系统,你会看到供应商的名称(如 HP、IBM 等)以及服务器的品牌和型号;而在虚拟机中,你应该看到 KVM、VirtualBox 等,这取决于创建虚拟机时使用了什么虚拟化软件:
|
||||
|
||||
@ -104,7 +104,7 @@ systemctl
|
||||
|
||||
### 7、网络连接
|
||||
|
||||
如今的机器网络化程度很高,它们需要与网络上的其他机器或服务进行通信。我识别服务器上哪些端口是开放的,是否有任何从网络到测试机器的连接,是否启用了防火墙,如果启用了,是否屏蔽了任何端口,以及机器与哪些 DNS 服务器对话。
|
||||
如今的机器网络化程度很高,它们需要与网络上的其他机器或服务进行通信。我会识别服务器上哪些端口是开放的,是否有到测试机器的任何网络连接,是否启用了防火墙,如果启用了,是否屏蔽了任何端口,以及机器与哪些 DNS 服务器对话。
|
||||
|
||||
使用以下命令来识别网络服务相关信息。如果一个过时的命令不可用,请从 yum 存储库中安装它或使用等效的新命令:
|
||||
|
||||
@ -146,7 +146,7 @@ journalctl
|
||||
|
||||
### 接下来的步骤
|
||||
|
||||
虽然命令和实用程序会发生变化,但它们所显示的基本信息大致不变。在你专注于掌握哪些命令之前,你需要对你要寻找的信息以及它属于什么类别有一个更高层面的看法。
|
||||
虽然命令和实用程序会发生变化,但它们所显示的基本信息大致不变。在你专注于掌握哪些命令之前,你需要对你要寻找的信息以及它属于什么类别有一个宏观的看法。
|
||||
|
||||
由于 Linux 将大部分信息保存在文件中,这些命令基本上是从文件中读取信息,并以一种易于理解的方式呈现出来。下一步的好做法是找出每个命令用来获取信息显示的文件。一个提示:寻找这些信息的方法是 `strace` 命令。
|
||||
|
@ -1,18 +1,20 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (robsean)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-12958-1.html)
|
||||
[#]: subject: (How to Install RPM Files on Fedora Linux [Beginner’s Tutorial])
|
||||
[#]: via: (https://itsfoss.com/install-rpm-files-fedora/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
如何在 Fedora Linux 上安装 RPM 文件 [初学者教程]
|
||||
如何在 Fedora Linux 上安装 RPM 文件
|
||||
======
|
||||
|
||||
_**这篇初学者文章介绍如何在 Fedora 和 Red Hat Linux 上安装 RPM 软件包。它也随后向你展示如何移除这些 RPM 软件包。**_
|
||||
![](https://img.linux.net.cn/data/attachment/album/202012/26/182851bxi1lstdz13siuif.jpg)
|
||||
|
||||
当你开始使用 Red Hat 系的 Fedora Linux 时,你早晚会偶然发现 .rpm 文件。就像在 Windows 中的 .exe 文件,以及在 Ubuntu 和 Debian 中的 .deb 文件一样,一个 rpm 文件能够使你在 [Fedora][1] 上快速安装一个软件。
|
||||
> 这篇初学者文章介绍如何在 Fedora 和 Red Hat Linux 上安装 RPM 软件包。它也随后向你展示如何移除这些 RPM 软件包。
|
||||
|
||||
当你开始使用 Red Hat 系的 Fedora Linux 时,你早晚会偶然发现 .rpm 文件。就像在 Windows 中的 .exe 文件,以及在 Ubuntu 和 Debian 中的 .deb 文件一样,一个 rpm 文件能够使你在 [Fedora][1] 上快速安装一个软件。
|
||||
|
||||
你可以从软件中心中找到并安装大量的软件,特别是 [如果你在 Fedora 中启用附加的存储库的话][2]。但是有时你会在它们的网站上找到可用的 RPM 格式的软件包。
|
||||
|
||||
@ -22,15 +24,13 @@ _**这篇初学者文章介绍如何在 Fedora 和 Red Hat Linux 上安装 RPM
|
||||
|
||||
我将向你展示安装 RPM 文件的三个方法:
|
||||
|
||||
* [使用软件中心安装 RPM 文件][3] (GUI 方法)
|
||||
* [使用 DNF 命令安装 RPM 文件][4] (CLI 方法)
|
||||
* [使用 Yum 命令安装 RPM 文件][5] (Red Hat 的 CLI 方法)
|
||||
|
||||
|
||||
* [使用软件中心安装 RPM 文件][3](GUI 方法)
|
||||
* [使用 DNF 命令安装 RPM 文件][4](CLI 方法)
|
||||
* [使用 Yum 命令安装 RPM 文件][5](Red Hat 的 CLI 方法)
|
||||
|
||||
#### 方法 1: 使用软件中心
|
||||
|
||||
在 Fedora 中使用默认的软件中心是最简单的方法。它真地很简单。转到你下载的 .rpm 文件的文件夹位置。它通常在 Downloads 文件夹。
|
||||
在 Fedora 中使用默认的软件中心是最简单的方法。它真地很简单。转到你下载的 .rpm 文件的文件夹位置。它通常在 “Downloads” 文件夹。
|
||||
|
||||
只需要 **双击 RPM 文件,它将会在软件中心中打开**。
|
||||
|
||||
@ -38,7 +38,7 @@ _**这篇初学者文章介绍如何在 Fedora 和 Red Hat Linux 上安装 RPM
|
||||
|
||||
![或者双击或者右键并选择软件安装][6]
|
||||
|
||||
当它在软件中心打开时,你应该会看到安装选项。只需要点击安装按钮并在提示时输入你的账号密码。
|
||||
当它在软件中心打开时,你应该会看到“安装”选项。只需要点击“安装”按钮并在提示时输入你的账号密码。
|
||||
|
||||
![通过 Fedora 软件中心安装 RPM][7]
|
||||
|
||||
@ -46,9 +46,9 @@ _**这篇初学者文章介绍如何在 Fedora 和 Red Hat Linux 上安装 RPM
|
||||
|
||||
#### 方法 2: 使用 DNF 命令来安装 RPM 文件
|
||||
|
||||
这是命令行方法。Fedora 使用新的 DNF [软件包管理器][8] ,你也可以使用它来安装下载的 RPM 文件。
|
||||
这是命令行方法。Fedora 使用新的 `dnf` [软件包管理器][8] ,你也可以使用它来安装下载的 RPM 文件。
|
||||
|
||||
打开一个终端并切换到你下载 RPM 文件的目录下。你也可以通过到 RPM 文件的路径。像这样使用 DNF 命令:
|
||||
打开一个终端并切换到你下载 RPM 文件的目录下。你也可以通过到 RPM 文件的路径。像这样使用 `dnf` 命令:
|
||||
|
||||
```
|
||||
sudo dnf install rpm_file_name
|
||||
@ -60,9 +60,9 @@ sudo dnf install rpm_file_name
|
||||
|
||||
#### 方法 3: 在 Red Hat 中使用 Yum 命令安装 RPM 文件
|
||||
|
||||
不像 Fedora ,Red Hat 仍然使用很好的旧式的 Yum 软件包管理器。在这里你还不能找到 DNF 命令。
|
||||
不像 Fedora ,Red Hat 仍然使用很好的旧式的 Yum 软件包管理器。在这里你还不能找到 `dnf` 命令。
|
||||
|
||||
这个过程与 DNF 命令相同。转到 RPM 文件所在的目录或提供它的路径。
|
||||
这个过程与 `dnf` 命令相同。转到 RPM 文件所在的目录或提供它的路径。
|
||||
|
||||
```
|
||||
sudo yum install path_to_RPM_file
|
||||
@ -78,21 +78,21 @@ sudo yum install path_to_RPM_file
|
||||
|
||||
![移除 RPM 软件包][11]
|
||||
|
||||
或者,你可以使用带有 `remove` 选项的 DNF 或 YUM 命令。
|
||||
或者,你可以使用带有 `remove` 选项的 `dnf` 或 `yum` 命令。
|
||||
|
||||
使用 DNF ,使用这个命令:
|
||||
使用 `dnf` ,使用这个命令:
|
||||
|
||||
```
|
||||
sudo dnf remove rpm_package_name
|
||||
```
|
||||
|
||||
使用 Yum ,使用这个命令:
|
||||
使用 `yum` ,使用这个命令:
|
||||
|
||||
```
|
||||
sudo yum remove rpm_package_name
|
||||
```
|
||||
|
||||
你可能不记得准确的软件包名称,没有关系。你可以做的是输入软件包的前几个字母,然后敲击 tab 按键。这是假设你已经启用 tab 按键补全,通常是这样的。
|
||||
你可能不记得准确的软件包名称,没有关系。你可以做的是输入软件包的前几个字母,然后敲击 `tab` 按键。这是假设你已经启用 `tab` 按键补全,通常是这样的。
|
||||
|
||||
这就是你需要做的全部。相当简单,对吧?作为一个初学者,你可能会为这样一个简单的任务而挣扎,我希望像这样的快速教程会让你对 Fedora 更自信一些。
|
||||
|
||||
@ -103,7 +103,7 @@ via: https://itsfoss.com/install-rpm-files-fedora/
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[robsean](https://github.com/robsean)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
@ -1,113 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ()
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How the Linux kernel handles interrupts)
|
||||
[#]: via: (https://opensource.com/article/20/10/linux-kernel-interrupts)
|
||||
[#]: author: (Stephan Avenwedde https://opensource.com/users/hansic99)
|
||||
|
||||
How the Linux kernel handles interrupts
|
||||
======
|
||||
Interrupts are a crucial part of how computers process data.
|
||||
![Penguin driving a car with a yellow background][1]
|
||||
|
||||
Interrupts are an essential part of how modern CPUs work. For example, every time you press a key on the keyboard, the CPU is interrupted so that the PC can read user input from the keyboard. This happens so quickly that you don't notice any change or impairment in user experience.
|
||||
|
||||
Moreover, the keyboard is not the only component that can cause interrupts. In general, there are three types of events that can cause the CPU to interrupt: _Hardware interrupts_, _software interrupts_, and _exceptions_. Before getting into the different types of interrupts, I'll define some terms.
|
||||
|
||||
### Definitions
|
||||
|
||||
An interrupt request (**IRQ**) is requested by the programmable interrupt controller (**PIC**) with the aim of interrupting the CPU and executing the interrupt service routine (**ISR**). The ISR is a small program that processes certain data depending on the cause of the IRQ. Normal processing is interrupted until the ISR finishes.
|
||||
|
||||
In the past, IRQs were handled by a separate microchip—the PIC—and I/O devices were wired directly to the PIC. The PIC managed the various hardware IRQs and could talk directly to the CPU. When an IRQ occurred, the PIC wrote the data to the CPU and raised the interrupt request (**INTR**) pin.
|
||||
|
||||
Nowadays, IRQs are handled by an advanced programmable interrupt controller (**APIC**), which is part of the CPU. Each core has its own APIC.
|
||||
|
||||
### Types of interrupts
|
||||
|
||||
As I mentioned, interrupts can be separated into three types depending on their source:
|
||||
|
||||
#### Hardware interrupts
|
||||
|
||||
When a hardware device wants to tell the CPU that certain data is ready to process (e.g., a keyboard entry or when a packet arrives at the network interface), it sends an IRQ to signal the CPU that the data is available. This invokes a specific ISR that was registered by the device driver during the kernel's start.
|
||||
|
||||
#### Software interrupts
|
||||
|
||||
When you're playing a video, it is essential to synchronize the music and video playback so that the music's speed doesn't vary. This is accomplished through a software interrupt that is repetitively fired by a precise timer system (known as [jiffies][2]). This timer enables your music player to synchronize. A software interrupt can also be invoked by a special instruction to read or write data to a hardware device.
|
||||
|
||||
Software interrupts are also crucial when real-time capability is required (such as in industrial applications). You can find more information about this in the Linux Foundation's article _[Intro to real-time Linux for embedded developers][3]_.
|
||||
|
||||
#### Exceptions
|
||||
|
||||
Exceptions are the type of interrupt that you probably know about. When the CPU executes a command that would result in division by zero or a page fault, any additional execution is interrupted. In such a case, you will be informed about it by a pop-up window or by seeing **segmentation fault (core dumped)** in the console output. But not every exception is caused by a faulty instruction.
|
||||
|
||||
Exceptions can be further divided into _Faults_, _Traps_, and _Aborts_.
|
||||
|
||||
* **Faults:** Faults are an exception that the system can correct, e.g., when a process tries to access data from a memory page that was swapped to the hard drive. The requested address is within the process address space, and the access rights are correct. If the page is not present in RAM, an IRQ is raised and it starts the **page fault exception handler** to load the desired memory page into RAM. If the operation is successful, execution will continue.
|
||||
* **Traps:** Traps are mainly used for debugging. If you set a breakpoint in a program, you insert a special instruction that causes it to trigger a trap. A trap can trigger a context switch that allows your debugger to read and display values of local variables. Execution can continue afterward. Traps are also the default way to execute system calls (like killing a process).
|
||||
* **Aborts:** Aborts are caused by hardware failure or inconsistent values in system tables. An abort does not report the location of the instruction that causes the exception. These are the most critical interrupts. An abort invokes the system's **abort exception handler**, which terminates the process that caused it.
|
||||
|
||||
|
||||
|
||||
### Get hands-on
|
||||
|
||||
IRQs are ordered by priority in a vector on the APIC (0=highest priority). The first 32 interrupts (0–31) have a fixed sequence that is specified by the CPU. You can find an overview of them on [OsDev's Exceptions][4] page. Subsequent IRQs can be assigned differently. The interrupt descriptor table (**IDT**) contains the assignment between IRQ and ISR. Linux defines an IRQ vector from 0 to 256 for the assignment.
|
||||
|
||||
To print a list of registered interrupts on your system, open a console and type:
|
||||
|
||||
|
||||
```
|
||||
`cat /proc/interrupts`
|
||||
```
|
||||
|
||||
You should see something like this:
|
||||
|
||||
![Registered interrupts list][5]
|
||||
|
||||
Registered interrupts in kernel version 5.6.6 (Stephan Avenwedde, [CC BY-SA 4.0][6])
|
||||
|
||||
From left to right, the columns are: IRQ vector, interrupt count per CPU (`0 .. n`), the hardware source, the hardware source's channel information, and the name of the device that caused the IRQ.
|
||||
|
||||
On the bottom of the table, there are some non-numeric interrupts. They are the architecture-specific interrupts, like the local timer interrupt (**LOC**) on IRQ 236. Some of them are specified in the [Linux IRQ vector layout][7] in the Linux kernel source tree.
|
||||
|
||||
![Architecture-specific interrupts][8]
|
||||
|
||||
Architecture-specific interrupts (Stephan Avenwedde, [CC BY-SA 4.0][6])
|
||||
|
||||
To get a live view of this table, run:
|
||||
|
||||
|
||||
```
|
||||
`watch -n1 "cat /proc/interrupts"`
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
Proper IRQ handling is essential for the proper interaction of hardware, drivers, and software. Luckily, the Linux kernel does a really good job, and a normal PC user will hardly notice anything about the kernel's entire interrupt handling.
|
||||
|
||||
This can get very complicated, and this article gives only a brief overview of the topic. Good sources of information for a deeper dive into the subject are the _[Linux Inside][9]_ eBook (CC BY-NC-SA 4.0) and the [Linux Kernel Teaching][10] repository.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/10/linux-kernel-interrupts
|
||||
|
||||
作者:[Stephan Avenwedde][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[萌新阿岩](https://github.com/mengxinayan)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/hansic99
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/car-penguin-drive-linux-yellow.png?itok=twWGlYAc (Penguin driving a car with a yellow background)
|
||||
[2]: https://elinux.org/Kernel_Timer_Systems
|
||||
[3]: https://www.linuxfoundation.org/blog/2013/03/intro-to-real-time-linux-for-embedded-developers/
|
||||
[4]: https://wiki.osdev.org/Exceptions
|
||||
[5]: https://opensource.com/sites/default/files/uploads/proc_interrupts_1.png (Registered interrupts list)
|
||||
[6]: https://creativecommons.org/licenses/by-sa/4.0/
|
||||
[7]: https://github.com/torvalds/linux/blob/master/arch/x86/include/asm/irq_vectors.h
|
||||
[8]: https://opensource.com/sites/default/files/uploads/proc_interrupts_2.png (Architecture-specific interrupts)
|
||||
[9]: https://0xax.gitbooks.io/linux-insides/content/Interrupts/
|
||||
[10]: https://linux-kernel-labs.github.io/refs/heads/master/lectures/interrupts.html#
|
@ -1,126 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Learn Bash by writing an interactive game)
|
||||
[#]: via: (https://opensource.com/article/20/12/learn-bash)
|
||||
[#]: author: (Jim Hall https://opensource.com/users/jim-hall)
|
||||
|
||||
Learn Bash by writing an interactive game
|
||||
======
|
||||
Programming a simple game is a great way to practice a new language and
|
||||
compare it against others you know.
|
||||
![bash logo on green background][1]
|
||||
|
||||
Learning a new programming language can be fun. Whenever I try to learn a new one, I focus on defining variables, writing a statement, and evaluating expressions. Once I have a general understanding of those concepts, I can usually figure out the rest on my own. Most programming languages have some similarities, so once you know one programming language, learning the next one is a matter of figuring out the unique details and recognizing the differences in it.
|
||||
|
||||
To help me practice a new programming language, I like to write a few test programs. One sample program I often write is a simple "guess the number" program, where the computer picks a number between one and 100 and asks me to guess the number. The program loops until I guess correctly.
|
||||
|
||||
The "guess the number" program exercises several concepts in programming languages: how to assign values to variables, how to write statements, and how to perform conditional evaluation and loops. It's a great practical experiment for learning a new programming language.
|
||||
|
||||
### Guess the number in Bash
|
||||
|
||||
[Bash][2] is the standard shell for most Linux systems. Aside from providing a rich command-line user interface, Bash also supports a complete programming language in the form of _scripts_.
|
||||
|
||||
If you're not familiar with Bash, I recommend these introductions:
|
||||
|
||||
* [What is Bash?][3]
|
||||
* [Get started with Bash programming][4]
|
||||
* [Get started with Bash scripting for sysadmins][5]
|
||||
* [How to write functions in Bash][6]
|
||||
* [Read more about Bash][7]
|
||||
|
||||
|
||||
|
||||
You can explore Bash by writing a version of the "guess the number" game. Here is my implementation:
|
||||
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
number=$(( $RANDOM % 100 + 1 ))
|
||||
|
||||
echo "Guess a number between 1 and 100"
|
||||
|
||||
guess=0
|
||||
|
||||
while [ "0$guess" -ne $number ] ; do
|
||||
read guess
|
||||
[ "0$guess" -lt $number ] && echo "Too low"
|
||||
[ "0$guess" -gt $number ] && echo "Too high"
|
||||
done
|
||||
|
||||
echo "That's right!"
|
||||
exit 0
|
||||
```
|
||||
|
||||
### Breaking down the script
|
||||
|
||||
The first line in the script, `#!/bin/bash` tells Linux to run this script using the Bash shell. Every script starts with the `#!` character pair, which indicates this is a shell script. What immediately follows `#!` is the shell to run. In this case, `/bin/bash` is the Bash shell.
|
||||
|
||||
To assign a value to a variable, list the variable's name followed by the `=` sign. For example, the statement `guess=0` assigns a zero value to the `guess` variable.
|
||||
|
||||
You can also prompt the user to enter a value using the `read` statement. If you write `read guess`, Bash waits for the user to enter some text then stores that value in the `guess` variable.
|
||||
|
||||
To reference the value of a variable, use `$` before the variable name. So, having stored a value in the `guess` variable, you can retrieve it using `$guess`.
|
||||
|
||||
You can use whatever names you like for variables, but Bash reserves a few special variable names for itself. One special variable is `RANDOM`, which generates a very large random number every time you reference it.
|
||||
|
||||
If you want to perform an operation at the same time you store a value, you need to enclose the statement in special brackets. This tells Bash to execute that statement first, and the `=` stores the resulting value in the variable. To evaluate a mathematical expression, use `$(( ))` around your statement. The double parentheses indicate an _arithmetic expression_. In my example, `number=$(( $RANDOM % 100 + 1 ))` evaluates the expression `$RANDOM % 100 + 1` and then stores the value in the `number` variable.
|
||||
|
||||
Standard arithmetic operators such as `+` (plus), `-` (minus), `*` (multiply), `/` (divide), and `%` (modulo) apply.
|
||||
|
||||
That means the statement `number=$(( $RANDOM % 100 + 1 ))` generates a random number between one and 100. The modulo operator (`%`) returns the _remainder_ after dividing two numbers. In this case, Bash divides a random number by 100, leaving a remainder in the range zero to 99. By adding one to that value, you get a random number between one and 100.
|
||||
|
||||
Bash supports _conditional expressions_ and _flow control_ like loops. In the "guess the number" game, Bash continues looping as long as the value in `guess` is not equal to `number`. If the guess is less than the random number, Bash prints "Too low," and if the guess is greater than the number, Bash prints "Too high."
|
||||
|
||||
### How it works
|
||||
|
||||
Now that you've written your Bash script, you can run it to play the "guess the number" game. Continue guessing until you find the correct number:
|
||||
|
||||
|
||||
```
|
||||
Guess a number between 1 and 100
|
||||
50
|
||||
Too high
|
||||
30
|
||||
Too high
|
||||
20
|
||||
Too high
|
||||
10
|
||||
Too low
|
||||
15
|
||||
Too high
|
||||
13
|
||||
Too low
|
||||
14
|
||||
That's right!
|
||||
```
|
||||
|
||||
Every time you run the script, Bash will pick a different random number.
|
||||
|
||||
This "guess the number" game is a great introductory program when learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts and compare details in each language.
|
||||
|
||||
Do you have a favorite programming language? How would you write the "guess the number" game in it? Follow this article series to see examples of other programming languages that might interest you.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/12/learn-bash
|
||||
|
||||
作者:[Jim Hall][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/jim-hall
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bash_command_line.png?itok=k4z94W2U (bash logo on green background)
|
||||
[2]: https://en.wikipedia.org/wiki/Bash_(Unix_shell)
|
||||
[3]: https://opensource.com/resources/what-bash
|
||||
[4]: https://opensource.com/article/20/4/bash-programming-guide
|
||||
[5]: https://opensource.com/article/20/4/bash-sysadmins-ebook
|
||||
[6]: https://opensource.com/article/20/6/bash-functions
|
||||
[7]: https://opensource.com/tags/bash
|
@ -0,0 +1,92 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (10 open source news headlines of 2020)
|
||||
[#]: via: (https://opensource.com/article/20/12/open-source-news)
|
||||
[#]: author: (Jason Blais https://opensource.com/users/jasonblais)
|
||||
|
||||
10 open source news headlines of 2020
|
||||
======
|
||||
Take a look back at the open source news that made headlines in 2020.
|
||||
![Digital creative of a browser on the internet][1]
|
||||
|
||||
Throughout this past year, we've shared [top open source news][2] to keep everyone updated on what's happening in the world of open source. In case you missed any of the headlines, catch up on 10 of the open source news events that grabbed our readers' attention in 2020.
|
||||
|
||||
### Conferences successfully move to the virtual world
|
||||
|
||||
When COVID-19 was declared a pandemic in March, in-person conferences and events around the world came to a halt. Although many were canceled or postponed, others moved to [virtual formats with massive early success][3], reports Correspondent Alan Formy-Duval in his May news roundup. More than 80,000 people attended [Red Hat Summit][4] 2020 online in April, and [GitHub Satellite][5] saw 40,000 tune in from 178 countries. These were some of the biggest virtual conferences anywhere in 2020.
|
||||
|
||||
### Open source in the battle against COVID-19
|
||||
|
||||
As the world focused on controlling the spread of COVID-19, it is no surprise that open source technologies were critical in the effort. Scott Nesbitt shares how open data was used to "[create tracking dashboards and apps, [design] ventilators, and [develop] protective gear][6]." COVID tools, datasets, and research findings were also shared openly on GitHub for others to use and collaborate on.
|
||||
|
||||
### Java celebrates its 25th anniversary
|
||||
|
||||
In February, Tim Hildred helped us celebrate the exciting news about [Java's 25th anniversary][7]. Java was initially designed for interactive televisions with applets embedded in HTML pages, and it has since become the primary programming language for many enterprise applications, especially in financial services. Congratulations to the beloved Java language on reaching the quarter-century mark!
|
||||
|
||||
### GNOME launches a new contributor program
|
||||
|
||||
The GNOME Foundation and Endless partnered to launch a new [Community Engagement Challenge][8]. The objective was to bring "beginners into open source and [encourage] ongoing participation in open source communities," writes Ben Cotton. Of the applications submitted by the July deadline, 20 projects were [selected to continue][9] to the challenge's second phase. Five were invited to participate in the third phase, with the winner to be announced in April 2021.
|
||||
|
||||
### CERN uses open source platform Ceph for storage
|
||||
|
||||
Ceph is an open source software-defined storage platform. It implements object storage on a single distributed computer cluster and powers several research centers' projects, including [CERN's particle physics research][10], Tim Hildred reports. This continues CERN's push, which [began in 2019][11], to use open source software.
|
||||
|
||||
### CNCF takes charge of Red Hat's Operator Framework
|
||||
|
||||
The Cloud Native Computing Foundation (CNCF) hosts various projects, including Kubernetes, to advance container technology, and it runs the world's largest open source developer conferences. In 2020, [CNCF took on Red Hat's Operator Framework][12], an open source toolkit for managing Kubernetes-native applications in an automated and scalable way. "The increasing number of projects and code in the stewardship of neutral foundations like the CNCF is a testament to the power of collaboration," writes Tim Hildred.
|
||||
|
||||
### Two German states roll out Element for their education system
|
||||
|
||||
Correspondent Lauren Maffeo writes that the German states of Schleswig-Holstein and Hamburg [deployed Element][13], an open source messaging platform, to their education system with 500,000 users. It was an exciting leap for open source in the collaboration software market, which has been led by closed source applications like Slack, Zoom, and Microsoft Teams. Several open source collaboration projects, including [Element][14], [Mattermost][15], and [Jitsi][16], took big strides during the COVID-19 pandemic.
|
||||
|
||||
### Strapi announces general availability
|
||||
|
||||
Strapi, an open source, headless CMS for building powerful APIs, [announced general availability][17] five years after its first commit, hundreds of contributors participating, and more than a million downloads, shares Jeff Macharyas. Used by organizations from small businesses to Global 500 companies, Strapi is a testament to the staying power of open source projects backed by a community of users and contributors.
|
||||
|
||||
### Wireguard merged into the Linux Kernel
|
||||
|
||||
Wireguard, the open source communication protocol for virtual private networks (VPNs), was [merged into the Linux Kernel codebase][18] in January, says Correspondent Don Watkins. While the full effects are yet to be seen, this move has the potential to change the landscape of VPNs powered by open source.
|
||||
|
||||
### Mozilla Foundation creates a new subsidiary to host Thunderbird
|
||||
|
||||
Scott Nesbitt reports on a new Mozilla Foundation subsidiary, MZLA Technologies, which will be "the [new home of the Thunderbird project][19]." MZLA Technologies will help Mozilla offer products and services that would not be possible otherwise. Time will tell if Mozilla transfers other products to the new subsidiary.
|
||||
|
||||
### Top headlines in 2021?
|
||||
|
||||
What news do you anticipate open source software will make in 2021? Which companies, projects, or communities will we hear from the most? Let us know in the comments.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/12/open-source-news
|
||||
|
||||
作者:[Jason Blais][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/jasonblais
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)
|
||||
[2]: https://opensource.com/tags/news
|
||||
[3]: https://opensource.com/article/20/5/news-may-9
|
||||
[4]: https://www.redhat.com/en/summit/about
|
||||
[5]: https://githubsatellite.com/
|
||||
[6]: https://opensource.com/article/20/3/news-march-28
|
||||
[7]: https://opensource.com/article/20/2/linux-java-and-other-industry-news
|
||||
[8]: https://opensource.com/article/20/4/news-april-11
|
||||
[9]: https://www.gnome.org/challenge/winners/
|
||||
[10]: https://opensource.com/article/20/2/linux-desktop-cern-more-industry-trends
|
||||
[11]: https://www.zdnet.com/article/cern-leaves-microsoft-programs-behind-for-open-source-software/
|
||||
[12]: https://opensource.com/article/20/8/standardizing-kube-and-more-industry-trends
|
||||
[13]: https://opensource.com/article/20/8/news-aug-8
|
||||
[14]: https://element.io/
|
||||
[15]: https://mattermost.com/
|
||||
[16]: http://jitsi.org/
|
||||
[17]: https://opensource.com/article/20/6/news-june-8
|
||||
[18]: https://opensource.com/article/20/2/new-feb-15
|
||||
[19]: https://opensource.com/article/20/2/news-february-1
|
@ -0,0 +1,112 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Try GNU nano, a lightweight alternative to Vim)
|
||||
[#]: via: (https://opensource.com/article/20/12/gnu-nano)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
Try GNU nano, a lightweight alternative to Vim
|
||||
======
|
||||
Lightweight and straightforward, nano delivers a simple, intuitive
|
||||
editor with no extra fuss.
|
||||
![A pink typewriter][1]
|
||||
|
||||
Many Linux distributions bundle [Vim][2] as their default text editor. This appeals to many longtime Linux users, and those who don’t like it can change it promptly after install anyway. Vim is a funny editor, though, as it’s one of the few that opens to a mode that doesn’t permit text entry. That’s a puzzling choice for any user, and it’s confusing for a new one.
|
||||
|
||||
Thanks to GNU nano, there’s a common alternative to Vim for a lightweight terminal-based text editor, and it’s so easy to use—it has its most important commands listed at the bottom of its window.
|
||||
|
||||
![Black nano terminal with white text][3]
|
||||
|
||||
### Installing
|
||||
|
||||
On Linux and macOS, you probably already have GNU nano installed. You can verify with the `which` command:
|
||||
|
||||
|
||||
```
|
||||
$ which nano
|
||||
/bin/nano
|
||||
```
|
||||
|
||||
If you don’t have it installed, you can install it from your software repository, or you can [download its source code and compile it][4] yourself.
|
||||
|
||||
On Windows, you can [install GNU nano][5] using [Chocolatey][6].
|
||||
|
||||
### Launching nano
|
||||
|
||||
Launch nano from the terminal, either alone:
|
||||
|
||||
|
||||
```
|
||||
`$ nano`
|
||||
```
|
||||
|
||||
Or you can also open a specific file by following your command with a path to a file. If the file you name doesn’t already exist, it’s created:
|
||||
|
||||
|
||||
```
|
||||
`$ nano example.txt`
|
||||
```
|
||||
|
||||
### Using nano
|
||||
|
||||
Nano is, with just a little reading, pretty self-explanatory. When you launch it, nano opens to either an empty buffer or the file you opened. At the bottom of the screen, there’s a list of functions and their corresponding keyboard shortcuts. More functions are available by pressing **Ctrl+G** for Get Help.
|
||||
|
||||
Here are the most important application commands:
|
||||
|
||||
* **Ctrl+S** saves your work
|
||||
* **Ctrl+W** save as
|
||||
* **Ctrl+R** loads a file ("Read")
|
||||
* **Ctrl+X** quits, or exits
|
||||
* **Ctrl+G** get help
|
||||
|
||||
|
||||
|
||||
Here are the most common editing commands:
|
||||
|
||||
* **Alt+A** select ("mark") a region
|
||||
* **Ctrl+K** cut marked text
|
||||
* **Ctrl+U** paste ("uncut")
|
||||
* **Alt+F** undo
|
||||
* **Alt+E** redo
|
||||
|
||||
|
||||
|
||||
### Customizable
|
||||
|
||||
Nano isn’t as extensible as Emacs or Vim, but you can make some significant customizations in a file called `~/.nanorc`. In this file, you can set global preferences, including word wrap settings, color schemes, line numbering, and more. You can also create your own key bindings, so if you want to use **Ctrl+V** to paste instead of nano’s default **Ctrl+U**, you can change the binding assigned to the **paste** function:
|
||||
|
||||
|
||||
```
|
||||
`bind ^V paste all`
|
||||
```
|
||||
|
||||
You can get a list of all available functions in the [GNU nano documentation][7].
|
||||
|
||||
### Simple and effective
|
||||
|
||||
GNU nano is a no-nonsense, straightforward text editor. It’s easy to use and provides all the functionality you expect from a text editor. Try it out, and enjoy the simplicity of intuitive editing.
|
||||
|
||||
Vim offers great benefits to writers, regardless of whether they are technically minded or not.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/12/gnu-nano
|
||||
|
||||
作者:[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/osdc-docdish-typewriter-pink.png?itok=OXJBtyYf (A pink typewriter)
|
||||
[2]: https://opensource.com/article/20/12/vi-text-editor
|
||||
[3]: https://opensource.com/sites/default/files/uploads/nano-31_days-nano-opensource.png (Black nano terminal with white text)
|
||||
[4]: http://nano-editor.org
|
||||
[5]: https://opensource.com/article/20/12/%C2%A0https://chocolatey.org/packages/nano
|
||||
[6]: https://opensource.com/article/20/3/chocolatey
|
||||
[7]: https://www.nano-editor.org/dist/latest/nanorc.5.html
|
@ -0,0 +1,116 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ()
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How the Linux kernel handles interrupts)
|
||||
[#]: via: (https://opensource.com/article/20/10/linux-kernel-interrupts)
|
||||
[#]: author: (Stephan Avenwedde https://opensource.com/users/hansic99)
|
||||
|
||||
Linux 内核如何处理中断
|
||||
======
|
||||
|
||||
中断是计算机处理数据的关键部分。
|
||||
|
||||
![企鹅驾驶着黄色背景的汽车][1]
|
||||
|
||||
中断是现代CPU工作中必不可少的一部分。例如:当你每次在键盘上按下一个按键后,CPU会被中断以使得PC读取用户键盘的输入。这个过程发生得相当快以致于在使用体验上你不会感到任何变化或损害。
|
||||
|
||||
此外,键盘并不是导致中断的唯一元素。一般来说,有三种类型的事件会导致CPU发生中断:硬件中断、软件中断和异常。再具体介绍不同类型的中断前,我需要先定义一些术语。
|
||||
|
||||
### 定义
|
||||
|
||||
一个中断请求(**IRQ**)是被可编程的中断控制器(**PIC**)发起,其目的是为了中断CPU和执行中断服务程序(**ISR**)。中断服务程序(ISR)是一个小的程序来处理具体的数据,其具体的处理方式依赖于造成中断请求(IRQ)的原因。之前正在运行的进程在中断服务程序(ISR)运行结束前都会被中断。
|
||||
|
||||
在过去,中断请求由单独的芯片处理(中断控制器芯片),I/O 设备与中断控制器(PIC)直接相连。中断控制器(PIC)管理着多种硬件的中断请求并且可以直接与CPU通信。当一个中断请求(IRQ)产生后,中断控制器向CPU写入数据并且触发中断请求(**INTR**)引脚。
|
||||
|
||||
现如今,中断请求(IRQ)由CPU中的高级可编程中断控制器(APIC)部分来处理。每个核中都拥有属于自己的高级可编程中断控制器。
|
||||
|
||||
### 中断的类型
|
||||
|
||||
正如我前文中提到的,中断可以根据其来源分为三种类型。
|
||||
|
||||
#### 硬件中断
|
||||
|
||||
当一个硬件设备想要告诉CPU某一需要处理的数据已经准备好后(例如:当键盘被按下或者一个数据包到了网络接口处),它将会发送一个中断请求(IRQ)来告诉CPU数据是可用的。接下来会调用在内核启动时设备驱动注册的对应的中断服务程序(ISR)。
|
||||
|
||||
#### 软件中断
|
||||
|
||||
当你在播放一个视频时,音频和视频是同步播放是相当重要的,因此音频的播放速度不应该发生变化。这是由软件中断实现的,由精确的计时器系统(称为 [jiffies][2])重复发起的。这个计时器会使得你的音乐播放器同步。软件中断也可以被特殊的指令所调用来读取或写入数据到硬件设备。
|
||||
|
||||
当系统需要实时性时(例如在工业应用中),软件中断会变得重要。你可以在Linux基金会的文章中找到更多相关信息:[面向嵌入式开发者的实时Linux介绍][3]。
|
||||
|
||||
#### 异常
|
||||
|
||||
异常是你可能之前知道的中断类型。当CPU执行一些将会导致除以0或缺页错误的指令时,任何其他运行中的程序都会被中断。在这种情况下,你会被一个弹窗提醒,或在控制台输出中看到**段错误(核心已转存)**。但并不是所有异常都是由指令错误引起的。
|
||||
|
||||
异常可以进一步分为 _错误_、_陷阱_ 和 _中止_ ?
|
||||
|
||||
Exceptions can be further divided into _Faults_, _Traps_, and _Aborts_.
|
||||
|
||||
* **错误:** 错误是系统可以纠正的异常。例如当一个进程尝试访问某个已经被换出到硬盘的页时。当请求的地址在进程的地址空间中,并且满足访问权限时,如果页不在内存(RAM)中,将会产生一个中断请求(IRQ),并开始启用**缺页异常处理程序**把所需的页加载到内存中。如果操作成功执行,程序将继续运行。
|
||||
|
||||
* **陷阱:** 陷阱主要用在调试中。如果你在某个程序中设置了一个断点,你就插入了一条可以触发陷阱执行的指令。一个陷阱可以触发上下文切换来允许你的调试器读取和展示局部变量的值。之后程序可以继续运行。陷阱同样也是运行系统调用的方式(如杀死一个进程)
|
||||
|
||||
* **中止:** 中止是由系统表中的硬件错误或值不一致而导致的。一个中止不会报告造成异常的指令的所在位置。这是最严重的中断,一个中止将会调用系统的**中止异常处理程序**来结束造成异常的进程。
|
||||
|
||||
### 动手实践
|
||||
|
||||
中断请求按照高级可编程中断控制器(APIC)中的优先级高低排序(0是最高优先级)。前32个中断(0~31)是由CPU指定的固定序列。你可以在 [OsDev 异常][4] 页面找到关于它们的概述。接下来的中断请求可以不同的分配。中断描述表(**IDT**)中记录了中断请求(IRQ)和中断服务程序(ISR)的对应关系。Linux 中定义了从 0 到 256 的 IRQ 向量。
|
||||
|
||||
为了打印出在你的系统中已注册的中断,打开一个终端并输入:
|
||||
|
||||
```
|
||||
`cat /proc/interrupts`
|
||||
```
|
||||
|
||||
你应该会看到类似如下图的结果:
|
||||
|
||||
![注册的中断列表][5]
|
||||
|
||||
内核版本为5.6.6中注册的中断 (Stephan Avenwedde, [CC BY-SA 4.0][6])
|
||||
|
||||
从左到右各列的含义依次为:中断向量号、每个CPU(0~n)中断发生次数、硬件来源、硬件源通道信息、以及造成中断请求的设备名。
|
||||
|
||||
在表的末尾,有一些非数字的中断。它们是特定于体系结构的中断,如本地计时器中断(**LOC**)的中断请求(IRQ)号为236。其中一些在 Linux 内核源树中的[Linux IRQ 向量布局][7]中指定。
|
||||
|
||||
![特定于体系结构的中断][8]
|
||||
|
||||
特定于体系结构的中断 (Stephan Avenwedde, [CC BY-SA 4.0][6])
|
||||
|
||||
|
||||
如果要实时获取该表,请运行如下命令:
|
||||
|
||||
```
|
||||
`watch -n1 "cat /proc/interrupts"`
|
||||
```
|
||||
|
||||
### 总结
|
||||
|
||||
正确的中断请求(IRQ)处理对于硬件、驱动和软件的正常交互是必要的。幸运地是,Linux 内核很好地完成了它,一个PC的普通用户很难注意到内核整个中断处理的过程。
|
||||
|
||||
中断相当复杂,本文仅仅是一个关于中断的概述。如果想要深入了解该主题可以阅读 [Linux 内部电子书][9](CC BY-NC-SA 4.0)和 [Linux 内核教程][10] 仓库。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/10/linux-kernel-interrupts
|
||||
|
||||
作者:[Stephan Avenwedde][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[萌新阿岩](https://github.com/mengxinayan)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/hansic99
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/car-penguin-drive-linux-yellow.png?itok=twWGlYAc (Penguin driving a car with a yellow background)
|
||||
[2]: https://elinux.org/Kernel_Timer_Systems
|
||||
[3]: https://www.linuxfoundation.org/blog/2013/03/intro-to-real-time-linux-for-embedded-developers/
|
||||
[4]: https://wiki.osdev.org/Exceptions
|
||||
[5]: https://opensource.com/sites/default/files/uploads/proc_interrupts_1.png (Registered interrupts list)
|
||||
[6]: https://creativecommons.org/licenses/by-sa/4.0/
|
||||
[7]: https://github.com/torvalds/linux/blob/master/arch/x86/include/asm/irq_vectors.h
|
||||
[8]: https://opensource.com/sites/default/files/uploads/proc_interrupts_2.png (Architecture-specific interrupts)
|
||||
[9]: https://0xax.gitbooks.io/linux-insides/content/Interrupts/
|
||||
[10]: https://linux-kernel-labs.github.io/refs/heads/master/lectures/interrupts.html#
|
@ -0,0 +1,123 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Learn Bash by writing an interactive game)
|
||||
[#]: via: (https://opensource.com/article/20/12/learn-bash)
|
||||
[#]: author: (Jim Hall https://opensource.com/users/jim-hall)
|
||||
|
||||
通过编写互动游戏学习 Bash
|
||||
======
|
||||
|
||||
> 编程一个简单的游戏是练习一门新语言并与其他你掌握的语言进行比较的好方法。
|
||||
|
||||
![绿底 bash 标志][1]
|
||||
|
||||
学习一门新的编程语言是很有趣的。每当我尝试学习一门新的语言时,我都会专注于定义变量、编写语句和评估表达式。一旦我对这些概念有了大致的了解,我通常可以自己弄清楚其余的概念。大多数编程语言都有一些相似之处,所以一旦你了解了一种编程语言,学习下一种编程语言就是要弄清楚其独特的细节,认识到其中的差异。
|
||||
|
||||
为了帮助我练习一种新的编程语言,我喜欢写一些测试程序。我经常写的一个示例程序是一个简单的“猜数字”程序,电脑在 1 到 100 之间选一个数字,让我猜这个数字。程序会一直循环,直到我猜对为止。
|
||||
|
||||
“猜数字”程序锻炼了编程语言中的几个概念:如何给变量赋值,如何写语句,如何进行条件判断和循环。对于学习一门新的编程语言来说,这是一个很好的实践实验。
|
||||
|
||||
### 用 Bash 猜数字
|
||||
|
||||
[Bash][2] 是大多数 Linux 系统的标准 shell。除了提供丰富的命令行用户界面外,Bash 还以*脚本*的形式支持完整的编程语言。
|
||||
|
||||
如果你对 Bash 不熟悉,我推荐你看这些介绍:
|
||||
|
||||
* [什么是 Bash?][3]。
|
||||
* [开始使用 Bash 编程][4]
|
||||
* [系统管理员的 Bash 脚本入门][5]
|
||||
* [如何在 Bash 中编写函数][6]
|
||||
* [阅读更多关于 Bash 的信息][7]
|
||||
|
||||
你可以通过编写一个 Bash 版本的“猜数字”游戏来探索它。这是我的实现:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
number=$(( $RANDOM % 100 + 1 ))
|
||||
|
||||
echo "Guess a number between 1 and 100"
|
||||
|
||||
guess=0
|
||||
|
||||
while [ "0$guess" -ne $number ] ; do
|
||||
read guess
|
||||
[ "0$guess" -lt $number ] && echo "Too low"
|
||||
[ "0$guess" -gt $number ] && echo "Too high"
|
||||
done
|
||||
|
||||
echo "That's right!"
|
||||
exit 0
|
||||
```
|
||||
|
||||
### 拆解这个脚本
|
||||
|
||||
脚本中的第一行,`#!/bin/bash` 告诉 Linux 使用 Bash shell 来运行这个脚本。每个脚本都以 `#!` 字符对(LCTT 译注:释伴)开始,这表示它是一个 shell 脚本。紧跟在`#!` 后面的是要运行的 shell。在本例中,`/bin/bash` 是指 Bash shell。
|
||||
|
||||
要给一个变量赋值,在变量名后面列出 `=` 号。例如,语句 `guess=0` 给 `guess` 变量分配一个零值。
|
||||
|
||||
你也可以使用 `read` 语句提示用户输入一个值。如果你写了 `read guess` 语句,Bash 会等待用户输入一些文本,然后把这个值存储在 `guess` 变量中。
|
||||
|
||||
要引用一个变量的值,在变量名前使用 `$`。所以, 在 `guess` 变量中存储了一个值后, 你可以使用 `$guess` 来检索它。
|
||||
|
||||
你可以使用任何你喜欢的变量名称,但是 Bash 为自己保留了一些特殊的变量名称。一个特殊的变量是 `RANDOM`,每次引用它都会产生一个很大的随机数。
|
||||
|
||||
如果你想在存储一个值的同时执行一个操作,你需要用特殊的括号把语句括起来。这将告诉 Bash 先执行该语句,而 `=` 则将结果值存储在变量中。要评估一个数学表达式,使用 `$(())` 围在你的语句上。双括号表示一个*算术表达式*。在我的例子中,`number=$(( $RANDOM % 100 + 1 ))` 评估表达式 `$RANDOM % 100 + 1`,然后将值存储在 `number` 变量中。
|
||||
|
||||
标准的算术运算符,如 `+`(加)、`-`(减)、`*`(乘)、`/`(除)和 `%`(模)都适用。
|
||||
|
||||
这意味着语句 `number=$(( $RANDOM % 100 + 1 ))` 产生一个 1 到 100 之间的随机数。模数运算符(`%`)返回两个数相除后的余数。在这种情况下,Bash 将一个随机数除以 100,剩下的余数范围是 0 到 99,通过在这个值上加 1,你可以得到一个介于 1 和 100 之间的随机数。
|
||||
|
||||
Bash 支持像循环这样的*条件表达式*和*流程控制*。在“猜数字”的游戏中,只要 `guess` 中的值不等于 `number`,Bash 就会继续循环。如果猜的数小于随机数,Bash 就会打印“太低”,如果猜的数大于数字,Bash 就会打印“太高”。
|
||||
|
||||
### 它是如何工作的
|
||||
|
||||
现在你已经写好了你的 Bash 脚本,你可以运行它来玩“猜数字”游戏。一直猜,直到你找到正确的数字:
|
||||
|
||||
```
|
||||
Guess a number between 1 and 100
|
||||
50
|
||||
Too high
|
||||
30
|
||||
Too high
|
||||
20
|
||||
Too high
|
||||
10
|
||||
Too low
|
||||
15
|
||||
Too high
|
||||
13
|
||||
Too low
|
||||
14
|
||||
That's right!
|
||||
```
|
||||
|
||||
每次运行这个脚本,Bash 都会随机选择一个不同的数字。
|
||||
|
||||
这个“猜数字”游戏是学习新的编程语言时的一个很好的入门程序,因为它以一种很直接的方式锻炼了几个常见的编程概念。通过在不同的编程语言中实现这个简单的游戏,你可以展示一些核心概念,并比较每种语言的细节。
|
||||
|
||||
你有喜欢的编程语言吗?你会如何用它来写“猜数字”游戏呢?请关注本系列文章,看看你可能感兴趣的其他编程语言的例子。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/12/learn-bash
|
||||
|
||||
作者:[Jim Hall][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/jim-hall
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bash_command_line.png?itok=k4z94W2U (bash logo on green background)
|
||||
[2]: https://en.wikipedia.org/wiki/Bash_(Unix_shell)
|
||||
[3]: https://opensource.com/resources/what-bash
|
||||
[4]: https://opensource.com/article/20/4/bash-programming-guide
|
||||
[5]: https://opensource.com/article/20/4/bash-sysadmins-ebook
|
||||
[6]: https://opensource.com/article/20/6/bash-functions
|
||||
[7]: https://opensource.com/tags/bash
|
Loading…
Reference in New Issue
Block a user