mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
c2e7e4d6d4
@ -1,3 +1,5 @@
|
||||
Translating by DavidChenLiang
|
||||
|
||||
What I Learned from Programming Interviews
|
||||
============================================================
|
||||
|
||||
@ -137,4 +139,4 @@ via: https://medium.freecodecamp.org/what-i-learned-from-programming-interviews-
|
||||
[2]:https://thewomenintechshow.com/2017/12/18/programming-interviews/
|
||||
[3]:https://www.amazon.com/How-We-Test-Software-Microsoft/dp/0735624259
|
||||
[4]:https://thewomenintechshow.com/2017/12/18/programming-interviews/
|
||||
[5]:https://thewomenintechshow.com/
|
||||
[5]:https://thewomenintechshow.com/
|
||||
|
@ -1,3 +1,5 @@
|
||||
pinewall translating
|
||||
|
||||
How do private keys work in PKI and cryptography?
|
||||
======
|
||||
|
||||
|
@ -1,131 +0,0 @@
|
||||
pinewall translating
|
||||
|
||||
Ubuntu 18.04 Vs. Fedora 28
|
||||
======
|
||||
![](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-vs-fedora-28_orig.jpg)
|
||||
|
||||
Hello folks. Today I'll highlight some of the features and differences between the two popular Linux distros; **Ubuntu 18.04** and **Fedora 28** . Each has their own package management; Ubuntu uses DEB while Fedora uses RPM, but both of them features the same [Desktop Environment][3] ([GNOME][4]) and aims to provide quality desktop experience for the Linux users.
|
||||
|
||||
**Ubuntu 18.04** is the latest Ubuntu [LTS][1] release and comes equipped with GNOME desktop. So is Fedora 28 that also features GNOME desktop, but both of them provides a unique desktop experience when it comes down to their software management and of course their User Interfaces too.
|
||||
|
||||
### Quick Facts
|
||||
|
||||
Did you know that Ubuntu which is based on Debian, provides the latest software earlier than the latter one? An example is the popular web browser Firefox Quantum found on Ubuntu while Debian follows behind on the ESR (Extended Support Release) version of the same web browser.
|
||||
|
||||
|
||||
|
||||
The same applies to Fedora which provides cutting-edge software to the end users and also acts as the testing platform for the next stable RHEL (Red Hat Enterprise Linux) release.
|
||||
|
||||
### Desktop overview
|
||||
|
||||
Fedora provides vanilla GNOME desktop experience while Ubuntu 18.04 have tweaked certain aspects of the desktop to enable long-time Unity users a smooth transition to GNOME Desktop Environment.
|
||||
|
||||
_Canonical decided to save development time by ditching Unity and switching to GNOME desktop (starting from Ubuntu [17.10][2]) so they can focus more on IoT.
|
||||
_
|
||||
|
||||
So on Fedora, we have a clean icon-less desktop, a hidden panel on the overview and its look featuring GNOME default theme: Adwaita.
|
||||
|
||||
[![fedora 28 gnome](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-gnome_orig.jpg)][5]
|
||||
|
||||
Whereas Ubuntu features its classic desktop style with icons, a panel on the left mimicking its traditional dock, and customized window looks (also traditional) with Ubuntu Ambiance theme set as its default look and feel.
|
||||
|
||||
[![Ubuntu gnome 18.04](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-gnome-18-04_orig.jpg)][6]
|
||||
|
||||
However, learning to use one of them and then switching to another won't cost you time. Instead, they are designed with simplicity and user-friendliness in mind so any newbie can feel right at home with either of the two Linux distros.
|
||||
|
||||
|
||||
|
||||
But it's not just the looks or UI that determines the user's decision to choose a Linux distro. Other factors come into the role too, and below are more sub-topics describing all about software management between the two Linux OS.
|
||||
|
||||
### Software center
|
||||
|
||||
Ubuntu uses dpkg; Debian Package Management, for distributing software to end users while Fedora uses Red Hat Package Management called rpm. Both are very popular package management among the Linux community and their command line tools are easy to use too.
|
||||
|
||||
[![ubuntu software center](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-software-center_2_orig.jpg)][7]
|
||||
|
||||
But each Linux distro quite varies when it comes to the software that's being distributed. Canonical releases new Ubuntu versions every six months; usually in the month of April and then on October. So for each release, the developers maintain a development schedule, after a new Ubuntu release, it enters into "freeze" state where its development on testing new software is halted.
|
||||
|
||||
|
||||
|
||||
Whereas, Fedora also following the same six months release cycle, pretty much mimics a rolling release Linux distro (though it is not one of those). Almost all software packages are updated regularly so users get the opportunity to try out the latest software, unlike Ubuntu. However, this invites "instability" on the user's side as software bugs are more commonly faced but not critical enough to render the system unusable.
|
||||
|
||||
### Software updates
|
||||
|
||||
I've mentioned above about Ubuntu "freeze" state. Well, I'll be exaggerating more about this state since it has some significant importance on the way Ubuntu software is updated... So, once a new version of Ubuntu is released, its development (testing new software) is halted.
|
||||
|
||||
_The development for the next upcoming Ubuntu release begins where it'll go through the phases of "daily builds" then "beta release" and finally shipping the new Ubuntu release to the end users._
|
||||
|
||||
In this "freeze" state Ubuntu maintainers no longer add the latest software (unless it fixes serious security issues) to its package repository. So Ubuntu users get more "bug fixes" updates than "feature" updates, which is great since the system would remain stable without disrupting user's productivity.
|
||||
|
||||
|
||||
|
||||
Fedora aims to provide cutting-edge software to the end users so users get more "feature" updates than on Ubuntu. Also, measures are taken by the developers to maintain its system stability. For instance, on computer start up the user will be given at most three working kernels (latest one on the top) choices so if one fails to start the user can revert to the other two previous working kernels.
|
||||
|
||||
### Snaps and flatpak
|
||||
|
||||
Both are new and cool sexy tools for distributing software across multiple Linux distributions. Ubuntu provides **snaps** out of the box while **flatpak** goes to Fedora. The most popular among the two is snaps where more popular and proprietary applications are finding their way on snap store. Flatpak too is gaining traction with more apps added onto its platform.
|
||||
|
||||
|
||||
|
||||
Unfortunately, both of them are still new and there are some "window theme-breaking" rants dispersed around the Internet. But still, switching between the two tools isn't nerve wrecking as they are easy to use.
|
||||
|
||||
### Apps showdown
|
||||
|
||||
Below are some of the common apps available on Ubuntu and Fedora, they are compared between the two platforms:
|
||||
|
||||
#### Calculator
|
||||
|
||||
The program launches faster on Fedora than on Ubuntu. The reason is on Fedora, calculator program is natively installed while on Ubuntu the snap version of the same program is installed.
|
||||
|
||||
#### System Monitor
|
||||
|
||||
This might sound nerdy but I find it necessary and intuitive to observe my computer performance and kill offending processes if any. The program's launch time is the same as above ie., faster on Fedora (natively installed) and slower on Ubuntu (snap version).
|
||||
|
||||
#### Help
|
||||
|
||||
I've mentioned above that Ubuntu provides a tweaked version of the GNOME Desktop Environment (for long time Unity users migration ease). Unfortunately, Ubuntu developers have either forgotten or ignored to update the Help program since it's somewhat confusing to look at the documentation (getting started videos) and finding out the demonstration videos and the actual environment varies a tad-bit slightly.
|
||||
|
||||
[![ubuntu 18.04 help manual](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-help-manual_orig.jpg)][8]
|
||||
|
||||
### Conclusion
|
||||
|
||||
Ubuntu and Fedora are two popular Linux distros. Each has their own eye-candy features, so choosing between the two would be quite a challenge for newbies. I recommend trying both of them out so you can later find out which tools provided by the two Linux distro better suits you.
|
||||
|
||||
|
||||
|
||||
I hope you had a good read and let me know what I missed out/your opinions in the comment section below.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.linuxandubuntu.com/home/ubuntu-1804-vs-fedora-28
|
||||
|
||||
作者:[LinuxAndUbuntu][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxandubuntu.com
|
||||
[1]:http://www.linuxandubuntu.com/home/ubuntu-1804-codename-announced-bionic-beaver
|
||||
[2]:http://www.linuxandubuntu.com/home/what-new-is-going-to-be-in-ubuntu-1704-zesty-zapus
|
||||
[3]:http://www.linuxandubuntu.com/home/5-best-linux-desktop-environments-with-pros-cons
|
||||
[4]:http://www.linuxandubuntu.com/home/walkthrough-on-how-to-use-gnome-boxes
|
||||
[5]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-gnome_orig.jpg
|
||||
[6]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-gnome-18-04_orig.jpg
|
||||
[7]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-software-center_2_orig.jpg
|
||||
[8]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-help-manual_orig.jpg
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.linuxandubuntu.com/home/ubuntu-1804-vs-fedora-28
|
||||
|
||||
作者:[LinuxAndUbuntu][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxandubuntu.com
|
@ -1,255 +0,0 @@
|
||||
LuuMing Translating
|
||||
How to define and use functions in Linux Shell Script
|
||||
======
|
||||
Function is a reusable block of code. Often we put repeated code in a function and call that function from various places. Library is a collection of functions. We can define commonly used function in a library and other scripts can use them without duplicating code.
|
||||
|
||||
[![Functions-Linux-Shell-Script][1]![Functions-Linux-Shell-Script][2]][2]
|
||||
|
||||
In this article we’ll discuss more about functions and recipes. For demonstration purpose I’ll be using **Bourne Again SHell(Bash)** on Ubuntu machine.
|
||||
|
||||
#### Calling function
|
||||
|
||||
In Shell calling function is exactly same as calling any other command. For instance, if your function name is my_func then it can be execute as follows:
|
||||
```
|
||||
$ my_func
|
||||
|
||||
```
|
||||
|
||||
If any function accepts arguments then those can be provided from command line as follows:
|
||||
```
|
||||
$ my_func arg1 arg2 arg3
|
||||
|
||||
```
|
||||
|
||||
#### Defining function
|
||||
|
||||
We can use below syntax to define function:
|
||||
```
|
||||
function function_name {
|
||||
Body of function
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Body of function can contain any valid command, loop constrain, other function or script. Now let us create simple function which displays message on screen.
|
||||
```
|
||||
function print_msg {
|
||||
echo "Hello, World"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Now let us execute this function:
|
||||
```
|
||||
$ print_msg
|
||||
Hello, World
|
||||
|
||||
```
|
||||
|
||||
As expected, this function displays message on screen.
|
||||
|
||||
In above example we have created function directly on terminal. We can store this function in file as well. Below example demonstrates this.
|
||||
```
|
||||
#! /bin/bash
|
||||
function print_msg {
|
||||
echo "Hello, World"
|
||||
}
|
||||
print_msg
|
||||
|
||||
```
|
||||
|
||||
We have defined this function inside **function.sh** file. Now let us execute this script:
|
||||
```
|
||||
$ chmod +x function.sh
|
||||
$ ./function.sh
|
||||
Hello, World
|
||||
|
||||
```
|
||||
|
||||
If you observe, above output is exactly identical to previous one.
|
||||
|
||||
#### More about functions
|
||||
|
||||
In previous section we have defined very basic function. However during software development we need more advanced functions which can accept various parameters and return values. In this section we’ll discuss such functions.
|
||||
|
||||
**Passing arguments to function**
|
||||
|
||||
We can provide arguments to function same as other commands. We can access these arguments from function using dollar($) symbol. For instance, $1 represents first argument, $2 represents second argument and so on.
|
||||
|
||||
Let us modify above function to accept message as an argument. Our modified function will look like this:
|
||||
```
|
||||
function print_msg {
|
||||
echo "Hello $1"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
In above function we are accessing first argument using $1. Let us execute this function:
|
||||
```
|
||||
$ print_msg "LinuxTechi"
|
||||
|
||||
```
|
||||
|
||||
When you execute this function, it will generate following output:
|
||||
```
|
||||
Hello LinuxTechi
|
||||
|
||||
```
|
||||
|
||||
**Returning value from function**
|
||||
|
||||
Like other programming languages, Bash provides return statement using that we can return value to the caller. Let us understand this with example:
|
||||
```
|
||||
function func_return_value {
|
||||
return 10
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Above function returns value 10 to its caller. Let us execute this function:
|
||||
```
|
||||
$ func_return_value
|
||||
$ echo "Value returned by function is: $?"
|
||||
|
||||
```
|
||||
|
||||
When you execute above function, it will generate following output:
|
||||
```
|
||||
Value returned by function is: 10
|
||||
|
||||
```
|
||||
|
||||
**NOTE:** In bash we have to use $? to capture return value of function
|
||||
|
||||
#### Function recipes
|
||||
|
||||
So far we got fair idea about bash functions. Now let us create some useful bash functions which can be used to make our lives easier.
|
||||
|
||||
**Logger**
|
||||
|
||||
Let us create logger function which will print date and time along with log message.
|
||||
|
||||
Let us execute this function:
|
||||
```
|
||||
$ log_msg "This is sample log message"
|
||||
|
||||
```
|
||||
|
||||
When you execute this function, it will generate following output:
|
||||
```
|
||||
[ 2018-08-16 19:56:34 ]: This is sample log message
|
||||
|
||||
```
|
||||
|
||||
**Display system information**
|
||||
|
||||
Let us create a function to display information about GNU/Linux system
|
||||
```
|
||||
function system_info {
|
||||
echo "### OS information ###"
|
||||
lsb_release -a
|
||||
|
||||
echo
|
||||
echo "### Processor information ###"
|
||||
processor=`grep -wc "processor" /proc/cpuinfo`
|
||||
model=`grep -w "model name" /proc/cpuinfo | awk -F: '{print $2}'`
|
||||
echo "Processor = $processor"
|
||||
echo "Model = $model"
|
||||
|
||||
echo
|
||||
echo "### Memory information ###"
|
||||
total=`grep -w "MemTotal" /proc/meminfo | awk '{print $2}'`
|
||||
free=`grep -w "MemFree" /proc/meminfo | awk '{print $2}'`
|
||||
echo "Total memory: $total kB"
|
||||
echo "Free memory : $free kB"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
When you execute above function it will generate following output:
|
||||
```
|
||||
### OS information ###
|
||||
No LSB modules are available.
|
||||
Distributor ID: Ubuntu
|
||||
Description: Ubuntu 18.04.1 LTS
|
||||
Release: 18.04
|
||||
Codename: bionic
|
||||
|
||||
### Processor information ###
|
||||
Processor = 1
|
||||
Model = Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
|
||||
|
||||
### Memory information ###
|
||||
Total memory: 4015648 kB
|
||||
Free memory : 2915428 kB
|
||||
|
||||
```
|
||||
|
||||
Find file or directory from current directory
|
||||
|
||||
Below function searches file or directory from current directory:
|
||||
```
|
||||
function search {
|
||||
find . -name $1
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Let us search directory namely dir4 using below command:
|
||||
```
|
||||
$ search dir4
|
||||
|
||||
```
|
||||
|
||||
When you execute above command, it will generate following output:
|
||||
```
|
||||
./dir1/dir2/dir3/dir4
|
||||
|
||||
```
|
||||
|
||||
**Digital clock**
|
||||
|
||||
Below function creates a simple digital clock on terminal
|
||||
```
|
||||
function digital_clock {
|
||||
clear
|
||||
while [ 1 ]
|
||||
do
|
||||
date +'%T'
|
||||
sleep 1
|
||||
clear
|
||||
done
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
#### Creating library
|
||||
|
||||
Library is a collection of functions. To create library – define functions in a file and import that file in current environment.
|
||||
|
||||
Let us suppose we have defined all functions in utils.sh file then use below command to import functions in current environment:
|
||||
```
|
||||
$ source utils.sh
|
||||
|
||||
```
|
||||
|
||||
Hereafter you can execute any function from library just like any other bash command.
|
||||
|
||||
##### Conclusion
|
||||
|
||||
In this article we discussed few useful recipes which will improve your productivity. I hope this articles inspires you to create your own recipes.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linuxtechi.com/define-use-functions-linux-shell-script/
|
||||
|
||||
作者:[Pradeep Kumar][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxtechi.com/author/pradeep/
|
||||
[1]:https://www.linuxtechi.com/wp-content/plugins/lazy-load/images/1x1.trans.gif
|
||||
[2]:https://www.linuxtechi.com/wp-content/uploads/2018/08/Functions-Linux-Shell-Script.jpg
|
@ -1,3 +1,5 @@
|
||||
pinewall translating
|
||||
|
||||
A Collection Of More Useful Unix Utilities
|
||||
======
|
||||
|
||||
|
@ -1,82 +1,81 @@
|
||||
Go 编译器介绍
|
||||
======
|
||||
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
## Go编译器介绍
|
||||
> Copyright 2018 The Go Authors. All rights reserved.
|
||||
> Use of this source code is governed by a BSD-style
|
||||
> license that can be found in the LICENSE file.
|
||||
|
||||
`cmd/compile` 包含构成 Go 编译器主要的包。编译器在逻辑上可以被分为四个阶段,我们将简要介绍这几个阶段以及包含相应代码的包的列表。
|
||||
|
||||
在谈到编译器时,有时可能会听到“前端”和“后端”这两个术语。粗略地说,这些对应于我们将在此列出的前两个和后两个阶段。第三个术语“中间端”通常指的是第二阶段执行的大部分工作。
|
||||
在谈到编译器时,有时可能会听到<ruby>前端<rt>front-end</rt></ruby>和<ruby>后端<rt>back-end</rt></ruby>这两个术语。粗略地说,这些对应于我们将在此列出的前两个和后两个阶段。第三个术语<ruby>中间端<rt>middle-end</rt></ruby>通常指的是第二阶段执行的大部分工作。
|
||||
|
||||
请注意,`go/parser` 和 `go/types` 等 `go/*` 系列包与编译器无关。由于编译器最初是用C编写的,所以这些 `go/*` 包被开发出来以便于能够写出和 `Go` 代码一起工作的工具,例如 `gofmt` 和 `vet`。
|
||||
请注意,`go/parser` 和 `go/types` 等 `go/*` 系列的包与编译器无关。由于编译器最初是用 C 编写的,所以这些 `go/*` 包被开发出来以便于能够写出和 `Go` 代码一起工作的工具,例如 `gofmt` 和 `vet`。
|
||||
|
||||
需要澄清的是,名称“gc”代表“Go 编译器”,与大写 GC 无关,后者代表垃圾收集。
|
||||
需要澄清的是,名称 “gc” 代表 “Go 编译器”,与大写 GC 无关,后者代表<ruby>垃圾收集<rt>garbage collection</rt></ruby>。
|
||||
|
||||
### 1. 解析
|
||||
|
||||
* `cmd/compile/internal/syntax` (词法分析器、解析器、语法树)
|
||||
* `cmd/compile/internal/syntax`(<ruby>词法分析器<rt>lexer</rt></ruby>、<ruby>解析器<rt>parser</rt></ruby>、<ruby>语法树<rt>syntax tree</rt></ruby>)
|
||||
|
||||
在编译的第一阶段,源代码被标记化(词法分析),解析(语法分析),并为每个源文件构造语法树(译注:这里标记指token,它是一组预定义、能够识别的字符串,通常由名字和值构成,其中名字一般是词法的类别,如标识符、关键字、分隔符、操作符、文字和注释等;语法树,以及下文提到的AST,Abstract Syntax Tree,抽象语法树,是指用树来表达程序设计语言的语法结构,通常叶子节点是操作数,其它节点是操作码)。
|
||||
在编译的第一阶段,源代码被标记化(词法分析),解析(语法分析),并为每个源文件构造语法树(译注:这里标记指 token,它是一组预定义的、能够识别的字符串,通常由名字和值构成,其中名字一般是词法的类别,如标识符、关键字、分隔符、操作符、文字和注释等;语法树,以及下文提到的<ruby>抽象语法树<rt>Abstract Syntax Tree</rt></ruby>(AST),是指用树来表达程序设计语言的语法结构,通常叶子节点是操作数,其它节点是操作码)。
|
||||
|
||||
每棵语法树都是相应源文件的确切表示,其中节点对应于源文件的各种元素,例如表达式,声明和语句。语法树还包括位置信息,用于错误报告和创建调试信息。
|
||||
每个语法树都是相应源文件的确切表示,其中节点对应于源文件的各种元素,例如表达式、声明和语句。语法树还包括位置信息,用于错误报告和创建调试信息。
|
||||
|
||||
### 2. 类型检查和AST变形
|
||||
### 2. 类型检查和 AST 变形
|
||||
|
||||
* `cmd/compile/internal/gc` (创建编译器AST,类型检查,AST变形)
|
||||
* `cmd/compile/internal/gc`(创建编译器 AST,<ruby>类型检查<rt>type-checking</rt></ruby>,<ruby>AST 变形<rt>AST transformation</rt></ruby>)
|
||||
|
||||
gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义。所有代码都是基于该 AST 编写的,所以 gc 包必须做的第一件事就是将 syntax 包(定义)的语法树转换为编译器的 AST 表示法。这个额外步骤可能会在将来重构。
|
||||
gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义。所有代码都是基于它编写的,所以 gc 包必须做的第一件事就是将 syntax 包(定义)的语法树转换为编译器的 AST 表示法。这个额外步骤可能会在将来重构。
|
||||
|
||||
然后对 AST 进行类型检查。第一步是名字解析和类型推断,它们确定哪个对象属于哪个标识符,以及每个表达式具有的类型。类型检查包括特定的额外检查,例如“声明但未使用”以及确定函数是否会终止。
|
||||
|
||||
特定转换也基于 AST 上完成。一些节点被基于类型信息而细化,例如把字符串加法从算术加法的节点类型中拆分出来。其他一些例子是死代码消除,函数调用内联和逃逸分析(译注:逃逸分析是一种分析指针有效范围的方法)。
|
||||
特定转换也基于 AST 完成。一些节点被基于类型信息而细化,例如把字符串加法从算术加法的节点类型中拆分出来。其它一些例子是<ruby>死代码消除<rt>dead code elimination</rt></ruby>,<ruby>函数调用内联<rt>function call inlining</rt></ruby>和<ruby>逃逸分析<rt>escape analysis</rt></ruby>(译注:逃逸分析是一种分析指针有效范围的方法)。
|
||||
|
||||
### 3. 通用SSA
|
||||
### 3. 通用 SSA
|
||||
|
||||
* `cmd/compile/internal/gc` (转换成 SSA)
|
||||
* `cmd/compile/internal/gc`(转换成 SSA)
|
||||
* `cmd/compile/internal/ssa`(SSA 相关的 pass 和规则)
|
||||
|
||||
* `cmd/compile/internal/ssa` (SSA 相关的 pass 和规则)
|
||||
(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍 pass;最后一遍 pass 之前所有的 pass 得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法。SSA,静态单赋值形式,是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义)。
|
||||
|
||||
(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即 pass;最后一遍之前所有的 pass 得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法。SSA,静态单赋值形式,是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义)。
|
||||
在此阶段,AST 将被转换为<ruby>静态单赋值<rt>Static Single Assignment</rt></ruby>(SSA)形式,这是一种具有特定属性的低级<ruby>中间表示法<rt>intermediate representation</rt></ruby>,可以更轻松地实现优化并最终从它生成机器码。
|
||||
|
||||
在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码。
|
||||
在这个转换过程中,将完成<ruby>内置函数<rt>function intrinsics</rt></ruby>的处理。这些是特殊的函数,编译器被告知逐个分析这些函数并决定是否用深度优化的代码替换它们(译注:内置函数指由语言本身定义的函数,通常编译器的处理方式是使用相应实现函数的指令序列代替对函数的调用指令,有点类似内联函数)。
|
||||
|
||||
在这个转换过程中,将完成内置函数的处理。 这些是特殊的函数,编译器被告知逐个分析这些函数并决定是否用深度优化的代码替换它们(译注:内置函数指由语言本身定义的函数,通常编译器的处理方式是使用相应实现函数的指令序列代替对函数的调用指令,有点类似内联函数)。
|
||||
在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range 循环被改写为 for 循环。由于历史原因,目前这里面有些在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。
|
||||
|
||||
在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环。由于历史原因,目前这里面有些在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。
|
||||
然后,一系列机器无关的规则和 pass 会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。
|
||||
|
||||
然后,一系列机器无关的规则和pass会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。
|
||||
|
||||
这类通用的 pass 的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。
|
||||
这类通用 pass 的一些例子包括,死代码消除,移除不必要的空值检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。
|
||||
|
||||
### 4. 生成机器码
|
||||
|
||||
* `cmd/compile/internal/ssa` (SSA 低级化和体系结构特定的pass)
|
||||
* `cmd/compile/internal/ssa`(SSA 低级化和架构特定的 pass)
|
||||
* `cmd/internal/obj`(机器码生成)
|
||||
|
||||
* `cmd/internal/obj` (机器代码生成)
|
||||
编译器中机器相关的阶段开始于“低级”的 pass,该阶段将通用变量改写为它们的特定的机器码形式。例如,在 amd64 架构中操作数可以在内存中操作,这样许多<ruby>加载-存储<rt>load-store</rt></ruby>操作就可以被合并。
|
||||
|
||||
编译器中机器相关的阶段开始于“低级”的 pass,该阶段将通用变量改写为它们的机器相关变形形式。例如,在 amd64 体系结构中操作数可以在内存中,这样许多装载-存储操作就可以被合并。
|
||||
注意低级的 pass 运行所有机器特定的重写规则,因此当前它也应用了大量优化。
|
||||
|
||||
注意低级的 pass 运行所有机器特定的重写规则,因此它也应用了很多优化。
|
||||
一旦 SSA 被“低级化”并且更具体地针对目标体系结构,就要运行最终代码优化的 pass 了。这包含了另外一个死代码消除的 pass,它将变量移动到更靠近它们使用的地方,移除从来没有被读过的局部变量,以及<ruby>寄存器<rt>register</rt></ruby>分配。
|
||||
|
||||
一旦 SSA 被“低级化”并且更具体地针对目标体系结构,就要运行最终代码优化的 pass 了。这包含了另外一个死代码消除的 pass,它将变量移动到更靠近它们使用的地方,移除从来没有被读过的局部变量,以及寄存器分配。
|
||||
本步骤中完成的其它重要工作包括<ruby>堆栈布局<rt>stack frame layout</rt></ruby>,它将堆栈偏移位置分配给局部变量,以及<ruby>指针活性分析<rt>pointer liveness analysis</rt></ruby>,后者计算每个垃圾收集安全点上的哪些堆栈上的指针仍然是活动的。
|
||||
|
||||
本步骤中完成的其它重要工作包括堆栈布局,它将指定局部变量在堆栈中的偏移位置,以及指针活性分析,后者计算每个垃圾收集安全点上的哪些堆栈上的指针仍然是活动的。
|
||||
在 SSA 生成阶段结束时,Go 函数已被转换为一系列 `obj.Prog` 指令。它们被传递给汇编程序(`cmd/internal/obj`),后者将它们转换为机器码并输出最终的目标文件。目标文件还将包含反射数据,导出数据和调试信息。
|
||||
|
||||
在 SSA 生成阶段结束时,Go 函数已被转换为一系列 obj.Prog 指令。它们被传递给汇编程序(`cmd/internal/obj`),后者将它们转换为机器代码并输出最终的目标文件。目标文件还将包含反射数据,导出数据和调试信息。
|
||||
### 扩展阅读
|
||||
|
||||
### 后续读物
|
||||
|
||||
要深入了解 SSA 包的工作方式,包括它的 pass 和规则,请转到 cmd/compile/internal/ssa/README.md。
|
||||
要深入了解 SSA 包的工作方式,包括它的 pass 和规则,请转到 [cmd/compile/internal/ssa/README.md][1]。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://github.com/golang/go/blob/master/src/cmd/compile/README.md
|
||||
|
||||
作者:[mvdan ][a]
|
||||
作者:[mvdan][a]
|
||||
译者:[stephenxs](https://github.com/stephenxs)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[pityonline](https://github.com/pityonline)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://github.com/mvdan
|
||||
[1]: https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/README.md
|
||||
|
120
translated/tech/20180812 Ubuntu 18.04 Vs. Fedora 28.md
Normal file
120
translated/tech/20180812 Ubuntu 18.04 Vs. Fedora 28.md
Normal file
@ -0,0 +1,120 @@
|
||||
对比 Ubuntu 18.04 和 Fedora 28
|
||||
======
|
||||
![](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-vs-fedora-28_orig.jpg)
|
||||
|
||||
大家好,我准备在今天突出说明一下两大主流 Linux 发行版,即 **Ubuntu 18.04** 和 **Fedora 28**,包括一些特性和差异。两者都有各自的包管理系统,其中 Ubuntu 使用 DEB,Fedora 使用 RPM;但二者使用同样的[<ruby>桌面环境<rt>Desktop Environment</rt></ruby>][3] (即 [GNOME][4])并致力于为 Linux 用户提供高品质的<ruby>桌面体验<rt>desktop experience</rt></ruby>。
|
||||
|
||||
**Ubuntu 18.04** 是 Ubuntu 目前最新的 [<ruby>长期支持版本<rt>LTS</rt></ruby>][1],为用户提供 GNOME 桌面系统。**Fedora 28** 也提供 GNOME 桌面系统,但落实到具体的软件包管理方面,二者的桌面体验存在差异;在<ruby>用户接口<rt>User Interfaces</rt></ruby>方面也显然存在差异。
|
||||
|
||||
### 基本概念
|
||||
|
||||
不知你是否了解,虽然 Ubuntu 基于 Debian,但 Ubuntu 比 Debian 更早提供最新版本的软件。举个例子,当 Ubuntu 提供流行网页浏览器 Firefox Quantum 时,Debian 仍在提供 Firefox 的<ruby>长期支持版<rt>ESR, Extended Support Release</rt></ruby>。
|
||||
|
||||
(LCTT 译注:从 2012 年 1 月开始,Firefox 进入快速版本期,每 6 周发布新的主线版本,每隔 7 个主线版本发布新的 ESR 版本。Firefox 57 的桌面版发布时被命名为Firefox Quantum,同期的 ESR 版本与 Firefox 52 一同发布并基于 Firefox 48。参考 [Wiki: History_of_Firefox][9])
|
||||
|
||||
同样的情况也适用于 Fedora,它为终端用户提供前沿的软件,也被用作下一个稳定 RHEL (Red Hat Enterprise Linux) 版本的测试平台。
|
||||
|
||||
### 桌面预览
|
||||
|
||||
Fedora 提供<ruby>原生版本的<rt>vanilla</rt></ruby> GNOME 桌面体验;相比之下,Ubuntu 18.04 对 GNOME 做了若干方面的微调,以便长期以来的 Unity 用户可以平滑的过渡到 GNOME 桌面环境。
|
||||
|
||||
_为节省开发时间,Canonical (从 Ubuntu [17.10][2] 开始)已经决定放弃 Unity并转向 GNOME 桌面,以便可以将更多精力投入到 IoT 领域。_
|
||||
|
||||
因此,在 Fedora 的桌面预览中,我们可以看到一个简洁的无图标桌面和一个自动隐藏的侧边栏,整体外观采用 GNOME 默认的 Adwaita 主题。
|
||||
|
||||
[![fedora 28 gnome](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-gnome_orig.jpg)][5]
|
||||
|
||||
相比之下,Ubuntu 采用其经典的有图标桌面样式,左侧边栏用于模拟其传统的<ruby>"程序坞"<rt>dock</rt></ruby>,使用 Ubuntu Ambiance 主题定制化窗口,与其传统的(Unity 桌面)外观和体验基本一致。
|
||||
|
||||
[![Ubuntu gnome 18.04](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-gnome-18-04_orig.jpg)][6]
|
||||
|
||||
虽然存在一定差异,但习惯使用其中一种桌面环境后切换到另外一种并不困难。毕竟二者设计时都充分考虑了简洁性和用户友好性,即使是新用户也不会对这两种 Linux 发行版感到不适应。
|
||||
|
||||
但外观或 UI 并不是决定用户选择哪一种 Linux 发行版的唯一因素,还有其它因素也会影响用户的选择。下面主要介绍两种 Linux 发行版在软件包管理相关方面的内容。
|
||||
|
||||
### 软件中心
|
||||
|
||||
Ubuntu 使用 dpkg,即 Debian Package Management,将软件分发给终端用户;Fedora 则使用 rpm,全称为 Red Hat Package Management。它们都是 Linux 社区中非常流行的包管理系统,对应的命令行工具也都简单易用。
|
||||
|
||||
[![ubuntu software center](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-software-center_2_orig.jpg)][7]
|
||||
|
||||
但在具体分发的软件方面,各个 Linux 发行版会有明显差异。Canonical 每 6 个月发布新版本的 Ubuntu,一般是在每年的 4 月和 10 月。对每个版本,开发者会维护一个开发计划;Ubuntu 新版本发布后,该版本就会进入 freeze 状态,即停止新软件的开发和测试。
|
||||
|
||||
相比之下,Fedora 也采用相似的 6 个月发布周期,看起来很像一种<ruby>滚动更新<rt>rolling release</rt></ruby>的 Linux 发行版(其实并不是这样)。与 Ubuntu 不同之处在于,(Fedora 中的)几乎所有软件包更新都很频繁,让用户有机会尝试最新版本的软件。但这样也导致软件 Bug 更频繁出现,给用户带来“不稳定性”,虽然还不至于导致系统不可用。
|
||||
|
||||
### 软件更新
|
||||
|
||||
我上面已经提到了 Ubuntu 版本的 freeze 状态。好吧,由于它对 Ubuntu 软件更新方式有着重要的影响,我再次提到这个状态:当 Ubuntu 新版本发布后,该版本的开发(这里是指测试新软件)就停止了。
|
||||
|
||||
_即将发布的下个版本的开发也随之开始,先后历经 “<ruby>每日构建<rt>daily build</rt></ruby>” 和 “<ruby>测试版<rt>beta release</rt></ruby>” 阶段,最后作为新版本发布给终端用户。_
|
||||
|
||||
在 freeze 状态下,Ubuntu 维护者不会在<ruby>软件源<rt>package repository</rt></ruby>中增加最新版软件,除非用于解决严重的安全问题。因此,Ubuntu 用户可用的软件更新更多涉及 Bug 修复而不是新特性,这样的好处在于系统可以保持稳定,不会扰乱用户的使用。
|
||||
|
||||
Fedora 试图为终端用户提供最新版本的软件,故用户的可用软件更新相比 Ubuntu 而言会更多涉及新特性。当然,开发者为了维持系统的稳定性,也采取了一系列措施。例如,在操作系统启动时,用户可以从最多三个<ruby>可用内核<rt>working kernel</rt></ruby>(最新内核处于最上方)中进行选择;当新内核无法启动时,用户可以回滚使用之前两个可用内核。
|
||||
|
||||
### Snaps 和 flatpak
|
||||
|
||||
它们都是新出现的酷炫工具,可以将软件发布到多个 Linux 发行版上。Ubuntu 提供 **snaps**,而 Fedora 则提供 **flatpak** 。二者之中 snaps 更加流行,更多流行软件或版权软件都在考虑上架 snap 商店。Flatpak 也在吸引关注,越来越多的软件上线该平台。
|
||||
|
||||
不幸的是,由于二者出现的时间都不久,很多人遇到“<ruby>窗口主题不一致<rt>window theme-breaking</rt></ruby>”问题并在网上表达不满。但由于二者都很易于使用,在二者之间切换并不是难事。
|
||||
|
||||
(LCTT 译注:按译者理解,由于二者都增加了一层安全隔离,读取系统主题方面会遇到问题;另外,似乎也有反馈 snap 专用主题无法及时应用于 snap 的问题)
|
||||
|
||||
### 应用对比
|
||||
|
||||
下面列出一些在 Ubuntu 和 Fedora 上共有的常见应用,然后在两个平台之间进行对比:
|
||||
|
||||
#### 计算器
|
||||
|
||||
Fedora 上的计算器程序启动速度更快。这是因为 Fedora 上的计算器程序是软件包形式安装的,而 Ubuntu 上的计算器程序则是 snap 版本。
|
||||
|
||||
#### 系统监视器
|
||||
|
||||
可能听上去比较书呆子气,但我认为观察计算机性能并杀掉令人讨厌的进程是必要且直观的。程序启动速度对比与计算器的结果一致,即 (软件包方式安装的)Fedora 版本快于(snap 形式提供的)Ubuntu 版本。
|
||||
|
||||
#### 帮助程序
|
||||
|
||||
我已经提到,(为便于长期以来的 Untiy 用户平滑切换到 GNOME),Ubuntu 提供的 GNOME 桌面环境是经过微调的版本。不幸的是,Ubuntu 开发者似乎忘记或忽略了对帮助程序的更新,用户阅读文档(入门视频)后会发现演示视频与真实环境有略微差异,这可能让人感到迷惑。
|
||||
|
||||
[![ubuntu 18.04 help manual](http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-help-manual_orig.jpg)][8]
|
||||
|
||||
### 结论
|
||||
|
||||
Ubuntu 和 Fedora 是两个主流的 Linux 发行版。两者都各自有一些华而不实的特性,因而新接触 Linux 的人很难抉择。我的建议是同时尝试二者,这样你在试用后可以发现哪个发行版提供的工具更适合你。
|
||||
|
||||
希望你阅读愉快,你可以在下方的评论区给出我漏掉的内容或你的建议。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.linuxandubuntu.com/home/ubuntu-1804-vs-fedora-28
|
||||
|
||||
作者:[LinuxAndUbuntu][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxandubuntu.com
|
||||
[1]:http://www.linuxandubuntu.com/home/ubuntu-1804-codename-announced-bionic-beaver
|
||||
[2]:http://www.linuxandubuntu.com/home/what-new-is-going-to-be-in-ubuntu-1704-zesty-zapus
|
||||
[3]:http://www.linuxandubuntu.com/home/5-best-linux-desktop-environments-with-pros-cons
|
||||
[4]:http://www.linuxandubuntu.com/home/walkthrough-on-how-to-use-gnome-boxes
|
||||
[5]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-gnome_orig.jpg
|
||||
[6]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-gnome-18-04_orig.jpg
|
||||
[7]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-software-center_2_orig.jpg
|
||||
[8]:http://www.linuxandubuntu.com/uploads/2/1/1/5/21152474/ubuntu-18-04-help-manual_orig.jpg
|
||||
[9]:https://en.wikipedia.org/wiki/History_of_Firefox
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.linuxandubuntu.com/home/ubuntu-1804-vs-fedora-28
|
||||
|
||||
作者:[LinuxAndUbuntu][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxandubuntu.com
|
@ -0,0 +1,259 @@
|
||||
如何在 Linux Shell 脚本中定义和使用函数
|
||||
======
|
||||
函数是一段可复用的代码。我们通常把重复的代码放进函数中并且在不同的地方去调用它。库是函数的集合。我们可以在库中定义经常使用的函数,这样其他脚本便可以不再重复代码而使用这些函数。
|
||||
[![Functions-Linux-Shell-Script][1]![Functions-Linux-Shell-Script][2]][2]
|
||||
|
||||
本文我们将讨论诸多关于函数的内容和一些使用技巧。为了方便展示,我将在 Ubuntu 机器上使用**Bourne Again SHell (Bash)**。
|
||||
|
||||
### 调用函数
|
||||
|
||||
在 Shell 中调用函数和调用其他命令是一模一样的。例如,如果你的函数名称为 my_func,你就可以像下面那样执行它:
|
||||
```
|
||||
$ my_func
|
||||
|
||||
```
|
||||
|
||||
如果你的函数接收多个参数,那么它们可以像下面那样写:
|
||||
```
|
||||
$ my_func arg1 arg2 arg3
|
||||
|
||||
```
|
||||
|
||||
### 定义函数
|
||||
|
||||
我们可以用下面的语法去定义一个函数:
|
||||
```
|
||||
function function_name {
|
||||
Body of function
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
函数的主体可以包含任何有效的命令、循环语句和其他函数或脚本。现在让我们创建一个简单的函数,它向屏幕上显示一些消息。
|
||||
```
|
||||
function print_msg {
|
||||
echo "Hello, World"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
现在,让我们执行这个函数:
|
||||
```
|
||||
$ print_msg
|
||||
Hello, World
|
||||
|
||||
```
|
||||
|
||||
不出所料,这个函数在屏幕上显示了一些消息。
|
||||
|
||||
在上面的例子中,我们直接在终端上创建了一个函数。我们也可以将这个函数保存在文件中。如下面的例子所示。
|
||||
```
|
||||
#! /bin/bash
|
||||
function print_msg {
|
||||
echo "Hello, World"
|
||||
}
|
||||
print_msg
|
||||
|
||||
```
|
||||
|
||||
我们已经在 **function.sh** 文件中定义了这个函数。现在让我们执行这个脚本:
|
||||
```
|
||||
$ chmod +x function.sh
|
||||
$ ./function.sh
|
||||
Hello, World
|
||||
|
||||
```
|
||||
|
||||
你可以看到,上面的输出和之前的是一模一样的。
|
||||
|
||||
### 更多关于函数
|
||||
|
||||
在上一小节中我们定义了一个非常简单的函数。然而在软件开发的过程中,我们需要更多高级的函数,它可以接收多个参数并且带有返回值。在这一小节中,我们将讨论这种函数。
|
||||
|
||||
**向函数传递参数**
|
||||
|
||||
我们可以像调用其他命令那样给函数提供参数。我们可以在函数里使用美元($)符号访问到这些参数。例如,$1 表示第一个参数,$2 代表第二个参数,以此类推。
|
||||
|
||||
让我们修改之前的函数,并接收信息当作参数。修改后的函数就像这样:
|
||||
```
|
||||
function print_msg {
|
||||
echo "Hello $1"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
在上面的函数中我们使用 $1 符号访问第一个参数。让我们执行这个函数:
|
||||
```
|
||||
$ print_msg "LinuxTechi"
|
||||
|
||||
```
|
||||
|
||||
执行完后,生成如下信息:
|
||||
```
|
||||
Hello LinuxTechi
|
||||
|
||||
```
|
||||
|
||||
**从函数中返回数值**
|
||||
|
||||
跟其他编程语言一样,Bash 提供了返回语句让我们可以向调用处返回一些数值。让我们举例说明:
|
||||
```
|
||||
function func_return_value {
|
||||
return 10
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
上面的函数向调用处返回 10。让我们执行这个函数:
|
||||
```
|
||||
$ func_return_value
|
||||
$ echo "Value returned by function is: $?"
|
||||
|
||||
```
|
||||
|
||||
当你执行完,将会产生如下的输出结果:
|
||||
```
|
||||
Value returned by function is: 10
|
||||
|
||||
```
|
||||
|
||||
**NOTE** 在 bash 中使用 $? 去获取函数的返回值
|
||||
|
||||
### 函数秘诀
|
||||
|
||||
到目前为止我们已经对 bash 中的函数有了相当多的了解。现在让我们创建一些非常有用的 bash 函数,它们可以让我们生活变得更加轻松。
|
||||
|
||||
**Logger**
|
||||
|
||||
让我们创建一个 logger 函数,它可以输出带有日期和时间的 log 信息。
|
||||
```
|
||||
function log_msg {
|
||||
echo "[`date '+ %F %T'` ]: $@"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
让我们执行这个函数:
|
||||
```
|
||||
$ log_msg "This is sample log message"
|
||||
|
||||
```
|
||||
|
||||
当你执行完,就会生成如下信息:
|
||||
```
|
||||
[ 2018-08-16 19:56:34 ]: This is sample log message
|
||||
|
||||
```
|
||||
|
||||
**显示系统信息**
|
||||
|
||||
让我们创建一个显示 GNU/Linux 信息的函数
|
||||
```
|
||||
function system_info {
|
||||
echo "### OS information ###"
|
||||
lsb_release -a
|
||||
|
||||
echo
|
||||
echo "### Processor information ###"
|
||||
processor=`grep -wc "processor" /proc/cpuinfo`
|
||||
model=`grep -w "model name" /proc/cpuinfo | awk -F: '{print $2}'`
|
||||
echo "Processor = $processor"
|
||||
echo "Model = $model"
|
||||
|
||||
echo
|
||||
echo "### Memory information ###"
|
||||
total=`grep -w "MemTotal" /proc/meminfo | awk '{print $2}'`
|
||||
free=`grep -w "MemFree" /proc/meminfo | awk '{print $2}'`
|
||||
echo "Total memory: $total kB"
|
||||
echo "Free memory : $free kB"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
当你执行完后将会生成以下信息:
|
||||
```
|
||||
### OS information ###
|
||||
No LSB modules are available.
|
||||
Distributor ID: Ubuntu
|
||||
Description: Ubuntu 18.04.1 LTS
|
||||
Release: 18.04
|
||||
Codename: bionic
|
||||
|
||||
### Processor information ###
|
||||
Processor = 1
|
||||
Model = Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
|
||||
|
||||
### Memory information ###
|
||||
Total memory: 4015648 kB
|
||||
Free memory : 2915428 kB
|
||||
|
||||
```
|
||||
|
||||
在当前目录下查找文件或者目录
|
||||
|
||||
下面的函数从当前目录下查找文件或者目录:
|
||||
```
|
||||
function search {
|
||||
find . -name $1
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
让我们使用下面的命令查找 dir4 这个目录:
|
||||
```
|
||||
$ search dir4
|
||||
|
||||
```
|
||||
|
||||
当你执行完命令后,将会产生如下输出:
|
||||
```
|
||||
./dir1/dir2/dir3/dir4
|
||||
|
||||
```
|
||||
|
||||
**数字时钟**
|
||||
|
||||
下面的函数在终端里创建了一个简单的数字时钟
|
||||
```
|
||||
function digital_clock {
|
||||
clear
|
||||
while [ 1 ]
|
||||
do
|
||||
date +'%T'
|
||||
sleep 1
|
||||
clear
|
||||
done
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### 创建库
|
||||
|
||||
库是函数的集合。创建库 – 将函数定义在文件里并在当前环境中导入那个文件。
|
||||
|
||||
假设我们已经在 utils.sh 中定义好了所有函数,接着在当前的环境下使用下面的命令导入函数:
|
||||
```
|
||||
$ source utils.sh
|
||||
|
||||
```
|
||||
|
||||
之后你就可以像调用其他 bash 命令那样执行库中任何的函数了。
|
||||
|
||||
### 结论
|
||||
|
||||
本文我们讨论了诸多实用的妙招,它将会提高你的效率。我希望这篇文章能够启发到你去创造自己的妙招。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linuxtechi.com/define-use-functions-linux-shell-script/
|
||||
|
||||
作者:[Pradeep Kumar][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[LuuMing](https://github.com/LuuMing)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.linuxtechi.com/author/pradeep/
|
||||
[1]:https://www.linuxtechi.com/wp-content/plugins/lazy-load/images/1x1.trans.gif
|
||||
[2]:https://www.linuxtechi.com/wp-content/uploads/2018/08/Functions-Linux-Shell-Script.jpg
|
Loading…
Reference in New Issue
Block a user