From 05955ce3dad0be7b4f8513802ca63d3b80e1deed Mon Sep 17 00:00:00 2001
From: HankChow <280630620@qq.com>
Date: Wed, 10 Apr 2019 21:48:59 +0800
Subject: [PATCH] hankchow translated
---
...6 Using Square Brackets in Bash- Part 1.md | 154 ------------------
...6 Using Square Brackets in Bash- Part 1.md | 149 +++++++++++++++++
2 files changed, 149 insertions(+), 154 deletions(-)
delete mode 100644 sources/tech/20190326 Using Square Brackets in Bash- Part 1.md
create mode 100644 translated/tech/20190326 Using Square Brackets in Bash- Part 1.md
diff --git a/sources/tech/20190326 Using Square Brackets in Bash- Part 1.md b/sources/tech/20190326 Using Square Brackets in Bash- Part 1.md
deleted file mode 100644
index ea54fdabed..0000000000
--- a/sources/tech/20190326 Using Square Brackets in Bash- Part 1.md
+++ /dev/null
@@ -1,154 +0,0 @@
-[#]: collector: (lujun9972)
-[#]: translator: (HankChow)
-[#]: reviewer: ( )
-[#]: publisher: ( )
-[#]: url: ( )
-[#]: subject: (Using Square Brackets in Bash: Part 1)
-[#]: via: (https://www.linux.com/blog/2019/3/using-square-brackets-bash-part-1)
-[#]: author: (Paul Brown https://www.linux.com/users/bro66)
-
-Using Square Brackets in Bash: Part 1
-======
-
-![square brackets][1]
-
-This tutorial tackle square brackets and how they are used in different contexts at the command line.
-
-[Creative Commons Zero][2]
-
-After taking a look at [how curly braces (`{}`) work on the command line][3], now it’s time to tackle brackets (`[]`) and see how they are used in different contexts.
-
-### Globbing
-
-The first and easiest use of square brackets is in _globbing_. You have probably used globbing before without knowing it. Think of all the times you have listed files of a certain type, say, you wanted to list JPEGs, but not PNGs:
-
-```
-ls *.jpg
-```
-
-Using wildcards to get all the results that fit a certain pattern is precisely what we call globbing.
-
-In the example above, the asterisk means " _zero or more characters_ ". There is another globbing wildcard, `?`, which means " _exactly one character_ ", so, while
-
-```
-ls d*k*
-```
-
-will list files called _darkly_ and _ducky_ (and _dark_ and _duck_ \-- remember `*` can also be zero characters),
-
-```
-ls d*k?
-```
-
-will not list _darkly_ (or _dark_ or _duck_ ), but it will list _ducky_.
-
-Square brackets are used in globbing for sets of characters. To see what this means, make directory in which to carry out tests, `cd` into it and create a bunch of files like this:
-
-```
-touch file0{0..9}{0..9}
-```
-
-(If you don't know why that works, [take a look at the last installment that explains curly braces `{}`][3]).
-
-This will create files _file000_ , _file001_ , _file002_ , etc., through _file097_ , _file098_ and _file099_.
-
-Then, to list the files in the 70s and 80s, you can do this:
-
-```
-ls file0[78]?
-```
-
-To list _file022_ , _file027_ , _file028_ , _file052_ , _file057_ , _file058_ , _file092_ , _file097_ , and _file98_ you can do this:
-
-```
-ls file0[259][278]
-```
-
-Of course, you can use globbing (and square brackets for sets) for more than just `ls`. You can use globbing with any other tool for listing, removing, moving, or copying files, although the last two may require a bit of lateral thinking.
-
-Let's say you want to create duplicates of files _file010_ through _file029_ and call the copies _archive010_ , _archive011_ , _archive012_ , etc..
-
-You can't do:
-
-```
-cp file0[12]? archive0[12]?
-```
-
-Because globbing is for matching against existing files and directories and the _archive..._ files don't exist yet.
-
-Doing this:
-
-```
-cp file0[12]? archive0[1..2][0..9]
-```
-
-won't work either, because `cp` doesn't let you copy many files to other many new files. Copying many files only works if you are copying them to a directory, so this:
-
-```
-mkdir archive
-
-cp file0[12]? archive
-```
-
-would work, but it would copy the files, using their same names, into a directory called _archive/_. This is not what you set out to do.
-
-However, if you look back at [the article on curly braces (`{}`)][3], you will remember how you can use `%` to lop off the end of a string contained in a variable.
-
-Of course, there is a way you can also lop of the beginning of string contained in a variable. Instead of `%`, you use `#`.
-
-For practice, you can try this:
-
-```
-myvar="Hello World"
-
-echo Goodbye Cruel ${myvar#Hello}
-```
-
-It prints " _Goodbye Cruel World_ " because `#Hello` gets rid of the _Hello_ part at the beginning of the string stored in `myvar`.
-
-You can use this feature alongside your globbing tools to make your _archive_ duplicates:
-
-```
-for i in file0[12]?;\
-
-do\
-
-cp $i archive${i#file};\
-
-done
-```
-
-The first line tells the Bash interpreter that you want to loop through all the files that contain the string _file0_ followed by the digits _1_ or _2_ , and then one other character, which can be anything. The second line `do` indicates that what follows is the instruction or list of instructions you want the interpreter to loop through.
-
-Line 3 is where the actually copying happens, and you use the contents of the loop variable _`i`_ **twice: First, straight out, as the first parameter of the `cp` command, and then you add _archive_ to its contents, while at the same time cutting of _file_. So, if _`i`_ contains, say, _file019_...
-
-```
-"archive" + "file019" - "file" = "archive019"
-```
-
-the `cp` line is expanded to this:
-
-```
-cp file019 archive019
-```
-
-Finally, notice how you can use the backslash `\` to split a chain of commands over several lines for clarity.
-
-In part two, we’ll look at more ways to use square brackets. Stay tuned.
-
---------------------------------------------------------------------------------
-
-via: https://www.linux.com/blog/2019/3/using-square-brackets-bash-part-1
-
-作者:[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/sites/lcom/files/styles/rendered_file/public/square-gabriele-diwald-475007-unsplash.jpg?itok=cKmysLfd (square brackets)
-[2]: https://www.linux.com/LICENSES/CATEGORY/CREATIVE-COMMONS-ZERO
-[3]: https://www.linux.com/blog/learn/2019/2/all-about-curly-braces-bash
diff --git a/translated/tech/20190326 Using Square Brackets in Bash- Part 1.md b/translated/tech/20190326 Using Square Brackets in Bash- Part 1.md
new file mode 100644
index 0000000000..b095e4d291
--- /dev/null
+++ b/translated/tech/20190326 Using Square Brackets in Bash- Part 1.md
@@ -0,0 +1,149 @@
+[#]: collector: (lujun9972)
+[#]: translator: (HankChow)
+[#]: reviewer: ( )
+[#]: publisher: ( )
+[#]: url: ( )
+[#]: subject: (Using Square Brackets in Bash: Part 1)
+[#]: via: (https://www.linux.com/blog/2019/3/using-square-brackets-bash-part-1)
+[#]: author: (Paul Brown https://www.linux.com/users/bro66)
+
+在 Bash 中使用方括号 [第 1 节]
+======
+
+![square brackets][1]
+
+> 这篇文章将要介绍方括号及其在命令行中的用法。
+
+看完[花括号在命令行中的用法][3]之后,现在我们继续来看方括号(`[]`)在上下文中是如何发挥作用的。
+
+### 通配
+
+方括号最简单的用法就是通配。你可能在知道“通配”这个概念之前就已经通过通配来匹配内容了,列出具有相同特征的多个文件就是一个很常见的场景,例如列出所有 JPEG 文件:
+
+```
+ls *.jpg
+```
+
+使用通配符来得到符合某个模式的所有内容,这个过程就叫通配。
+
+在上面的例子当中,星号(`*`)就代表“0 个或多个字符”。除此以外,还有代表“有且仅有一个字符”的问号(`?`)。因此
+
+```
+ls d*k*
+```
+
+可以列出 `darkly` 和 `ducky`,而且 `dark` 和 `duck` 也是可以被列出的,因为 `*` 可以匹配 0 个字符。而
+
+```
+ls d*k?
+```
+
+则只能列出 `ducky`,不会列出 `darkly`、`dark` 和 `duck`。
+
+方括号也可以用于通配。为了便于演示,可以创建一个用于测试的目录,并在这个目录下创建文件:
+
+```
+touch file0{0..9}{0..9}
+```
+
+(如果你还不清楚上面这个命令的原理,可以看一下[另一篇介绍花括号的文章][3])
+
+执行上面这个命令之后,就会创建 `file000`、`file001`、……、`file099` 这 100 个文件。
+
+如果要列出这些文件当中第二位数字是 7 或 8 的文件,可以执行:
+
+```
+ls file0[78]?
+```
+
+如果要列出 `file022`、`file027`、`file028`、`file052`、`file057`、`file058`、`file092`、`file097`、`file098`,可以执行:
+
+```
+ls file0[259][278]
+```
+
+当然,不仅仅是 `ls`,很多其它的命令行工具都可以使用方括号来进行通配操作。但在删除文件、移动文件、复制文件的过程中使用通配,你需要有一点横向思维。
+
+例如将 `file010` 到 `file029` 这 30 个文件复制成 `archive010` 到 `archive029` 这 30 个副本,不可以这样执行:
+
+```
+cp file0[12]? archive0[12]?
+```
+
+因为通配只能针对已有的文件,而 archive 开头的文件并不存在,不能进行通配。
+
+而这条命令
+
+```
+cp file0[12]? archive0[1..2][0..9]
+```
+
+也同样不行,因为 `cp` 并不允许将多个文件复制到多个文件。在复制多个文件的情况下,只能将多个文件复制到一个指定的目录下:
+
+```
+mkdir archive
+
+cp file0[12]? archive
+```
+
+这条命令是可以正常运行的,但它只会把这 30 个文件以同样的名称复制到 `archive/` 目录下,而这并不是我们想要的效果。
+
+如果你阅读过我[关于花括号的文章][3],你大概会记得可以使用 `%` 来截掉字符串的末尾部分,而使用 `#` 则可以截掉字符串的开头部分。
+
+例如:
+
+```
+myvar="Hello World"
+
+echo Goodbye Cruel ${myvar#Hello}
+```
+
+就会输出 `Goodbye Cruel World`,因为 `#Hello` 将 `myvar` 变量中开头的 `Hello` 去掉了。
+
+在通配的过程中,也可以使用这一个技巧。
+
+```
+for i in file0[12]?;\
+
+do\
+
+cp $i archive${i#file};\
+
+done
+```
+
+上面的第一行命令告诉 Bash 需要对所有 `file01` 开头或者 `file02` 开头,且后面只跟一个任意字符的文件进行操作,第二行的 `do` 和第四行的 `done` 代表需要对这些文件都执行这一块中的命令。
+
+第三行就是实际的复制操作了,这里使用了两次 `$i` 变量:第一次在 `cp` 命令中直接作为源文件的文件名使用,第二次则是截掉文件名开头的 `file` 部分,然后在开头补上一个 `archive`,也就是这样:
+
+```
+"archive" + "file019" - "file" = "archive019"
+```
+
+最终整个 `cp` 命令是这样的:
+
+```
+cp file019 archive019
+```
+
+最后,顺带说明一下反斜杠 `\` 的作用是将一条长命令拆分成多行,这样可以方便阅读。
+
+在下一节,我们会了解方括号的更多用法,敬请关注。
+
+--------------------------------------------------------------------------------
+
+via: https://www.linux.com/blog/2019/3/using-square-brackets-bash-part-1
+
+作者:[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/sites/lcom/files/styles/rendered_file/public/square-gabriele-diwald-475007-unsplash.jpg?itok=cKmysLfd "square brackets"
+[2]: https://www.linux.com/LICENSES/CATEGORY/CREATIVE-COMMONS-ZERO
+[3]: https://www.linux.com/blog/learn/2019/2/all-about-curly-braces-bash
+