translated

This commit is contained in:
geekpi 2021-07-06 08:49:43 +08:00
parent 492fd0ec29
commit 3dc1311f9f
2 changed files with 155 additions and 155 deletions

View File

@ -1,155 +0,0 @@
[#]: subject: (How to parse Bash program configuration files)
[#]: via: (https://opensource.com/article/21/6/bash-config)
[#]: author: (David Both https://opensource.com/users/dboth)
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
How to parse Bash program configuration files
======
Separating config files from code enables anyone to change their
configurations without any special programming skills.
![bash logo on green background][1]
Keeping program configurations separate from code is important. It enables non-programmers to alter configurations without having to modify the program's code. With compiled binary executables, that would be impossible for non-programmers because it not only requires access to source files (which we do have with open source programs) but also a programmer's skill set. Few people have that, and most people don't want to learn.
With shell languages such as Bash, source code access is available by definition since shell scripts are not compiled into binary formats. Despite that openness, it is not a particularly good idea for non-programmers to root around in shell scripts and alter them. Even knowledgeable developers and sysadmins can make accidental changes that cause errors or worse.
So placing configuration items into easily maintained text files provides separation and allows non-programmers to edit configuration elements without the danger of making unintentional changes to the code. Many developers do this for programs written in compiled languages because they don't expect the users to be developers. For many of the same reasons, it also makes sense to do this with interpreted shell languages.
### The usual way
As with many other languages, you can write code for a Bash program that reads and parses ASCII text configuration files, reads the variable name, and sets values as the program code executes. For example, a configuration file might look like this:
```
var1=LinuxGeek46
var2=Opensource.com
```
The program would read that file, parse each line, and set the values into each variable.
### Sourcing
Bash uses a much easier method for parsing and setting variables called _sourcing_. Sourcing an external file from an executable shell program is a simple method for including the content of that file into a shell program in its entirety. In one sense, this is very much like compiled language `include` statements that include library files at runtime. Such a file can include any type of Bash code, including variable assignments.
As usual, it is easier to demonstrate than to explain.
First, create a `~/bin` directory (if it does not already exist), and make it the present working directory (PWD). The [Linux Filesystem Hierarchical Standard][2] defines `~/bin` as the appropriate place for users to store their executable files.
Create a new file in this directory. Name it `main` and make it executable:
```
[dboth@david bin]$ touch main
[dboth@david bin]$ chmod +x main
[dboth@david bin]$
```
Add the following content to this executable file:
```
#!/bin/bash
Name="LinuxGeek"
echo $Name
```
And execute this Bash program:
```
[dboth@david bin]$ ./main
LinuxGeek
[dboth@david bin]$
```
Create a new file and call it `~/bin/data`. This file does not need to be executable. Add the following information to it:
```
# Sourced code and variables
echo "This is the sourced code from the data file."
FirstName="David"
LastName="Both"
```
Add three lines to the `main` program so that it looks like this:
```
#!/bin/bash
Name="LinuxGeek"
echo $Name
source ~/bin/data
echo "First name: $FirstName"
echo "LastName: $LastName"
```
Rerun the program:
```
[dboth@david bin]$ ./main
LinuxGeek
This is the sourced code from the data file.
First name: David
LastName: Both
[dboth@david bin]$
```
There is one more really cool thing to know about sourcing. You can use a single dot (`.`) as a shortcut for the `source` command. Change the `main` file to substitute the `.` in place of `source`:
```
#!/bin/bash
Name="LinuxGeek"
echo $Name
. ~/bin/data
echo "First name: $FirstName"
echo "LastName: $LastName"
```
And run the program again. The result should be exactly the same as the previous run.
### Starting Bash
Every Linux host that uses Bash—which is pretty much all of them since Bash is the default shell for all distributions—includes some excellent, built-in examples of sourcing.
Whenever a Bash shell starts, its environment must be configured so that it is usable. There are five main files and one directory that are used to configure the Bash environment. They are listed here along with their main functions:
* `/etc/profile`: System-wide environment and startup programs
* `/etc/bashrc`: System-wide functions and aliases
* `/etc/profile.d/`: Directory that contains system-wide scripts for configuring various command-line tools such as `vim` and `mc` and any custom configuration scripts a sysadmin creates
* `~/.bash_profile`: User-specific environment and startup programs
* `~/.bashrc`: User-specific aliases and functions
* `~/.bash_logout`: User-specific commands to execute when the user logs out
Try to trace the execution sequence through these files and determine which sequence it uses for a non-login Bash initialization versus a log-in Bash initialization. I did this in Chapter 17 of Volume 1 in my Linux training series, [_Using and administering Linux: Zero to sysadmin_][3].
I'll give you one hint. It all starts with the `~/.bashrc` script.
### Conclusion
This article explored sourcing for pulling code and variable assignments into a Bash program. This method of parsing variables from a configuration file is fast, easy, and flexible. It provides a method for separating Bash code from variable assignments to allow non-programmers to set the values of those variables.
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/6/bash-config
作者:[David Both][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/dboth
[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]: http://refspecs.linuxfoundation.org/fhs.shtml
[3]: http://www.both.org/?page_id=1183

View File

@ -0,0 +1,155 @@
[#]: subject: (How to parse Bash program configuration files)
[#]: via: (https://opensource.com/article/21/6/bash-config)
[#]: author: (David Both https://opensource.com/users/dboth)
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
如何解析 Bash 程序的配置文件
======
将配置文件与代码分开,使任何人都可以改变他们的配置,而不需要任何特殊的编程技巧。
![bash logo on green background][1]
将程序配置与代码分开是很重要的。它使非程序员能够改变配置而不需要修改程序的代码。如果是编译的二进制可执行文件,这对非程序员来说是不可能的,因为它不仅需要访问源文件(我们在开源程序中会这样),而且还需要程序员的技能组合。很少有人有这种能力,而且大多数人都不想学习。
对于像 Bash 这样的 shell 语言,由于 shell 脚本没有被编译成二进制格式,所以从定义上讲,源码是可以访问的。尽管有这种开放性,但对于非程序员来说,在 shell 脚本中钻研和修改它们并不是一个特别好的主意。即使是经验丰富的开发人员和系统管理员,也会意外地做出一些改变,导致错误或更糟。
因此,将配置项放在容易维护的文本文件中,提供了分离,并允许非程序员编辑配置,而不会有对代码进行意外修改的危险。许多开发者对用编译语言编写的程序都是这样做的,因为他们并不期望用户是开发者。由于许多相同的原因,对解释型 shell 语言这样做也是有意义的。
### 通常的方式
和其他许多语言一样, 你可以为 Bash 程序编写代码, 读取并解析 ASCII 文本的配置文件, 读取变量名称, 并在程序代码执行时设置值. 例如,一个配置文件可能看起来像这样:
```
var1=LinuxGeek46
var2=Opensource.com
```
程序将读取文件,解析每一行,并将值设置到每个变量中。
### Sourcing
Bash 使用一种更简单的方法来解析和设置变量, 叫做 _sourcing_. 从一个可执行的 shell 程序中获取一个外部文件是一种简单的方法,可以将该文件的内容完整地引入 shell 程序中。在某种意义上,这很像编译语言的 `include` 语句,在运行时包括库文件。这样的文件可以包括任何类型的 Bash 代码,包括变量赋值。
像往常一样,演示比解释更容易。
首先,创建一个 `~/bin` 目录(如果它还不存在的话),并将其作为当前工作目录 PWD。[Linux 文件系统分层标准][2]将 `~/bin` 定义为用户存储可执行文件的适当位置。
在这个目录下创建一个新文件。将其命名为 `main`,并使其可执行:
```
[dboth@david bin]$ touch main
[dboth@david bin]$ chmod +x main
[dboth@david bin]$
```
在这个可执行文件中添加以下内容:
```
#!/bin/bash
Name="LinuxGeek"
echo $Name
```
并执行这个 Bash 程序:
```
[dboth@david bin]$ ./main
LinuxGeek
[dboth@david bin]$
```
创建一个新的文件并命名为 `~/bin/data`。这个文件不需要是可执行的。在其中添加以下信息:
```
# Sourced code and variables
echo "This is the sourced code from the data file."
FirstName="David"
LastName="Both"
```
`main` 程序中增加三行,看起来像这样:
```
#!/bin/bash
Name="LinuxGeek"
echo $Name
source ~/bin/data
echo "First name: $FirstName"
echo "LastName: $LastName"
```
重新运行该程序:
```
[dboth@david bin]$ ./main
LinuxGeek
This is the sourced code from the data file.
First name: David
LastName: Both
[dboth@david bin]$
```
关于 sourcing 还有一件非常酷的事情要知道。你可以使用一个单点(`.`)作为 `source` 命令的快捷方式。改变 `main` 文件,用 `.` 代替 `source`
```
#!/bin/bash
Name="LinuxGeek"
echo $Name
. ~/bin/data
echo "First name: $FirstName"
echo "LastName: $LastName"
```
并再次运行该程序。其结果应该与之前的运行完全相同。
### 运行 Bash
每一台使用 Bash 的 Linux 主机(几乎所有主机都是,因为 Bash 是所有发行版的默认 shell都包括一些优秀的、内置的 sourcing 示例。
每当 Bash shell 运行时,它的环境必须被配置成可以使用的样子。有五个主要文件和一个目录用于配置 Bash 环境。它们和它们的主要功能如下:
* `/etc/profile`: 全系统环境和启动程序
* `/etc/bashrc`: 全系统的函数和别名
* `/etc/profile.d/`: 包含全系统脚本的目录,用于配置各种命令行工具,如 `vim``mc` 以及系统管理员创建的任何自定义配置脚本
* `~/.bash_profile`: 用户特定的环境和启动程序
* `~/.bashrc`: 用户特定的别名和函数
* `~/.bash_logout`: 用户特定的命令,在用户注销时执行
试着通过这些文件追踪执行顺序,确定它在非登录 Bash 初始化和登录 Bash 初始化中使用的顺序。我在我的 Linux 培训系列[_使用和管理 Linux从零到系统管理员_][3]的第一卷第 17 章中做了这个工作。
给你一个提示。这一切都从 `~/.bashrc` 脚本开始。
### 总结
这篇文章探讨了在 Bash 程序中引用代码和变量的方法。这种从配置文件中解析变量的方法是快速、简单和灵活的。它提供了一种将 Bash 代码与变量赋值分开的方法,以使非程序员能够设置这些变量的值。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/6/bash-config
作者:[David Both][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/dboth
[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]: http://refspecs.linuxfoundation.org/fhs.shtml
[3]: http://www.both.org/?page_id=1183