Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu.Wang 2018-05-03 21:17:56 +08:00
commit 01f6a497ee
12 changed files with 750 additions and 551 deletions

View File

@ -1,12 +1,13 @@
一个简单的命令行片段管理器
Pet一个简单的命令行片段管理器
=====
![](https://www.ostechnix.com/wp-content/uploads/2018/01/pet-6-720x340.png)
我们不可能记住所有的命令,对吧?是的。除了经常使用的命令之外,我们几乎不可能记住一些很少使用的长命令。这就是为什么需要一些外部工具来帮助我们在需要时找到命令。在过去,我们已经审查了两个有用的工具,名为 "Bashpast" 和 "Keep"。使用 Bashpast我们可以轻松地为 Linux 命令添加书签以便更轻松地重复调用。而且Keep 实用程序可以用来在终端中保留一些重要且冗长的命令,以便你可以按需使用它们。今天,我们将看到该系列中的另一个工具,以帮助你记住命令。现在向 "Pet" 打个招呼,这是一个用 Go 语言编写的简单的命令行代码管理器。
我们不可能记住所有的命令,对吧?是的。除了经常使用的命令之外,我们几乎不可能记住一些很少使用的长命令。这就是为什么需要一些外部工具来帮助我们在需要时找到命令。在过去,我们已经点评了两个有用的工具,名为 “Bashpast” 和 “Keep”。使用 Bashpast我们可以轻松地为 Linux 命令添加书签,以便更轻松地重复调用。而 Keep 实用程序可以用来在终端中保留一些重要且冗长的命令,以便你可以随时使用它们。今天,我们将看到该系列中的另一个工具,以帮助你记住命令。现在让我们认识一下 “Pet”这是一个用 Go 语言编写的简单的命令行代码管理器。
使用 Pet你可以
* 注册/添加你重要的冗长和复杂的命令片段。
* 注册/添加你重要的冗长和复杂的命令片段。
* 以交互方式来搜索保存的命令片段。
* 直接运行代码片段而无须一遍又一遍地输入。
* 轻松编辑保存的代码片段。
@ -14,68 +15,78 @@
* 在片段中使用变量
* 还有很多特性即将来临。
#### 安装 Pet 命令行接口代码管理器
### 安装 Pet 命令行接口代码管理器
由于它是用 Go 语言编写的,所以确保你在系统中已经安装了 Go。
安装 Go 后,从 [**Pet 发布页面**][3] 获取最新的二进制文件。
```
wget https://github.com/knqyf263/pet/releases/download/v0.2.4/pet_0.2.4_linux_amd64.zip
```
对于 32 位计算机:
```
wget https://github.com/knqyf263/pet/releases/download/v0.2.4/pet_0.2.4_linux_386.zip
```
解压下载的文件:
```
unzip pet_0.2.4_linux_amd64.zip
```
对于 32 位:
```
unzip pet_0.2.4_linux_386.zip
```
将 pet 二进制文件复制到 PATH**/usr/local/bin** 之类的)。
`pet` 二进制文件复制到 PATH`/usr/local/bin` 之类的)。
```
sudo cp pet /usr/local/bin/
```
最后,让它可以执行:
```
sudo chmod +x /usr/local/bin/pet
```
如果你使用的是基于 Arch 的系统,那么你可以使用任何 AUR 帮助工具从 AUR 安装它。
使用 [**Pacaur**][4]:
使用 [Pacaur][4]
```
pacaur -S pet-git
```
使用 [**Packer**][5]:
使用 [Packer][5]
```
packer -S pet-git
```
使用 [**Yaourt**][6]:
使用 [Yaourt][6]
```
yaourt -S pet-git
```
使用 [**Yay** :][7]
使用 [Yay][7]
```
yay -S pet-git
```
此外,你需要安装 **[fzf][8]** 或 [**peco**][9] 工具已启用交互式搜索。请参阅官方 GitHub 链接了解如何安装这些工具。
此外,你需要安装 [fzf][8] 或 [peco][9] 工具以启用交互式搜索。请参阅官方 GitHub 链接了解如何安装这些工具。
#### 用法
### 用法
运行没有任何参数的 `pet` 来查看可用命令和常规选项的列表。
运行没有任何参数的 'pet' 来查看可用命令和常规选项的列表。
```
$ pet
pet - Simple command-line snippet manager.
@ -103,21 +114,23 @@ Use "pet [command] --help" for more information about a command.
```
要查看特定命令的帮助部分,运行:
```
$ pet [command] --help
```
**配置 Pet**
它只适用于默认值。但是,你可以更改默认目录来保存片段,选择要使用的选择器 (fzf 或 peco),默认文本编辑器编辑片段,添加 GIST id 详细信息等。
#### 配置 Pet
默认配置其实工作的挺好。但是你可以更改保存片段的默认目录选择要使用的选择器fzf 或 peco编辑片段的默认文本编辑器添加 GIST id 详细信息等。
要配置 Pet运行
```
$ pet configure
```
该命令将在默认的文本编辑器中打开默认配置(例如我是 **vim**),根据你的要求更改或编辑特定值。
该命令将在默认的文本编辑器中打开默认配置(例如我是 vim根据你的要求更改或编辑特定值。
```
[General]
snippetfile = "/home/sk/.config/pet/snippet.toml"
@ -133,24 +146,27 @@ $ pet configure
~
```
**创建片段**
#### 创建片段
为了创建一个新的片段,运行:
```
$ pet new
```
添加命令和描述,然后按下 ENTER 键保存它。
添加命令和描述,然后按下回车键保存它。
```
Command> echo 'Hell1o, Welcome1 2to OSTechNix4' | tr -d '1-9'
Description> Remove numbers from output.
```
[![][10]][11]
![][11]
这是一个简单的命令,用于从 echo 命令输出中删除所有数字。你可以很轻松地记住它。但是,如果你很少使用它,几天后你可能会完全忘记它。当然,我们可以使用 "CTRL+r" 搜索历史记录,但 "Pet" 会更容易。另外Pet 可以帮助你添加任意数量的条目。
这是一个简单的命令,用于从 `echo` 命令输出中删除所有数字。你可以很轻松地记住它。但是,如果你很少使用它,几天后你可能会完全忘记它。当然,我们可以使用 `CTRL+R` 搜索历史记录,但 Pet 会更容易。另外Pet 可以帮助你添加任意数量的条目。
另一个很酷的功能是我们可以轻松添加以前的命令。为此,在你的 `.bashrc``.zshrc` 文件中添加以下行。
另一个很酷的功能是我们可以轻松添加以前的命令。为此,在你的 **.bashrc** 或 **.zshrc** 文件中添加以下行。
```
function prev() {
PREV=$(fc -lrn | head -n 1)
@ -159,46 +175,53 @@ function prev() {
```
执行以下命令来使保存的更改生效。
```
source .bashrc
```
或者
或者:
```
source .zshrc
```
现在,运行任何命令,例如:
```
$ cat Documents/ostechnix.txt | tr '|' '\n' | sort | tr '\n' '|' | sed "s/.$/\\n/g"
```
要添加上述命令,你不必使用 "pet new" 命令。只需要:
要添加上述命令,你不必使用 `pet new` 命令。只需要:
```
$ prev
```
将说明添加到命令代码片段中,然后按下 ENTER 键保存。
将说明添加到该命令代码片段中,然后按下回车键保存。
![][12]
**片段列表**
#### 片段列表
要查看保存的片段,运行:
```
$ pet list
```
![][13]
**编辑片段**
#### 编辑片段
如果你想编辑代码片段的描述或命令,运行:
如果你想编辑描述或代码片段的命令,运行:
```
$ pet edit
```
这将在你的默认文本编辑器中打开所有保存的代码片段,你可以根据需要编辑或更改片段。
```
[[snippets]]
description = "Remove numbers from output."
@ -211,33 +234,35 @@ $ pet edit
output = ""
```
**在片段中使用标签**
#### 在片段中使用标签
要将标签用于判断,使用下面的 `-t` 标志。
要将标签用于判断,使用下面的 **-t** 标志。
```
$ pet new -t
Command> echo 'Hell1o, Welcome1 2to OSTechNix4' | tr -d '1-9
Description> Remove numbers from output.
Tag> tr command examples
```
**执行片段**
#### 执行片段
要执行一个保存的片段,运行:
```
$ pet exec
```
从列表中选择你要运行的代码段,然后按 ENTER 键来运行它:
从列表中选择你要运行的代码段,然后按回车键来运行它:
![][14]
记住你需要安装 fzf 或 peco 才能使用此功能。
**寻找片段**
#### 寻找片段
如果你有很多要保存的片段,你可以使用字符串或关键词如 below.qjz 轻松搜索它们。
```
$ pet search
```
@ -246,40 +271,43 @@ $ pet search
![][15]
**同步片段**
#### 同步片段
首先,你需要获取访问令牌。转到此链接 <https://github.com/settings/tokens/new> 并创建访问令牌(只需要 "gist" 范围)。
首先,你需要获取访问令牌。转到此链接 <https://github.com/settings/tokens/new> 并创建访问令牌(只需要 “gist” 范围)。
使用以下命令来配置 Pet
```
$ pet configure
```
标记设置到 **[Gist]** 字段中的 **access_token**
令牌设置到 `[Gist]` 字段中的 `access_token`
设置完成后,你可以像下面一样将片段上传到 Gist。
```
$ pet sync -u
Gist ID: 2dfeeeg5f17e1170bf0c5612fb31a869
Upload success
```
你也可以在其他 PC 上下载片段。为此,编辑配置文件并在 **[Gist]** 中将 **Gist ID** 设置为 **gist_id**
你也可以在其他 PC 上下载片段。为此,编辑配置文件并在 `[Gist]` 中将 `gist_id` 设置为 GIST id。
之后,使用以下命令下载片段:
之后,下载片段使用以下命令:
```
$ pet sync
Download success
```
获取更多细节,参阅 help 选项:
获取更多细节,参阅帮助选项:
```
pet -h
```
或者
或者:
```
pet [command] -h
```
@ -289,14 +317,13 @@ pet [command] -h
干杯!
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/pet-simple-command-line-snippet-manager/
作者:[SK][a]
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -1,17 +1,17 @@
如何使用 Rsync 通过 SSH 恢复部分传输的文件
如何使用 rsync 通过 SSH 恢复部分传输的文件
======
![](https://www.ostechnix.com/wp-content/uploads/2016/02/Resume-Partially-Transferred-Files-Over-SSH-Using-Rsync.png)
由于诸如电源故障、网络故障或用户干预等各种原因,使用 SCP 命令通过 SSH 复制的大型文件可能会中断,取消或损坏。有一天,我将 Ubuntu 16.04 ISO 文件复制到我的远程系统。不幸的是断电了,网络连接立即丢失。结果么复制过程终止这只是一个简单的例子。Ubuntu ISO 并不是那么大,一旦电源恢复,我就可以重新启动复制过程。但在生产环境中,当你在传输大型文件时,你可能并不希望这样做。
由于诸如电源故障、网络故障或用户干预等各种原因,使用 `scp` 命令通过 SSH 复制的大型文件可能会中断、取消或损坏。有一天,我将 Ubuntu 16.04 ISO 文件复制到我的远程系统。不幸的是断电了,网络连接立即断了。结果么复制过程终止这只是一个简单的例子。Ubuntu ISO 并不是那么大,一旦电源恢复,我就可以重新启动复制过程。但在生产环境中,当你在传输大型文件时,你可能并不希望这样做。
而且,你不能总是使用 **scp** 命令恢复被中止的进度。因为,如果你这样做,它只会覆盖现有的文件。这时你会怎么做?别担心!这是 **Rsync** 派上用场的地方Rsync 可以帮助你恢复中断的复制或下载过程。对于那些好奇的人Rsync 是一个快速、多功能的文件复制程序,可用于复制和传输远程和本地系统中的文件或文件夹。
而且,你不能继续使用 `scp` 命令恢复被中止的进度。因为,如果你这样做,它只会覆盖现有的文件。这时你会怎么做?别担心!这是 `rsync` 派上用场的地方!`rsync` 可以帮助你恢复中断的复制或下载过程。对于那些好奇的人,`rsync` 是一个快速、多功能的文件复制程序,可用于复制和传输远程和本地系统中的文件或文件夹。
它提供了大量控制其行为的每个方面的选项,并允许非常灵活地指定要复制的一组文件。它以增量传输算法而闻名,它通过仅发送源文件和目标中现有文件之间的差异来减少通过网络发送的数据量。 Rsync 广泛用于备份和镜像,以及日常使用中改进的复制命令。
它提供了大量控制其各种行为的选项,并允许非常灵活地指定要复制的一组文件。它以增量传输算法而闻名,它通过仅发送源文件和目标中现有文件之间的差异来减少通过网络发送的数据量。 `rsync` 广泛用于备份和镜像,以及日常使用中改进的复制命令。
就像 SCP 一样rsync 也会通过 SSH 复制文件。如果你想通过 SSH 下载或传输大文件和文件夹,我建议您使用 rsync。请注意**应该在两边都安装 rsync**(远程和本地系统)来恢复部分传输的文件。
就像 `scp` 一样,`rsync` 也会通过 SSH 复制文件。如果你想通过 SSH 下载或传输大文件和文件夹,我建议您使用 `rsync`。请注意,应该在两边(远程和本地系统)都安装 `rsync` 来恢复部分传输的文件。
### 使用 Rsync 恢复部分传输的文件
### 使用 rsync 恢复部分传输的文件
好吧,让我给你看一个例子。我将使用命令将 Ubuntu 16.04 ISO 从本地系统复制到远程系统:
@ -21,33 +21,32 @@ $ scp Soft_Backup/OS\ Images/Linux/ubuntu-16.04-desktop-amd64.iso sk@192.168.43.
这里,
* **sk**是我的远程系统的用户名
* **192.168.43.2** 是远程机器的 IP 地址。
* `sk`是我的远程系统的用户名
* `192.168.43.2` 是远程机器的 IP 地址。
现在,我按下 `CTRL+C` 结束它。
现在,我按下 **CTRL+c** 结束它。
**示例输出:**
示例输出:
```
sk@192.168.43.2's password:
ubuntu-16.04-desktop-amd64.iso 26% 372MB 26.2MB/s 00:39 ETA^c
```
[![][1]][2]
![][2]
正如你在上面的输出中看到的,当它达到 26 时,我终止了复制过程。
如果我重新运行上面的命令,它只会覆盖现有的文件。换句话说,复制过程不会在我断开的地方恢复。
为了恢复复制过程,我们可以使用 **rsync** 命令,如下所示。
为了恢复复制过程,我们可以使用 `rsync` 命令,如下所示。
```
$ rsync -P -rsh=ssh Soft_Backup/OS\ Images/Linux/ubuntu-16.04-desktop-amd64.iso sk@192.168.43.2:/home/sk/
```
**示例输出:**
示例输出:
```
sk@192.168.1.103's password:
sending incremental file list
@ -55,14 +54,15 @@ ubuntu-16.04-desktop-amd64.iso
                   380.56M 26% 41.05MB/s 0:00:25
```
[![][1]][4]
![][4]
看见了吗?现在,复制过程在我们之前断开的地方恢复了。你也可以像下面那样使用 `-partial` 而不是 `-P` 参数。
看见了吗?现在,复制过程在我们之前断开的地方恢复了。你也可以像下面那样使用 “-partial” 而不是 “-P” 参数。
```
$ rsync --partial -rsh=ssh Soft_Backup/OS\ Images/Linux/ubuntu-16.04-desktop-amd64.iso sk@192.168.43.2:/home/sk/
```
这里,参数 “-partial” 或 “-P” 告诉 rsync 命令保留部分下载的文件并恢复进度。
这里,参数 `-partial``-P` 告诉 `rsync` 命令保留部分下载的文件并恢复进度。
或者,我们也可以使用以下命令通过 SSH 恢复部分传输的文件。
@ -76,26 +76,24 @@ $ rsync -avP Soft_Backup/OS\ Images/Linux/ubuntu-16.04-desktop-amd64.iso sk@192.
rsync -av --partial Soft_Backup/OS\ Images/Linux/ubuntu-16.04-desktop-amd64.iso sk@192.168.43.2:/home/sk/
```
就是这样了。你现在知道如何使用 rsync 命令恢复取消、中断和部分下载的文件。正如你所看到的,它也不是那么难。如果两个系统都安装了 rsync我们可以轻松地通过上面描述的那样恢复复制进度。
就是这样了。你现在知道如何使用 `rsync` 命令恢复取消、中断和部分下载的文件。正如你所看到的,它也不是那么难。如果两个系统都安装了 `rsync`,我们可以轻松地通过上面描述的那样恢复复制进度。
如果你觉得本教程有帮助,请在你的社交、专业网络上分享,并支持 OSTechNix。还有更多的好东西。敬请关注!
如果你觉得本教程有帮助,请在你的社交、专业网络上分享,并支持我们。还有更多的好东西。敬请关注!
干杯!
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/how-to-resume-partially-downloaded-or-transferred-files-using-rsync/
作者:[SK][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.ostechnix.com/author/sk/
[1]:data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]:http://www.ostechnix.com/wp-content/uploads/2016/02/scp.png ()
[2]:http://www.ostechnix.com/wp-content/uploads/2016/02/scp.png
[3]:/cdn-cgi/l/email-protection
[4]:http://www.ostechnix.com/wp-content/uploads/2016/02/rsync.png ()
[4]:http://www.ostechnix.com/wp-content/uploads/2016/02/rsync.png

View File

@ -1,7 +1,10 @@
对于 Linux 新手来说 10 个基础的命令
每个 Linux 新手都应该知道的 10 个命令
=====
> 通过这 10 个基础命令开始掌握 Linux 命令行。
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/osdc_terminals.png?itok=CfBqYBah)
你可能认为你是 Linux 新手,但实际上并不是。全球互联网用户有 [3.74 亿][1],他们都以某种方式使用 Linux因为 Linux 服务器占据了互联网的 90%。大多数现代路由器运行 Linux 或 Unix[TOP500 超级计算机][2] 也依赖于 Linux。如果你拥有一台 Android 智能手机,那么你的操作系统就是由 Linux 内核构建的。
换句话说Linux 无处不在。
@ -10,118 +13,124 @@
下面是你需要知道的基本的 Linux 命令。每一个都很简单,也很容易记住。换句话说,你不必成为比尔盖茨就能理解它们。
### 1\. ls
### 1 ls
你可能会想:“这是什么东西?”不,那不是一个印刷错误 - 我真的打算输入一个小写的 l。`ls`,或者 “list,” 是你需要知道的使用 Linux CLI 的第一个命令。这个 list 命令在 Linux 终端中运行,以显示在相应文件系统下归档的所有主要目录。例如,这个命令:
你可能会想:“这是is什么东西?”不,那不是一个印刷错误 —— 我真的打算输入一个小写的 l。`ls`,或者 “list” 是你需要知道的使用 Linux CLI 的第一个命令。这个 list 命令在 Linux 终端中运行,以显示在存放在相应文件系统下的所有主要目录。例如,这个命令:
`ls /applications`
```
ls /applications
```
显示存储在 applications 文件夹下的每个文件夹,你将使用它来查看文件、文件夹和目录。
显示存储在 `applications` 文件夹下的每个文件夹,你将使用它来查看文件、文件夹和目录。
显示所有隐藏的文件都可以使用命令 `ls -a`
### 2\. cd
### 2 cd
这个命令是你用来跳转(或“更改”)到一个目录的。它指导你如何从一个文件夹导航到另一个文件夹。假设你位于 Downloads 文件夹中,但你想到名为 Gym Playlist 的文件夹中,简单地输入 `cd Gym Playlist` 将不起作用,(译注:这应该是 Gym 目录下的 Playlist 文件夹)因为 shell 不会识别它,并会报告你正在查找的文件夹不存在。要跳转到那个文件夹,你需要包含一个反斜杠。改命令如下所示:
这个命令是你用来跳转(或“更改”)到一个目录的。它指导你如何从一个文件夹导航到另一个文件夹。假设你位于 `Downloads` 文件夹中,但你想到名为 `Gym Playlist` 的文件夹中,简单地输入 `cd Gym Playlist` 将不起作用,因为 shell 不会识别它,并会报告你正在查找的文件夹不存在LCTT 译注:这是因为目录名中有空格)。要跳转到那个文件夹,你需要包含一个反斜杠。改命令如下所示:
`cd Gym\ Playlist`
```
cd Gym\ Playlist
```
要从当前文件夹返回到上一个文件夹,你可以输入 `cd ..` 后跟着文件夹名称(译注:返回上一层目录不应该是 cd .. )。把这两个点想象成一个后退按钮。
要从当前文件夹返回到上一个文件夹,你可以在该文件夹输入 `cd ..`。把这两个点想象成一个后退按钮。
### 3\. mv
### 3 mv
该命令将文件从一个文件夹转移到另一个文件夹;`mv` 代表“移动”。你可以使用这个简单的命令,就像你把一个文件拖到 PC 上的一个文件夹一样。
例如,如果我想创建一个名为 `testfile` 的文件来演示所有基本的 Linux 命令,并且我想将它移动到我的 Documents 文件夹中,我将输入这个命令:
例如,如果我想创建一个名为 `testfile` 的文件来演示所有基本的 Linux 命令,并且我想将它移动到我的 `Documents` 文件夹中,我将输入这个命令:
`mv /home/sam/testfile /home/sam/Documents/`
```
mv /home/sam/testfile /home/sam/Documents/
```
命令的第一部分(`mv`)说我想移动一个文件,第二部分(`home/sam/testfile`)表示我想移动的文件,第三部分(`/home/sam/Documents/`)表示我希望传输文件的位置。
### 4\. 快捷键
### 4 快捷键
好吧,这不止一个命令,但我忍不住把它们都包括进来。为什么?因为它们能节省时间并避免经历头痛。
`CTRL+K` 从光标处剪切文本直至本行结束
`CTRL+Y` 粘贴文本
`CTRL+E` 将光标移到本行的末尾
`CTRL+A` 将光标移动到本行的开头
`ALT+F` 跳转到下一个空格处
`ALT+B` 回到之前的空格处
`ALT+Backspace` 删除前一个词
`CTRL+W` 将光标前一个词剪贴
`Shift+Insert` 将文本粘贴到终端中
`Ctrl+D` 注销
- `CTRL+K` 从光标处剪切文本直至本行结束
- `CTRL+Y` 粘贴文本
- `CTRL+E` 将光标移到本行的末尾
- `CTRL+A` 将光标移动到本行的开头
- `ALT+F` 跳转到下一个空格处
- `ALT+B` 回到前一个空格处
- `ALT+Backspace` 删除前一个词
- `CTRL+W` 剪切光标前一个词
- `Shift+Insert` 将文本粘贴到终端中
- `Ctrl+D` 注销
这些命令在许多方面都能派上用场。例如,假设你在命令行文本中拼错了一个单词:
`sudo apt-get intall programname`
```
sudo apt-get intall programname
```
你可能注意到 "insatll" 拼写错了,因此该命令无法工作。但是快捷键可以让你分容易回去修复它。如果我的光标在这一行的末尾,我可以按下两次 `ALT+B` 来将光标移动到下面用 `^` 符号标记的地方:
你可能注意到 `install` 拼写错了,因此该命令无法工作。但是快捷键可以让你很容易回去修复它。如果我的光标在这一行的末尾,我可以按下两次 `ALT+B` 来将光标移动到下面用 `^` 符号标记的地方:
`sudo apt-get^intall programname`
```
sudo apt-get^intall programname
```
现在,我们可以快速地添加字母 `s` 来修复 `install`,十分简单!
### 5\. mkdir
### 5 mkdir
这是你用来在 Linux 环境下创建目录或文件夹的命令。例如,如果你像我一样喜欢 DIY你可以输入 `mkdir DIY` 为你的 DIY 项目创建一个目录。
### 6\. at
### 6 at
如果你想在特定时间运行 Linux 命令,你可以将 `at` 添加到语句中。语法是 `at` 后面跟着你希望命令运行的日期和时间,然后命令提示符变为 `at>`,这样你就可以输入在上面指定的时间运行的命令。
例如:
`at 4:08 PM Sat`
`at> cowsay 'hello'`
`at> CTRL+D`
```
at 4:08 PM Sat
at> cowsay 'hello'
at> CTRL+D
```
这将会在周六下午 4:08 运行 cowsay 程序。
这将会在周六下午 4:08 运行 `cowsay` 程序。
### 7\. rmdir
### 7 rmdir
这个命令允许你通过 Linux CLI 删除一个目录。例如:
`rmdir testdirectory`
```
rmdir testdirectory
```
请记住,这个命令不会删除里面有文件的目录。这只在删除空目录时才起作用。
### 8\. rm
### 8 rm
如果你想删除文件,`rm` 命令就是你想要的。它可以删除文件和目录。要删除一个文件,键入 `rm testfile`,或者删除一个目录和里面的文件,键入 `rm -r`
### 9\. touch
### 9 touch
`touch` 命令,也就是所谓的 "make file 命令",允许你使用 Linux CLI 创建新的、空的文件。很像 `mkdir` 创建目录,`touch` 会创建文件。例如,`touch testfile` 将会创建一个名为 testfile 的空文件。
`touch` 命令,也就是所谓的 “make file 的命令”,允许你使用 Linux CLI 创建新的、空的文件。很像 `mkdir` 创建目录,`touch` 会创建文件。例如,`touch testfile` 将会创建一个名为 testfile 的空文件。
### 10\. locate
### 10 locate
这个命令是你在 Linux 系统中用来查找文件的命令。就像在 Windows 中搜索一样,如果你忘了存储文件的位置或它的名字,这是非常有用的。
例如,如果你有一个关于区块链用例的文档,但是你忘了标题,你可以输入 `locate -blockchain` 或者通过用星号分隔单词来查找 "blockchain use cases",或者星号(`*`)。例如:
`locate -i*blockchain*use*cases*`
```
locate -i*blockchain*use*cases*
```
还有很多其他有用的 Linux CLI 命令,比如 `pkill` 命令,如果你开始关机但是你意识到你并不想这么做,那么这条命令很棒。但是这里描述的 10 个简单而有用的命令是你开始使用 Linux 命令行所需的基本知识。
--------------------------------------------------------------------------------
via: https://opensource.com/article/18/4/10-commands-new-linux-users
作者:[Sam Bocetta][a]
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -0,0 +1,66 @@
9 ways to improve collaboration between developers and designers
======
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/BUS_consensuscollab1.png?itok=ULQdGjlV)
This article was co-written with [Jason Porter][1].
Design is a crucial element in any software project. Sooner or later, the developers' reasons for writing all this code will be communicated to the designers, human beings who aren't as familiar with its inner workings as the development team.
Stereotypes exist on both side of the divide; engineers often expect designers to be flaky and irrational, while designers often expect engineers to be inflexible and demanding. The truth is considerably more nuanced and, at the end of the day, the fates of designers and developers are forever intertwined.
Here are nine things that can improve collaboration between the two.
### 1\. First, knock down the wall. Seriously.
There are loads of memes about the "wall of confusion" in just about every industry. No matter what else you do, the first step toward tearing down this wall is getting both sides to agree it needs to be gone. Once everyone agrees the existing processes aren't functioning optimally, you can pick and choose from the rest of these ideas to begin fixing the problems.
### 2\. Learn to empathize.
Before rolling up any sleeves to build better communication, take a break. This is a great junction point for team building. A time to recognize that we're all people, we all have strengths and weaknesses, and most importantly, we're all on the same team. Discussions around workflows and productivity can become feisty, so it's crucial to build a foundation of trust and cooperation before diving on in.
### 3\. Recognize differences.
Designers and developers attack the same problem from different angles. Given a similar problem, designers will seek the solution with the biggest impact while developers will seek the solution with the least amount of waste. These two viewpoints do not have to be mutually exclusive. There is plenty of room for negotiation and compromise, and somewhere in the middle is where the end user receives the best experience possible.
### 4\. Embrace similarities.
This is all about workflow. CI/CD, scrum, agile, etc., are all basically saying the same thing: Ideate, iterate, investigate, and repeat. Iteration and reiteration are common denominators for both kinds of work. So instead of running a design cycle followed by a development cycle, it makes much more sense to run them concurrently and in tandem. Syncing cycles allows teams to communicate, collaborate, and influence each other every step of the way.
### 5\. Manage expectations.
All conflict can be distilled down to one simple idea: incompatible expectations. Therefore, an easy way to prevent systemic breakdowns is to manage expectations by ensuring that teams are thinking before talking and talking before doing. Setting expectations often evolves organically through everyday conversation. Forcing them to happen by having meetings can be counterproductive.
### 6\. Meet early and meet often.
Meeting once at the beginning of work and once at the end simply isn't enough. This doesn't mean you need daily or even weekly meetings. Setting a cadence for meetings can also be counterproductive. Let them happen whenever they're necessary. Great things can happen with impromptu meetings—even at the watercooler! If your team is distributed or has even one remote employee, video conferencing, text chat, or phone calls are all excellent ways to meet. It's important that everyone on the team has multiple ways to communicate with each other.
### 7\. Build your own lexicon.
Designers and developers sometimes have different terms for similar ideas. One person's card is another person's tile is a third person's box. Ultimately, the fit and accuracy of a term aren't as important as everyone's agreement to use the same term consistently.
### 8\. Make everyone a communication steward.
Everyone in the group is responsible for maintaining effective communication, regardless of how or when it happens. Each person should strive to say what they mean and mean what they say.
### 9\. Give a darn.
It only takes one member of a team to sabotage progress. Go all in. If every individual doesn't care about the product or the goal, there will be problems with motivation to make changes or continue the process.
This article is based on [Designers and developers: Finding common ground for effective collaboration][2], a talk the authors will be giving at [Red Hat Summit 2018][3], which will be held May 8-10 in San Francisco. [Register by May 7][3] to save US$ 500 off of registration. Use discount code **OPEN18** on the payment page to apply the discount.
--------------------------------------------------------------------------------
via: https://opensource.com/article/18/5/9-ways-improve-collaboration-developers-designers
作者:[Jason Brock][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]:https://opensource.com/users/jkbrock
[1]:https://opensource.com/users/lightguardjp
[2]:https://agenda.summit.redhat.com/SessionDetail.aspx?id=154267
[3]:https://www.redhat.com/en/summit/2018

View File

@ -1,3 +1,5 @@
pinewall translating
Kubernetes distributed application deployment with sample Face Recognition App
============================================================

View File

@ -1,349 +0,0 @@
pinewall translating
How to do math on the Linux command line
======
![](https://images.techhive.com/images/article/2014/12/math_blackboard-100534564-large.jpg)
Can you do math on the Linux command line? You sure can! In fact, there are quite a few commands that can make the process easy and some you might even find interesting. Let's look at some very useful commands and syntax for command line math.
### expr
First and probably the most obvious and commonly used command for performing mathematical calculations on the command line is the **expr** (expression) command. It can manage addition, subtraction, division, and multiplication. It can also be used to compare numbers. Here are some examples:
#### Incrementing a variable
```
$ count=0
$ count=`expr $count + 1`
$ echo $count
1
```
#### Performing a simple calculations
```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```
Notice that you have to use a \ character in front of * to avoid the syntax error. The % operator is for modulo calculations.
Here's a slightly more complex example:
```
participants=11
total=156
share=`expr $total / $participants`
remaining=`expr $total - $participants \* $share`
echo $share
14
echo $remaining
2
```
If we have 11 participants in some event and 156 prizes to distribute, each participant's fair share of the take is 14, leaving 2 in the pot.
#### Making comparisons
Now let's look at the logic for comparisons. These statements may look a little odd at first. They are not setting values, but only comparing the numbers. What **expr** is doing in the examples below is determining whether the statements are true. If the result is 1, the statement is true; otherwise, it's false.
```
$ expr 11 = 11
1
$ expr 11 = 12
0
```
Read them as "Does 11 equal 11?" and "Does 11 equal 12?" and you'll get used to how this works. Of course, no one would be asking if 11 equals 11 on the command line, but they might ask if $age equals 11.
```
$ age=11
$ expr $age = 11
1
```
If you put the numbers in quotes, you'd actually be doing a string comparison rather than a numeric one.
```
$ expr "11" = "11"
1
$ expr "eleven" = "11"
0
```
In the following examples, we're asking whether 10 is greater than 5 and, then, whether it's greater than 99.
```
$ expr 10 \> 5
1
$ expr 10 \> 99
0
```
Of course, having true comparisons resulting in 1 and false resulting in 0 goes against what we generally expect on Linux systems. The example below shows that using **expr** in this kind of context doesn't work because **if** works with the opposite orientation (0=true).
```
#!/bin/bash
echo -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read price
if [ `expr $price \> $cost` ]; then
echo "We make money"
else
echo "Don't sell it"
fi
```
Now, let's run this script:
```
$ ./checkPrice
Cost to us> 11.50
Price we're asking> 6
We make money
```
That sure isn't going to help with sales! With a small change, this would work as we'd expect:
```
#!/bin/bash
echo -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read price
if [ `expr $price \> $cost` == 1 ]; then
echo "We make money"
else
echo "Don't sell it"
fi
```
### factor
The **factor** command works just like you'd probably expect. You feed it a number, and it tells you what its factors are.
```
$ factor 111
111: 3 37
$ factor 134
134: 2 67
$ factor 17894
17894: 2 23 389
$ factor 1987
1987: 1987
```
NOTE: The factor command didn't get very far on factoring that last value because 1987 is a **prime number**.
### jot
The **jot** command allows you to create a list of numbers. Provide it with the number of values you want to see and the number that you want to start with.
```
$ jot 8 10
10
11
12
13
14
15
16
17
```
You can also use **jot** like this. Here we're asking it to decrease the numbers by telling it we want to stop when we get to 2:
```
$ jot 8 10 2
10
9
8
7
5
4
3
2
```
The **jot** command can be useful if you want to iterate through a series of numbers to create a list for some other purpose.
```
$ for i in `jot 7 17`; do echo April $i; done
April 17
April 18
April 19
April 20
April 21
April 22
April 23
```
### bc
The **bc** command is probably one of the best tools for doing calculations on the command line. Enter the calculation that you want performed, and pipe it to the command like this:
```
$ echo "123.4+5/6-(7.89*1.234)" | bc
113.664
```
Notice that **bc** doesn't shy away from precision and that the string you need to enter is fairly straightforward. It can also make comparisons, handle Booleans, and calculate square roots, sines, cosines, tangents, etc.
```
$ echo "sqrt(256)" | bc
16
$ echo "s(90)" | bc -l
.89399666360055789051
```
In fact, **bc** can even calculate pi. You decide how many decimal points you want to see:
```
$ echo "scale=5; 4*a(1)" | bc -l
3.14156
$ echo "scale=10; 4*a(1)" | bc -l
3.1415926532
$ echo "scale=20; 4*a(1)" | bc -l
3.14159265358979323844
$ echo "scale=40; 4*a(1)" | bc -l
3.1415926535897932384626433832795028841968
```
And **bc** isn't just for receiving data through pipes and sending answers back. You can also start it interactively and enter the calculations you want it to perform. Setting the scale (as shown below) determines how many decimal places you'll see.
```
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=2
3/4
.75
2/3
.66
quit
```
Using **bc** , you can also convert numbers between different bases. The **obase** setting determines the output base.
```
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase=16
16 <=== entered
10 <=== response
256 <=== entered
100 <=== response
quit
```
One of the easiest ways to convert between hex and decimal is to use **bc** like this:
```
$ echo "ibase=16; F2" | bc
242
$ echo "obase=16; 242" | bc
F2
```
In the first example above, we're converting from hex to decimal by setting the input base (ibase) to hex (base 16). In the second, we're doing the reverse by setting the outbut base (obase) to hex.
### Easy bash math
With sets of double-parentheses, we can do some easy math in bash. In the examples below, we create a variable and give it a value and then perform addition, decrement the result, and then square the remaining value.
```
$ ((e=11))
$ (( e = e + 7 ))
$ echo $e
18
$ ((e--))
$ echo $e
17
$ ((e=e**2))
$ echo $e
289
```
The arithmetic operators allow you to:
```
+ - Add and subtract
++ -- Increment and decrement
* / % Multiply, divide, find remainder
^ Get exponent
```
You can also use both logical and boolean operators:
```
$ ((x=11)); ((y=7))
$ if (( x > y )); then
> echo "x > y"
> fi
x > y
$ ((x=11)); ((y=7)); ((z=3))
$ if (( x > y )) >> (( y > z )); then
> echo "letters roll downhill"
> fi
letters roll downhill
```
or if you prefer ...
```
$ if [ x > y ] << [ y > z ]; then echo "letters roll downhill"; fi
letters roll downhill
```
Now let's raise 2 to the 3rd power:
```
$ echo "2 ^ 3"
2 ^ 3
$ echo "2 ^ 3" | bc
8
```
### Wrap-up
There are sure a lot of different ways to work with numbers and perform calculations on the command line on Linux systems. I hope you picked up a new trick or two by reading this post.
Join the Network World communities on [Facebook][1] and [LinkedIn][2] to comment on topics that are top of mind.
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3268964/linux/how-to-do-math-on-the-linux-command-line.html
作者:[Sandra Henry-Stocker][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
选题:[lujun9972](https://github.com/lujun9972)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
[1]:https://www.facebook.com/NetworkWorld/
[2]:https://www.linkedin.com/company/network-world

View File

@ -1,3 +1,5 @@
pinewall translating
Getting started with Anaconda Python for data science
======

View File

@ -1,82 +0,0 @@
translating---geekpi
A Perl module for better debugging
======
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/annoyingbugs.png?itok=ywFZ99Gs)
It's occasionally useful to have a block of Perl code that you use only for debugging or development tweaking. That's fine, but having blocks like this can be expensive to performance, particularly if the decision whether to execute it is made at runtime.
[Curtis "Ovid" Poe][1] recently wrote a module that can help with this problem: [Keyword::DEVELOPMENT][2]. The module utilizes Keyword::Simple and the pluggable keyword architecture introduced in Perl 5.012 to create a new keyword: DEVELOPMENT. It uses the value of the PERL_KEYWORD_DEVELOPMENT environment variable to determine whether or not a block of code is to be executed.
Using it couldn't be easier:
```
use Keyword::DEVELOPMENT;
       
sub doing_my_big_loop {
    my $self = shift;
    DEVELOPMENT {
        # insert expensive debugging code here!
    }
}Keyworddoing_my_big_loopDEVELOPMENT
```
At compile time, the code inside the DEVELOPMENT block is optimized away and simply doesn't exist.
Do you see the advantage here? Set up the PERL_KEYWORD_DEVELOPMENT environment variable to be true on your sandbox and false on your production environment, and valuable debugging tools can be committed to your code repo, always there when you need them.
You could also use this module, in the absence of a more evolved configuration management system, to handle variations in settings between production and development or test environments:
```
sub connect_to_my_database {
       
    my $dsn = "dbi:mysql:productiondb";
    my $user = "db_user";
    my $pass = "db_pass";
   
    DEVELOPMENT {
        # Override some of that config information
        $dsn = "dbi:mysql:developmentdb";
    }
   
    my $db_handle = DBI->connect($dsn, $user, $pass);
}connect_to_my_databaseDEVELOPMENTDBI
```
Later enhancement to this snippet would have you reading in configuration information from somewhere else, perhaps from a YAML or INI file, but I hope you see the utility here.
I looked at the source code for Keyword::DEVELOPMENT and spent about a half hour wondering, "Gosh, why didn't I think of that?" Once Keyword::Simple is installed, the module that Curtis has given us is surprisingly simple. It's an elegant solution to something I've needed in my own coding practice for a long time.
--------------------------------------------------------------------------------
via: https://opensource.com/article/18/4/perl-module-debugging-code
作者:[Ruth Holloway][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]:https://opensource.com/users/druthb
[1]:https://metacpan.org/author/OVID
[2]:https://metacpan.org/pod/release/OVID/Keyword-DEVELOPMENT-0.04/lib/Keyword/DEVELOPMENT.pm

View File

@ -1,3 +1,5 @@
translating---geekpi
Enhance your Python with an interactive shell
======
![](https://fedoramagazine.org/wp-content/uploads/2018/03/python-shells-816x345.jpg)

View File

@ -0,0 +1,100 @@
How To Use Vim Editor To Input Text Anywhere
======
![](https://www.ostechnix.com/wp-content/uploads/2018/05/vim-anywhere-720x340.png)
Howdy Vim users! Today, I have come up with a good news to all of you. Say hello to **Vim-anywhere** , a simple script that allows you to use the Vim editor to input text anywhere in your Linux box. That means you can simply invoke your favorite Vim editor, type whatever you want and paste the text on any application or on a website. The text will be available in your clipboard until you restart your system. This utility is absolutely useful for those who love to use the Vim keybindings often in non-vim environment.
### Install Vim-anywhere in Linux
The Vim-anywhere utility will work on any GNOME based (or derivatives) Linux distributions. Also, make sure you have installed the following prerequisites.
* Curl
* Git
* gVim
* xclip
For instance, you can those utilities in Ubuntu as shown below.
```
$ sudo apt install curl git vim-gnome xclip
```
Then, run the following command to install Vim-anywhere:
```
$ curl -fsSL https://raw.github.com/cknadler/vim-anywhere/master/install | bash
```
Vim-anywhere has been installed. Now let us see how to use it.
### Use Vim Editor To Input Text Anywhere
Let us say you need to create a word document. But youre much more comfortable using Vim editor than LibreOffice writer. No problem, this is where Vim-anywhere comes in handy. It automates the entire process. It simply invokes the Vim editor, so you can write whatever you want in it and paste it in the .doc file.
Let me show you an example. Open LibreOffice writer or any graphical text editor of your choice. Then, open Vim-anywhere. To do so, simply press **CTRL+ALT+V**. It will open the gVim editor. Press “i” to switch to interactive mode and input the text. Once done, save and close it by typing **:wq**.
![][2]
The text will be available in the clipboard until you restart the system. After you closed the editor, your previous application is refocused. Just press **CTRL+P** to paste the text in it.
![][3]
Its just an example. You can even use Vim-anywhere to write something on an annoying web form or any other applications. Once Vim-anywhere invoked, it will open a buffer. Close it and its contents are automatically copied to your clipboard and your previous application is refocused.
The vim-anywhere utility will create a temporary file in **/tmp/vim-anywhere** when invoked. These temporary files stick around until you restart your system, giving you a temporary history.
```
$ ls /tmp/vim-anywhere
```
You can re-open your most recent file using command:
```
$ vim $( ls /tmp/vim-anywhere | sort -r | head -n 1 )
```
**Update Vim-anywhere**
Run the following command to update Vim-anywhere:
```
$ ~/.vim-anywhere/update
```
**Change keyboard shotcut**
The default keybinding to invoke Vim-anywhere is CTRL+ALT+V. You can change it to any custom keybinding using gconf tool.
```
$ gconftool -t str --set /desktop/gnome/keybindings/vim-anywhere/binding <custom binding>
```
**Uninstall Vim-anywhere**
Some of you might think that opening Vim editor each time to input text and paste the text back to another application might be pointless and completely unnecessary.
If you dont find this utility useful, simply uninstall it using command:
```
$ ~/.vim-anywhere/uninstall
```
--------------------------------------------------------------------------------
via: https://www.ostechnix.com/how-to-use-vim-editor-to-input-text-anywhere/
作者:[SK][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]:https://www.ostechnix.com/author/sk/
[2]:http://www.ostechnix.com/wp-content/uploads/2018/05/vim-anywhere-1-1.png
[3]:http://www.ostechnix.com/wp-content/uploads/2018/05/vim-anywhere-2.png

View File

@ -0,0 +1,344 @@
Linux 命令行下的数学运算
======
![](https://images.techhive.com/images/article/2014/12/math_blackboard-100534564-large.jpg)
可以在 Linux 命令行下做数学运算吗?当然可以!事实上,有不少命令可以轻松完成这些操作,其中一些甚至让你大吃一惊。让我们来学习这些有用的数学运算命令或命令语法吧。
### expr
首先,对于在命令行使用命令进行数学运算,可能最容易想到、最常用的命令就是 **expr** (expression)。它可以完成四则运算,也可以用于比较大小。下面是几个例子:
#### 变量递增
```
$ count=0
$ count=`expr $count + 1`
$ echo $count
1
```
#### 完成简单运算
```
$ expr 11 + 123
134
$ expr 134 / 11
12
$ expr 134 - 11
123
$ expr 11 * 123
expr: syntax error <== oops!
$ expr 11 \* 123
1353
$ expr 20 % 3
2
```
注意,你需要在 * 运算符之前增加 \ 符号,避免语法错误。% 运算符用于取余运算。
下面是一个稍微复杂的例子:
```
participants=11
total=156
share=`expr $total / $participants`
remaining=`expr $total - $participants \* $share`
echo $share
14
echo $remaining
2
```
假设某个活动中有 11 位参与者,需要颁发的奖项总数为 156那么平均每个参与者获得 14 项奖项,额外剩余 2 个奖项。
#### 比较大小
下面让我们看一下比较大小的操作。从第一印象来看,语句看似有些怪异;这里并不是设置数值,而是进行数字大小比较。在本例中 **expr** 判断表达式是否为真:如果结果是 1那么表达式为真反之表达式为假。
```
$ expr 11 = 11
1
$ expr 11 = 12
0
```
请读作"11 是否等于 11"及"11 是否等于 12",你很快就会习惯这种写法。当然,我们不会在命令行上执行上述比较,可能的比较是 $age 是否等于 11。
```
$ age=11
$ expr $age = 11
1
```
如果将数字放到引号中间,那么你将进行字符串比较,而不是数值比较。
```
$ expr "11" = "11"
1
$ expr "eleven" = "11"
0
```
在本例中,我们判断 10 是否大于 5以及是否 大于 99。
```
$ expr 10 \> 5
1
$ expr 10 \> 99
0
```
的确,返回 1 和 0 分别代表比较的结果为真和假,我们一般预期在 Linux 上得到这个结果。在下面的例子中,按照上述逻辑使用 **expr** 并不正确,因为 **if** 的工作原理刚好相反,即 0 代表真。
```
#!/bin/bash
echo -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read price
if [ `expr $price \> $cost` ]; then
echo "We make money"
else
echo "Don't sell it"
fi
```
下面,我们运行这个脚本:
```
$ ./checkPrice
Cost to us> 11.50
Price we're asking> 6
We make money
```
这显然与我们预期不符!我们稍微修改一下,以便使其按我们预期工作:
```
#!/bin/bash
echo -n "Cost to us> "
read cost
echo -n "Price we're asking> "
read price
if [ `expr $price \> $cost` == 1 ]; then
echo "We make money"
else
echo "Don't sell it"
fi
```
### factor
**factor** 命令的功能基本与你预期相符。你给出一个数字,该命令会给出对应数字的因子。
```
$ factor 111
111: 3 37
$ factor 134
134: 2 67
$ factor 17894
17894: 2 23 389
$ factor 1987
1987: 1987
```
factor 命令对于最后一个数字没有返回很多,这是因为 1987 是一个 **质数**
### jot
**jot** 命令可以创建一系列数字。给定数字总数及起始数字即可。
```
$ jot 8 10
10
11
12
13
14
15
16
17
```
你也可以用如下方式使用 **jot**,这里我们要求递减至数字 2。
```
$ jot 8 10 2
10
9
8
7
5
4
3
2
```
**jot** 可以帮你构造一系列数字组成的列表,该列表可以用于其它任务。
```
$ for i in `jot 7 17`; do echo April $i; done
April 17
April 18
April 19
April 20
April 21
April 22
April 23
```
### bc
**bc** 基本上是命令行数学运算最佳工具之一。输入你想执行的运算,使用管道发送至该命令即可:
```
$ echo "123.4+5/6-(7.89*1.234)" | bc
113.664
```
可见 **bc** 并没有忽略精度,而且输入的字符串也相当直截了当。它还可以进行大小比较、处理布尔值、计算平方根、正弦、余弦和正切等。
```
$ echo "sqrt(256)" | bc
16
$ echo "s(90)" | bc -l
.89399666360055789051
```
事实上,**bc** 甚至可以计算 pi。你需要指定需要的精度。
```
$ echo "scale=5; 4*a(1)" | bc -l
3.14156
$ echo "scale=10; 4*a(1)" | bc -l
3.1415926532
$ echo "scale=20; 4*a(1)" | bc -l
3.14159265358979323844
$ echo "scale=40; 4*a(1)" | bc -l
3.1415926535897932384626433832795028841968
```
除了通过管道接收数据并返回结果,**bc**还可以交互式运行,输入你想执行的运算即可。本例中提到的 scale 设置可以指定有效数字的个数。
```
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=2
3/4
.75
2/3
.66
quit
```
你还可以使用 **bc** 完成数字进制转换。**obase** 用于设置输出的数字进制。
```
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase=16
16 <=== entered
10 <=== response
256 <=== entered
100 <=== response
quit
```
按如下方式使用 **bc** 也是完成十六进制与十进制转换的最简单方式之一:
```
$ echo "ibase=16; F2" | bc
242
$ echo "obase=16; 242" | bc
F2
```
在上面第一个例子中,我们将输入进制 (ibase) 设置为十六进制 (hex),完成十六进制到为十进制的转换。在第二个例子中,我们执行相反的操作,即将输出进制 (obase) 设置为十六进制。
### 简单的 bash 数学运算
通过使用双括号,我们可以在 bash 中完成简单的数学运算。在下面的例子中,我们创建一个变量,为变量赋值,然后依次执行加法、自减和平方。
```
$ ((e=11))
$ (( e = e + 7 ))
$ echo $e
18
$ ((e--))
$ echo $e
17
$ ((e=e**2))
$ echo $e
289
```
允许使用的运算符包括:
```
+ - 加法及减法
++ -- 自增与自减
* / % 乘法,除法及求余数
^ 指数运算
```
你还可以使用逻辑运算符和布尔运算符:
```
$ ((x=11)); ((y=7))
$ if (( x > y )); then
> echo "x > y"
> fi
x > y
$ ((x=11)); ((y=7)); ((z=3))
$ if (( x > y )) >> (( y > z )); then
> echo "letters roll downhill"
> fi
letters roll downhill
```
或者如下方式:
```
$ if [ x > y ] << [ y > z ]; then echo "letters roll downhill"; fi
letters roll downhill
```
下面计算 2 的 3 次幂:
```
$ echo "2 ^ 3"
2 ^ 3
$ echo "2 ^ 3" | bc
8
```
### 总结
在 Linux 系统中,有很多不同的命令行工具可以完成数字运算。希望你在读完本文之后,能掌握一两个新工具。
使用 [Facebook][1] 或 [LinkedIn][2] 加入 Network World 社区,点评你最喜爱的主题。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3268964/linux/how-to-do-math-on-the-linux-command-line.html
作者:[Sandra Henry-Stocker][a]
译者:[pinewall](https://github.com/pinewall)
校对:[校对者ID](https://github.com/校对者ID)
选题:[lujun9972](https://github.com/lujun9972)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
[1]:https://www.facebook.com/NetworkWorld/
[2]:https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,80 @@
一个更好的调试 Perl 模块
======
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/annoyingbugs.png?itok=ywFZ99Gs)
只有在调试或开发调整时才使用 Perl 代码块有时会很有用。这很好,但是这样的代码块可能会对性能产生很大的影响, 尤其是在运行时决定是否执行它。
[Curtis“Ovid”Poe][1] 编写了一个可以帮助解决这个问题的模块:[Keyword::DEVELOPMENT][2]。该模块利用 Keyword::Simple 和 Perl 5.012 中引入的可插入关键字架构来创建新的关键字DEVELOPMENT。它使用 PERL_KEYWORD_DEVELOPMENT 环境变量的值来确定是否要执行一段代码。
使用它并不容易:
```
use Keyword::DEVELOPMENT;
       
sub doing_my_big_loop {
    my $self = shift;
    DEVELOPMENT {
        # insert expensive debugging code here!
    }
}Keyworddoing_my_big_loopDEVELOPMENT
```
在编译时DEVELOPMENT 块内的代码已经被优化掉了,根本就不存在。
你看到好处了么?在沙盒中将 PERL_KEYWORD_DEVELOPMENT 环境变量设置为 true在生产环境设为 false并且可以将有价值的调试工具提交到你的代码库中在你需要的时候随时可用。
在缺乏高级配置管理的系统中,你也可以使用此模块来处理生产和开发或测试环境之间的设置差异:
```
sub connect_to_my_database {
       
    my $dsn = "dbi:mysql:productiondb";
    my $user = "db_user";
    my $pass = "db_pass";
   
    DEVELOPMENT {
        # Override some of that config information
        $dsn = "dbi:mysql:developmentdb";
    }
   
    my $db_handle = DBI->connect($dsn, $user, $pass);
}connect_to_my_databaseDEVELOPMENTDBI
```
稍后对此代码片段的增强使你能在其他地方,比如 YAML 或 INI 中读取配置信息,但我希望您能在此看到该工具。
我查看了关键字 Keyword::DEVELOPMENT 的源码,花了大约半小时研究,“天哪,我为什么没有想到这个?”安装 Keyword::Simple 后Curtis 给我们的模块就非常简单了。这是我长期以来在自己的编码实践中需要的一个优雅解决方案。
--------------------------------------------------------------------------------
via: https://opensource.com/article/18/4/perl-module-debugging-code
作者:[Ruth Holloway][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[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/druthb
[1]:https://metacpan.org/author/OVID
[2]:https://metacpan.org/pod/release/OVID/Keyword-DEVELOPMENT-0.04/lib/Keyword/DEVELOPMENT.pm