提交译文

This commit is contained in:
Asche 2019-01-21 13:54:54 +08:00 committed by GitHub
parent 4b4d3b1bfb
commit 5b19b7df4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,44 +7,47 @@
[#]: via: (https://www.linux.com/blog/learn/2019/1/linux-tools-meaning-dot) [#]: via: (https://www.linux.com/blog/learn/2019/1/linux-tools-meaning-dot)
[#]: author: (Paul Brown https://www.linux.com/users/bro66) [#]: author: (Paul Brown https://www.linux.com/users/bro66)
Linux Tools: The Meaning of Dot Linux 工具: 点的含义
====== ======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/psychedelic-dot.jpg?itok=giKEHvwQ) ![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/psychedelic-dot.jpg?itok=giKEHvwQ)
Let's face it: writing one-liners and scripts using shell commands can be confusing. Many of the names of the tools at your disposal are far from obvious in terms of what they do ( _grep_ , _tee_ and _awk_ , anyone?) and, when you combine two or more, the resulting "sentence" looks like some kind of alien gobbledygook. 我们会面对这样的现实使用shell命令编写简短的脚本可能会令人很困惑。很多任你使用的工具的名称与它们的实际功能相差甚远(_grep_ , _tee__awk_ ,还有吗?) 并且当你将两个或更多个组合起来时,产生的“句子”看起来像某种外星人官样文章。
None of the above is helped by the fact that many of the symbols you use to build a chain of instructions can mean different things depending on their context.
### Location, location, location 由于这样一个现实: 你用来编写一连串的指令所使用的符号标志根据你使用的场景有着不同的意义。所以它对我们上面的事并没有给予到帮助。
Take the humble dot (`.`) for example. Used with instructions that are expecting the name of a directory, it means "this directory" so this: ### 位置,位置,位置
就拿这个谦卑的点 (`.`) 为例。 它与接收一个目录名称的指令一起使用,表示“当前目录”:
``` ```
find . -name "*.jpg" find . -name "*.jpg"
``` ```
translates to " _find in this directory (and all its subdirectories) files that have names that end in`.jpg`_ ". 意思就是“_在当前目录包括子目录中寻找以`.jpg`结尾的文件_”
Both `ls .` and `cd .` act as expected, so they list and "change" to the current directory, respectively, although including the dot in these two cases is not necessary. `ls .` and `cd .`结果也同期望的那样,他们分别列举和“进入”到当前目录,虽然在这两种情况下点都是多余的。
Two dots, one after the other, in the same context (i.e., when your instruction is expecting a directory path) means " _the directory immediately above the current one_ ". If you are in _/home/your_directory_ and run
两个点一个紧接着另一个同样的场景下即当你的命令期望一个文件目录的时候表示“_当前目录的父目录_”。如果你当前在 _/home/your_directory_ 下并且运行
``` ```
cd .. cd ..
``` ```
you will be taken to _/home_. So, you may think this still kind of fits into the “dots represent nearby directories” narrative and is not complicated at all, right? 你就会进入到 _/home_。所以,你可能认为这仍然适合“点代表附近目录”的叙述,并且毫不复杂,对吧?
How about this, then? If you use a dot at the beginning of a directory or file, it means the directory or file will be hidden:
那这样会怎样呢?如果你在一个文件或目录的开头加上点,它表示这个文件或目录会被隐藏:
``` ```
$ touch somedir/file01.txt somedir/file02.txt somedir/.secretfile.txt $ touch somedir/file01.txt somedir/file02.txt somedir/.secretfile.txt
$ ls -l somedir/ $ ls -l somedir/
total 0 total 0
-rw-r--r-- 1 paul paul 0 Jan 13 19:57 file01.txt -rw-r--r-- 1 paul paul 0 Jan 13 19:57 file01.txt
-rw-r--r-- 1 paul paul 0 Jan 13 19:57 file02.txt -rw-r--r-- 1 paul paul 0 Jan 13 19:57 file02.txt
$ # Note how there is no .secretfile.txt in the listing above $ # 注意上面列举的文件中没有 .secretfile.txt
$ ls -la somedir/ $ ls -la somedir/
total 8 total 8
drwxr-xr-x 2 paul paul 4096 Jan 13 19:57 . drwxr-xr-x 2 paul paul 4096 Jan 13 19:57 .
@ -52,128 +55,138 @@ drwx------ 48 paul paul 4096 Jan 13 19:57 ..
-rw-r--r-- 1 paul paul 0 Jan 13 19:57 file01.txt -rw-r--r-- 1 paul paul 0 Jan 13 19:57 file01.txt
-rw-r--r-- 1 paul paul 0 Jan 13 19:57 file02.txt -rw-r--r-- 1 paul paul 0 Jan 13 19:57 file02.txt
-rw-r--r-- 1 paul paul 0 Jan 13 19:57 .secretfile.txt -rw-r--r-- 1 paul paul 0 Jan 13 19:57 .secretfile.txt
$ # The -a option tells ls to show "all" files, including the hidden ones $ # 这个 -a 选项告诉 ls 去展示"all" 文件, 包括那些隐藏的
``` ```
And then there's when you use `.` as a command. Yep! You heard me: `.` is a full-fledged command. It is a synonym of `source` and you use that to execute a file in the current shell, as opposed to running a script some other way (which usually mean Bash will spawn a new shell in which to run it). 然后就是你将`.`当作命令。是的,你听我说过:`.`是个羽翼丰满的命令。它是`source` 的代名词 所以在当前shell中你可以用它来执行一个文件而不是以某些方法去运行一个脚本文件这样通常指的是Bash会产生一个新的shell去运行它
Confused? Don't worry -- try this: Create a script called _myscript_ that contains the line
很困惑?别担心 -- 试试这个:创建一个名为 _myscript_ 的脚本,内容包含下面一行
``` ```
myvar="Hello" myvar="Hello"
``` ```
and execute it the regular way, that is, with `sh myscript` (or by making the script executable with `chmod a+x myscript` and then running `./myscript`). Now try and see the contents of `myvar` with `echo $myvar` (spoiler: You will get nothing). This is because, when your script plunks " _Hello_ " into `myvar`, it does so in a separate bash shell instance. When the script ends, the spawned instance disappears and control returns to the original shell, where `myvar` never even existed.
However, if you run _myscript_ like this: 然后通过常规的方法执行它,也就是用 `sh myscript`(或者通过 `chmod a+x myscript` 命令让它可执行,然后运行`./myscript`)。现在尝试并且观察`myvar` 的内容 ,通过`echo $myvar` 理所当然的你什么也得不到。那是因为当你的脚本赋值“_Hello_”给`myvar`时它是在一个隔离的bash shell实例中进行的。当脚本运行结束时这个新产生的实例会消失并且控制权转交给原来的shell而原来的shell甚至都不存在`myvar` 变量。
然而,如果你这样运行 _myscript_ :
``` ```
. myscript . myscript
``` ```
`echo $myvar` will print _Hello_ to the command line.
You will often use the `.` (or `source`) command after making changes to your _.bashrc_ file, [like when you need to expand your `PATH` variable][1]. You use `.` to make the changes available immediately in your current shell instance. `echo $myvar` 就会打印 _Hello_ 到命令行上。
### Double Trouble 当你的 _.bashrc_ 文件发生变化后,你经常会用到`.` (或 `source`) 命令, [就像当你要扩展 `PATH` 变量那样][1] 在你的当前shell实例中你使用`.`来让变化立即生效。
Just like the seemingly insignificant single dot has more than one meaning, so has the double dot. Apart from pointing to the parent of the current directory, the double dot (`..`) is also used to build sequences. ### 双重麻烦
Try this: 就像看似无关紧要的一个点有多个含义一样,两个点也是如此。除了指向当前目录的父级之外,两个点(`..`)也用于构建序列。
尝试下这个:
``` ```
echo {1..10} echo {1..10}
``` ```
It will print out the list of numbers from 1 to 10. In this context, `..` means " _starting with the value on my left, count up to the value on my right_ ". 它会打印出从1到10的序列。这种场景下`..` 表示 “_从左边的值开始计数到右边的值_”.
Now try this:
现在试下这个:
``` ```
echo {1..10..2} echo {1..10..2}
``` ```
You'll get _1 3 5 7 9_. The `..2` part of the command tells Bash to print the sequence, but not one by one, but two by two. In other words, you'll get all the odd numbers from 1 to 10. 你会得到 _1 3 5 7 9_ 。`..2`这部分命令告诉Bash输出这个序列不过不是每个相差1而是相差2。换句话说就是你会得到从1到10之间的奇数。
It works backwards, too:
反着它也仍然有效:
``` ```
echo {10..1..2} echo {10..1..2}
``` ```
You can also pad your numbers with 0s. Doing:
你也可以用多个0填充你的数字。这样
``` ```
echo {000..121..2} echo {000..121..2}
``` ```
will print out every even number from 0 to 121 like this: 会这样打印出从0到121之间的偶数
``` ```
000 002 004 006 ... 050 052 054 ... 116 118 120 000 002 004 006 ... 050 052 054 ... 116 118 120
``` ```
But how is this sequence-generating construct useful? Well, suppose one of your New Year's resolutions is to be more careful with your accounts. As part of that, you want to create directories in which to classify your digital invoices of the last 10 years:
不过这样的序列发生器怎样才有作用呢?当然,假设您的新年决心之一是对您的帐户更加谨慎。 作为其中一部分您需要创建目录以便对过去10年的数字发票进行分类
``` ```
mkdir {2009..2019}_Invoices mkdir {2009..2019}_Invoices
``` ```
Job done. 工作完成。
Or maybe you have a hundreds of numbered files, say, frames extracted from a video clip, and, for whatever reason, you want to remove only every third frame between the frames 43 and 61:
或者你可能有数百个带编号的文件比如从视频剪辑中提取的帧无论出于什么原因你只想删除第43帧和第61帧之间的每隔三帧
``` ```
rm frame_{043..61..3} rm frame_{043..61..3}
``` ```
It is likely that, if you have more than 100 frames, they will be named with padded 0s and look like this:
很可能如果你有超过100帧它们将以填充0命名如下所示
``` ```
frame_000 frame_001 frame_002 ... frame_000 frame_001 frame_002 ...
``` ```
Thats why you will use `043` in your command instead of just `43`. 那就是为什么你要用 `043` 在你的命令中,而不是`43` 的原因。
### Curly~Wurly ### Curly~Wurly
Truth be told, the magic of sequences lies not so much in the double dot as in the sorcery of the curly braces (`{}`). Look how it works for letters, too. Doing:
说实话,序列的神奇之处不在于双点,就像花括号(`{}`)的巫术一样。 看看它对于字母是如何工作的。这样做:
``` ```
touch file_{a..z}.txt touch file_{a..z}.txt
``` ```
creates the files _file_a.txt_ through _file_z.txt_.
You must be careful, however. Using a sequence like `{Z..a}` will run through a bunch of non-alphanumeric characters (glyphs that are neither numbers or letters) that live between the uppercase alphabet and the lowercase one. Some of these glyphs are unprintable or have a special meaning of their own. Using them to generate names of files could lead to a whole bevy of unexpected and potentially unpleasant effects. 创建了从 _file_a.txt_ 到 _file_z.txt_ 的文件。
One final thing worth pointing out about sequences encased between `{...}` is that they can also contain lists of strings:
但是,你必须小心。 使用像`{Z..a}`这样的序列将运行大量字母和小写字母之间的一堆非字母数字字符(既不是数字或字母的字形)。 其中一些字形是不可打印的或具有自己的特殊含义。 使用它们来生成文件名称可能会导致一系列意外和可能令人不快的影响。
最后一件值得指出的事:包围在`{...}`的序列,它们也可以包含字符串的列举:
``` ```
touch {blahg, splurg, mmmf}_file.txt touch {blahg, splurg, mmmf}_file.txt
``` ```
Creates _blahg_file.txt_ , _splurg_file.txt_ and _mmmf_file.txt_. 创建了 _blahg_file.txt_ , _splurg_file.txt_ and _mmmf_file.txt_.
Of course, in other contexts, the curly braces have different meanings (surprise!). But that is the stuff of another article.
### Conclusion 当然,在别的场景中,大括号也有不同的含义(惊喜!)。不过那是别的文章的内容了。
Bash and the utilities you can run within it have been shaped over decades by system administrators looking for ways to solve very particular problems. To say that sysadmins and their ways are their own breed of special would be an understatement. Consequently, as opposed to other languages, Bash was not designed to be user-friendly, easy or even logical. ### 总结
That doesn't mean it is not powerful -- quite the contrary. Bash's grammar and shell tools may be inconsistent and sprawling, but they also provide a dizzying range of ways to do everything you can possibly imagine. It is like having a toolbox where you can find everything from a power drill to a spoon, as well as a rubber duck, a roll of duct tape, and some nail clippers.
Apart from fascinating, it is also fun to discover all you can achieve directly from within the shell, so next time we will delve ever deeper into how you can build bigger and better Bash command lines. 系统管理员开发Bash和你可以在上面使用的工具集已有数十载他们仍在寻找着解决各种特殊问题的方法。要说系统管理员和他们的方式是他们自己的特殊品味将是轻描淡写。总而言之与其他语言相反Bash的设计并不是用户友好简单甚至合乎逻辑的。
Until then, have fun! 这并不意味着它不强大 -- 恰恰相反。 Bash的语法和shell工具可能不一致且庞大但它们也提供了一系列令人眼花缭乱的方法来完成您可能想象到的一切。 就像有一个工具箱,你可以找到从电钻到勺子的所有东西,以及橡皮鸭,一卷胶带和一些指甲钳。
除了引人入胜之外发现你可以直接在shell中实现的所有内容也很有趣所以下次我们将深入探讨如何构建更大更好的Bash命令行。
在那之前,玩得开心!
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
via: https://www.linux.com/blog/learn/2019/1/linux-tools-meaning-dot via: https://www.linux.com/blog/learn/2019/1/linux-tools-meaning-dot
作者:[Paul Brown][a] 作者:[Paul Brown][a]
选题:[lujun9972][b] 选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID) 译者:[asche910](https://github.com/asche910)
校对:[校对者ID](https://github.com/校对者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出