Merge pull request #12253 from HankChow/master

翻译完成 More About Angle Brackets in Bash.md
This commit is contained in:
Xingyu.Wang 2019-02-02 11:03:24 +08:00 committed by GitHub
commit 8e719fdbf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 88 deletions

View File

@ -1,88 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (HankChow)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (More About Angle Brackets in Bash)
[#]: via: (https://www.linux.com/blog/learn/2019/1/more-about-angle-brackets-bash)
[#]: author: (Paul Brown https://www.linux.com/users/bro66)
More About Angle Brackets in Bash
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/bash-angles.png?itok=mVFnxPzF)
In the previous article, we [introduced the subject of angle brackets][1] (`< >`) and demonstrated some of their uses. Here, we'll look at the topic from a few more angles. Let's dive right in.
You can use `<` to trick a tool into believing the output of a command is data from a file.
Let's say you are not sure your backup is complete, and you want to check that a certain directory contains all the files copied over from the original. You can try this:
```
diff <(ls /original/dir/) <(ls /backup/dir/)
```
[`diff`][2] is a tool that typically compares two text files line by line, looking for differences. Here it gets the output from two `ls` commands and treats them as if coming from a file and compares them as such.
Note that there is no space between the `<` and the `(...)`.
Running that on the original and backup of a directory where I save pretty pictures, I get:
```
diff <(ls /My/Pictures/) <(ls /My/backup/Pictures/) 5d4 < Dv7bIIeUUAAD1Fc.jpg:large.jpg
```
The `<` in the output is telling me that there is file ( _Dv7bIIeUUAAD1Fc.jpg:large.jpg_ ) on the left side of the comparison (in _/My/Pictures_ ) that is not on the right side of the comparison (in _/My/backup/Pictures_ ), which means copying over has failed for some reason. If `diff` didn't cough up any output, it would mean that the list of files were the same.
So, you may be wondering, if you can take the output of a command or command line, make it look like the contents of a file, and feed it to an instruction that is expecting a file, that means that in the _sorting by favorite actor_ example from above, you could've done away with the intermediate file and just piped the output from the loop into `sort`.
In short, yep! The line:
```
sort -r <(while read -r name surname films;do echo $films $name $surname ; done < CBactors)
```
does the trick nicely.
### Here string! Good string!
There is one more case for redirecting data using angle brackets (or arrows, or whatever you want to call them).
You may be familiar with the practice of passing variables to commands using `echo` and a pipe (`|`). Say you want to convert a variable containing a string to uppercase characters because... I don't know... YOU LIKE SHOUTING A LOT. You could do this:
```
myvar="Hello World" echo $myvar | tr '[:lower:]' '[:upper:]' HELLO WORLD
```
The [`tr`][3] command _tr_ anslates strings to different formats. In the example above, you are telling `tr` to change all the lowercase characters that come along in the string to uppercase characters.
It is important to know that you are not passing on the variable, but only its contents, that is, the string " _Hello World_ ". This is called the _here string_ , as in " _it is here, in this context, that we know what string we are dealing with_ ". But there is shorter, clearer, and all round better way of delivering _here strings_ to commands. Using
```
tr '[:lower:]' '[:upper:]' <<< $myvar
```
does the same thing with no need to use echo or a pipe. It also uses angle brackets, which is the whole obsessive point of this article.
### Conclusion
Again, Bash proves to give you lots of options with very little. I mean, who would've thunk that you could do so much with two simple characters like `<` and `>`?
The thing is we aren't done. There are plenty of more characters that bring meaning to chains of Bash instructions. Without some background, they can make shell commands look like gibberish. Hopefully, post by post, we can help you decipher them. Until next time!
--------------------------------------------------------------------------------
via: https://www.linux.com/blog/learn/2019/1/more-about-angle-brackets-bash
作者:[Paul Brown][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/bro66
[b]: https://github.com/lujun9972
[1]: https://www.linux.com/blog/learn/2019/1/understanding-angle-brackets-bash
[2]: https://linux.die.net/man/1/diff
[3]: https://linux.die.net/man/1/tr

View File

@ -0,0 +1,87 @@
[#]: collector: (lujun9972)
[#]: translator: (HankChow)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (More About Angle Brackets in Bash)
[#]: via: (https://www.linux.com/blog/learn/2019/1/more-about-angle-brackets-bash)
[#]: author: (Paul Brown https://www.linux.com/users/bro66)
Bash 中尖括号的更多用法
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/bash-angles.png?itok=mVFnxPzF)
在[上一篇文章][1]当中,我们介绍了尖括号(`<>`)以及它们的一些用法。在这篇文章,我们继续来深入探讨尖括号的更多其它用法。
通过使用 `<`,可以实现“欺骗”的效果,让其它命令认为某个命令的输出是一个文件。
例如,在进行备份文件的时候不确定备份是否完整,就需要去确认某个目录是否已经包含从原目录中复制过去的所有文件。你可以试一下这样操作:
```
diff <(ls /original/dir/) <(ls /backup/dir/)
```
[`diff`][2] 命令是一个逐行比较两个文件之间差异的工具。在上面的例子中,就使用了 `<``diff` 认为两个 `ls` 命令输出的结果都是文件,从而能够比较它们之间的差异。
要注意,在 `<``(...)` 之间是没有空格的。
我尝试在我的图片目录和它的备份目录执行上面的命令,输出的是以下结果:
```
diff <(ls /My/Pictures/) <(ls /My/backup/Pictures/) 5d4 < Dv7bIIeUUAAD1Fc.jpg:large.jpg
```
输出结果中的 `<` 表示 `Dv7bIIeUUAAD1Fc.jpg:large.jpg` 这个文件存在于左边的目录(`/My/Pictures`)但不存在于右边的目录(`/My/backup/Pictures`)中。也就是说,在备份过程中可能发生了问题,导致这个文件没有被成功备份。如果 `diff` 没有显示出任何输出结果,就表明两个目录中的文件是一致的。
看到这里你可能会想到,既然可以通过 `<` 将一些命令行的输出内容作为一个文件,提供给一个需要接受文件格式的命令,那么在上一篇文章的“最喜欢的演员排序”例子中,就可以省去中间的一些步骤,直接对输出内容执行 `sort` 操作了。
确实如此,这个例子可以简化成这样:
```
sort -r <(while read -r name surname films;do echo $films $name $surname ; done < CBactors)
```
### Here string
除此以外,尖括号的重定向功能还有另一种使用方式。
使用 `echo` 和管道(`|`)来传递变量的用法,相信大家都不陌生。假如想要把一个字符串变量转换为全大写形式,你可以这样做:
```
myvar="Hello World" echo $myvar | tr '[:lower:]' '[:upper:]' HELLO WORLD
```
[`tr`][3] 命令可以将一个字符串转换为某种格式。在上面的例子中,就使用了 `tr` 将字符串中的所有小写字母都转换为大写字母。
要理解的是,这个传递过程的重点不是变量,而是变量的值,也就是字符串 `Hello World`。这样的字符串叫做 here string含义是“这就是我们要处理的字符串”。但对于上面的例子还可以用更直观的方式的处理就像下面这样
```
tr '[:lower:]' '[:upper:]' <<< $myvar
```
这种简便方式并不需要使用到 `echo` 或者管道,而是使用了我们一直在说的尖括号。
### 总结
使用 `<``>` 这两个简单的符号原来可以实现这么多功能Bash 又一次为工作的灵活性提供了很多选择。
当然,我们的介绍还远远没有完结,因为还有很多别的符号可以为 Bash 命令带来更多便利。不过如果没有充分理解它们,充满符号的 Bash 命令看起来只会像是一堆乱码。接下来我会解读更多类似的 Bash 符号,下次见!
--------------------------------------------------------------------------------
via: https://www.linux.com/blog/learn/2019/1/more-about-angle-brackets-bash
作者:[Paul Brown][a]
选题:[lujun9972][b]
译者:[HankChow](https://github.com/HankChow)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/bro66
[b]: https://github.com/lujun9972
[1]: https://www.linux.com/blog/learn/2019/1/understanding-angle-brackets-bash
[2]: https://linux.die.net/man/1/diff
[3]: https://linux.die.net/man/1/tr