mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
commit
583c9bfcaa
@ -1,281 +0,0 @@
|
||||
[#]: subject: "Bash Shell Scripting for beginners (Part 2)"
|
||||
[#]: via: "https://fedoramagazine.org/bash-shell-scripting-for-beginners-part-2/"
|
||||
[#]: author: "Matthew Darnell https://fedoramagazine.org/author/zexcon/"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "unigeorge"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Bash Shell Scripting for beginners (Part 2)
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
Photo by [N Bandaru][2] on [Unsplash][3]
|
||||
|
||||
Welcome to part 2 of Bash Shell Scripting at a beginner level. This article will dive into some more unique aspects of bash scripting. It will continue to use familiar commands, with an explain of anything new, and cover standard output standard input, standard error, the “pipe”, and data redirection.
|
||||
|
||||
### Adding comments #
|
||||
|
||||
As your scripts get more complicated and functional you will need to add comments to remember what you were doing. If you share your scripts with others, comments will help them understand the thought process and what you intended for your script to do. From the last article recall there were mathematical equations. Some comments have been added in the new version. Notice that in the _learnToScript.sh_ file (reproduced below) the comments are the lines with the hash sign before them. When the script runs these lines do not appear.
|
||||
|
||||
```
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
#Let's pick up from our last article. We
|
||||
#learned how to use mathematical equations
|
||||
#in bash scripting.
|
||||
|
||||
echo $((5+3))
|
||||
echo $((5-3))
|
||||
echo $((5*3))
|
||||
echo $((5/3))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ./learnToScript.sh
|
||||
8
|
||||
2
|
||||
15
|
||||
1
|
||||
|
||||
```
|
||||
|
||||
### Pipe Operator |
|
||||
|
||||
We will use another tool called _grep_ to introduce the pipe operator.
|
||||
|
||||
> Grep searches one or more input files for lines containing a match to a specified pattern. By default, Grep outputs the matching lines.
|
||||
>
|
||||
> <https://www.gnu.org/software/grep/>
|
||||
|
||||
Paul W. Frields’ article in the Fedora Magazine provides a good background on _grep_.
|
||||
|
||||
> [Command line quick tips: Searching with grep][4]
|
||||
|
||||
You will find the pipe key above the Enter key. Enter it by pressing Shift + \\. (English Keyboard)
|
||||
|
||||
Now that you are all freshened up on grep, look at an example of the use of the pipe command. At the command line type in _ls -l | grep_ _learn_
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l | grep learn
|
||||
-rwxrw-rw-. 1 zexcon zexcon 70 Sep 17 10:10 learnToScript.sh
|
||||
|
||||
```
|
||||
|
||||
Normally the _ls -l_ command would provide a list of the files on your screen. Here the full results of the _ls_ _-l_ command are piped into the grep command which searches for the string _learn_. Think of the pipe command like a filter. A command is run, in this case _ls -l_, and the results are limited to the files inside your directory. These results are sent via the pipe command to _grep_ which searches for the work _learn_ and only that line appears.
|
||||
|
||||
Look at one more example to try and nail this home. The _less_ command will allow you to see the results of a command that would extend beyond one screen size. Here is a quick description from the man pages for the _less_ command.
|
||||
|
||||
> Less is a program similar to more(1), but which allows backward movement in the file as well as
|
||||
> forward movement. Also, less does not have to read the entire input file before starting, so
|
||||
> with large input files it starts up faster than text editors like vi(1). Less uses termcap (or
|
||||
> terminfo on some systems), so it can run on a variety of terminals. There is even limited sup‐
|
||||
> port for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top
|
||||
> of the screen are prefixed with a caret.)
|
||||
>
|
||||
> Fedora 34 Manual(man) Pages
|
||||
|
||||
So let’s see what it looks like utilizing the pipe and the _less_ command
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l /etc | less
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
total 1504
|
||||
drwxr-xr-x. 1 root root 126 Jul 7 17:46 abrt
|
||||
-rw-r--r--. 1 root root 18 Jul 7 16:04 adjtime
|
||||
-rw-r--r--. 1 root root 1529 Jun 23 2020 aliases
|
||||
drwxr-xr-x. 1 root root 70 Jul 7 17:47 alsa
|
||||
drwxr-xr-x. 1 root root 14 Apr 23 05:58 cron.d
|
||||
drwxr-xr-x. 1 root root 0 Jan 25 2021 cron.daily
|
||||
:
|
||||
:
|
||||
|
||||
```
|
||||
|
||||
The results have been trimmed, here, for readability. Use the arrow keys on the keyboard to scroll up or down. Unlike the command line, where you might miss the top of the results if they scroll off screen, you can control the display. To get out of the _less_ screen tap the _q_ key.
|
||||
|
||||
### Standard Output (stdout), >, >>, 1>, and 1>>
|
||||
|
||||
The output of a command preceding the > or >> is sent to a file whose name follows. Keep in mind that > and 1> have the same results since the 1 stands for stdout (the standard output). Stdout is assumed if it does not appear. The >> and 1>> will append the data to the end of the file. In each case (> or >>) the file is created if it does not exist.
|
||||
|
||||
As an example, say you want to watch the ping command output to see if it dropped a packet. Rather than sit and watch the console, redirect the output to a file. You can come back later and see if packets were dropped. Here is a test of the redirect using _>_.
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l ~ > learnToScriptOutput
|
||||
|
||||
```
|
||||
|
||||
This takes the normal results you see in the terminal (recall ~ is your home directory) and redirects it to the _learnToScriptOutput_ file. Did you notice that _learnToScriptOutput_ was never created but now the file exists? Kind of cool.
|
||||
|
||||
```
|
||||
|
||||
total 128
|
||||
drwxr-xr-x. 1 zexcon zexcon 268 Oct 1 16:02 Desktop
|
||||
drwxr-xr-x. 1 zexcon zexcon 80 Sep 16 08:53 Documents
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Oct 1 15:59 Downloads
|
||||
-rw-rw-r--. 1 zexcon zexcon 685 Oct 4 16:00 learnToScriptAllOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 23 Oct 4 12:42 learnToScriptInput
|
||||
-rw-rw-r--. 1 zexcon zexcon 0 Oct 4 16:42 learnToScriptOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 52 Oct 4 16:07 learnToScriptOutputError
|
||||
-rwxrw-rw-. 1 zexcon zexcon 477 Oct 4 15:01 learnToScript.sh
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Jul 7 16:04 Videos
|
||||
|
||||
```
|
||||
|
||||
### Standard Error (stderr), 2>, and 2>>
|
||||
|
||||
The error output of a command preceding the > or >> is sent to a file whose name follows. Keep in mind that 2> and 2>> have the same result but the 2>> will append the data to the end of the file. So what is the purpose of these? What if you only want to catch an error. Then the 2> or 2>> is here to help. The 2 indicates the output that would normally go to stderr (standard error). Now put this into practice by listing a non-existent file.
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l /etc/invalidTest 2> learnToScriptOutputError
|
||||
|
||||
```
|
||||
|
||||
This takes the error results and redirects it to the _learnToScriptOutputError_ file.
|
||||
|
||||
```
|
||||
|
||||
ls: cannot access '/etc/invalidTest': No such file or directory
|
||||
|
||||
```
|
||||
|
||||
### All Output &>, &>> and |&
|
||||
|
||||
If you are thinking, I don’t want to write both standard output (stdout) and standard error (stderr) to different files. You are in luck. In Bash 5 the preferred way to redirect both stdout and stderr to the same file is to use &> or, as you might guess, &>> to append to a file.
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l ~ &>> learnToScriptAllOutput
|
||||
[zexcon ~]$ ls -l /etc/invalidTest &>> learnToScriptAllOutput
|
||||
|
||||
```
|
||||
|
||||
After running these commands, the output of both appear in the same file without identifying error or a standard output.
|
||||
|
||||
```
|
||||
|
||||
total 128
|
||||
drwxr-xr-x. 1 zexcon zexcon 268 Oct 1 16:02 Desktop
|
||||
drwxr-xr-x. 1 zexcon zexcon 80 Sep 16 08:53 Documents
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Oct 1 15:59 Downloads
|
||||
-rw-rw-r--. 1 zexcon zexcon 685 Oct 4 16:00 learnToScriptAllOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 23 Oct 4 12:42 learnToScriptInput
|
||||
-rw-rw-r--. 1 zexcon zexcon 0 Oct 4 16:42 learnToScriptOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 52 Oct 4 16:07 learnToScriptOutputError
|
||||
-rwxrw-rw-. 1 zexcon zexcon 477 Oct 4 15:01 learnToScript.sh
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Jul 7 16:04 Videos
|
||||
ls: cannot access '/etc/invalidTest': No such file or directory
|
||||
|
||||
```
|
||||
|
||||
If you are working directly from the command line and looking to pipe all results to another command, you can use |& for this purpose.
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l |& grep learn
|
||||
-rw-rw-r--. 1 zexcon zexcon 1197 Oct 18 09:46 learnToScriptAllOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 343 Oct 14 10:47 learnToScriptError
|
||||
-rw-rw-r--. 1 zexcon zexcon 0 Oct 14 11:11 learnToScriptOut
|
||||
-rw-rw-r--. 1 zexcon zexcon 348 Oct 14 10:27 learnToScriptOutError
|
||||
-rwxr-x---. 1 zexcon zexcon 328 Oct 18 09:46 learnToScript.sh
|
||||
[zexcon ~]$
|
||||
|
||||
```
|
||||
|
||||
### Standard Input (stdin)
|
||||
|
||||
You have used standard input (stdin) numerous times throughout articles 1 and 2 since your keyboard uses standard input every time you type a key. To give a bit of a change to the usual “it’s your keyboard”, let’s use the _read_ command in a script. The _read_ command, used in the script below, does what it sounds like, reads standard input.
|
||||
|
||||
```
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
#Here we are asking a question to prompt the user for standard input. i.e.keyboard
|
||||
echo 'Please enter your name.'
|
||||
|
||||
#Here we are reading the standard input and assigning it to the variable name with the read command.
|
||||
read name
|
||||
|
||||
#We are now going back to standard output, by using echo and printing your name to the command line.
|
||||
echo "With standard input you have told me your name is: $name"
|
||||
|
||||
```
|
||||
|
||||
This example prompts for input via standard output, for information it obtains from standard input(keyboard), storing it in a variable called _name_ using _read_ and displays the value in _name_ via standard output.
|
||||
|
||||
```
|
||||
|
||||
[zexcon@fedora ~]$ ./learnToScript.sh
|
||||
Please enter your name.
|
||||
zexcon
|
||||
With standard input you have told me your name is: zexcon
|
||||
[zexcon@fedora ~]$
|
||||
|
||||
```
|
||||
|
||||
### Into the script…
|
||||
|
||||
Now put what has been learned in a script to see how it can be used. The following is a new version of the previous learnToScript.sh file. There are a few added lines. It uses the append options for standard output, standard error and both into one file. It will write the standard output into learnToScriptStandardOutput, standard error into learnToScriptStandardError and both output and error into learnToScriptAllOutput
|
||||
|
||||
```
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
#As we know this article is about scripting. So let's
|
||||
#use what we learned in a script.
|
||||
|
||||
#Let's get some information from the user and add it to our scripts with stanard input and read
|
||||
|
||||
echo "What is your name? "
|
||||
read name
|
||||
|
||||
|
||||
#Here standard output directed to append a file to learnToScirptStandardOutput
|
||||
echo "$name, this will take standard output with append >> and redirect to learnToScriptStandardOutput." 1>> learnToScriptStandardOutput
|
||||
|
||||
|
||||
#Here we are taking the standard error and appending it to learnToScriptStandardError but to see this we need to #create an error.
|
||||
eco "Standard error with append >> redirect to learnToScriptStandardError." 2>> learnToScriptStandardError
|
||||
|
||||
#Here we are going to create an error and a standard output and see they go to the same place.
|
||||
echo "Standard output with append >> redirect to learnToScriptAllOutput." &>> learnToScriptAllOutput
|
||||
eco "Standard error with append >> redirect to learnToScriptAllOutput." &>> learnToScriptAllOutput
|
||||
|
||||
```
|
||||
|
||||
This example creates three files in the same directory. The command _echo_ is intentionally typed incorrectly to generate an error. If you check out all three files, you will see one message in learnToScriptStandardOutput, one in learnToScriptStandardError and two in learnToScriptAllOutput. Also notice the script prompts for a name which it writes to the learnToScriptStandardOutput.
|
||||
|
||||
# Conclusion
|
||||
|
||||
At this point it should start to be clear that anything you can do on the command line you can also do in a script. When writing a script that others might use, documentation is extremely important. Continuing the dive into scripting, the standard output will make more sense as you will be the one generating them. Inside a script you can use the same things used from the command line. The next article will get into functions, loops and things that will continue to build on this foundation.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/bash-shell-scripting-for-beginners-part-2/
|
||||
|
||||
作者:[Matthew Darnell][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[unigeorge](https://github.com/unigeorge)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/zexcon/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2021/10/bash_shell_scripting_pt2-816x345.jpg
|
||||
[2]: https://unsplash.com/@nbandana?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[3]: https://unsplash.com/s/photos/shell-scripting?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[4]: https://fedoramagazine.org/command-line-quick-tips-searching-with-grep/
|
@ -0,0 +1,281 @@
|
||||
[#]: subject: "Bash Shell Scripting for beginners (Part 2)"
|
||||
[#]: via: "https://fedoramagazine.org/bash-shell-scripting-for-beginners-part-2/"
|
||||
[#]: author: "Matthew Darnell https://fedoramagazine.org/author/zexcon/"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "unigeorge"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Bash Shell 脚本新手指南(二)
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
Photo by [N Bandaru][2] on [Unsplash][3]
|
||||
|
||||
欢迎来到面向初学者的 Bash Shell 脚本知识第二部分。本篇将就 Bash 脚本一些更独特的方面进行深入探讨。我们会用到一些上篇中已经熟悉的命令(如果遇到新命令,会给出讲解),进而涵盖一些标准输出、标准输入、标准错误、“管道”和数据重定向的相关知识。
|
||||
|
||||
### 使用 # 添加注释
|
||||
|
||||
随着脚本变得愈加复杂和实用,我们需要添加注释,以便记住程序在做什么。如果与其他人分享你的脚本,注释也将帮助他们理解思考过程,以及更好理解你的脚本实现的功能。想一想上篇文章中的数学方程,我们在新版脚本中添加了一些注释。注意,在 _learnToScript.sh_ 文件(如下所示)中,注释是前面带有井号的行。当脚本运行时,这些注释行并不会出现。
|
||||
|
||||
```
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
#Let's pick up from our last article. We
|
||||
#learned how to use mathematical equations
|
||||
#in bash scripting.
|
||||
|
||||
echo $((5+3))
|
||||
echo $((5-3))
|
||||
echo $((5*3))
|
||||
echo $((5/3))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ./learnToScript.sh
|
||||
8
|
||||
2
|
||||
15
|
||||
1
|
||||
|
||||
```
|
||||
|
||||
### 管道符 |
|
||||
|
||||
我们将使用另一个名为 _grep_ 的工具来介绍管道运算符。
|
||||
|
||||
> Grep 可以在输入文件中搜索可以匹配指定模式的行。默认情况下,Grep 会输出相应的匹配行。
|
||||
>
|
||||
> <https://www.gnu.org/software/grep/>
|
||||
|
||||
Paul W. Frields 在 Fedora 杂志上的文章很好地介绍了关于 _grep_ 的知识。
|
||||
|
||||
> [命令行快小技巧:使用 grep 进行搜索][4]
|
||||
|
||||
管道键在键盘上位于 Enter 键上方,可以在英文状态下按 Shift + \\ 输入。
|
||||
|
||||
现在你已经略微熟悉了 grep,接下来看一个使用管道命令的示例。在命令行输入 _ls -l | grep_ _learn_
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l | grep learn
|
||||
-rwxrw-rw-. 1 zexcon zexcon 70 Sep 17 10:10 learnToScript.sh
|
||||
|
||||
```
|
||||
|
||||
通常 _ls -l_ 命令会在屏幕上显示文件列表。这里 _ls_ _-l_ 命令的完整结果通过管道传送到搜索字符串 _learn_ 的 grep 命令中。你可以将管道命令想象成一个过滤器。先运行一个命令(本例中为 _ls -l_,结果会给出目录中的文件),这些结果通过管道命令给到 _grep_,后者会在其中搜索 _learn_,并且只显示符合条件的目标行。
|
||||
|
||||
下面再看一个例子以巩固相关知识。_less_ 命令可以让用户查看超出一个屏幕尺寸的命令结果。以下是命令手册页中关于 _less_ 的简要说明。
|
||||
|
||||
> Less 是一个类似于 more 的程序,但它允许在文件中向后或向前
|
||||
> 进行翻页移动。此外,less 不必在开始之前读取整个输入文件,因此
|
||||
> 对于大型输入文件而言,它比 vi 等文本编辑器启动更快。该命令较少使用 termcap(或
|
||||
> 某些系统上的 terminfo),因此可以在各种终端上运行。甚至还在一定程度上支持
|
||||
> 用于硬拷贝终端的端口。(在硬拷贝终端上,显示在屏幕顶部的行
|
||||
> 会以插入符号为前缀。)
|
||||
>
|
||||
> Fedora 手册 34 页
|
||||
|
||||
下面让我们看看管道命令和 _less_ 命令结合使用会是什么样子。
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l /etc | less
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
total 1504
|
||||
drwxr-xr-x. 1 root root 126 Jul 7 17:46 abrt
|
||||
-rw-r--r--. 1 root root 18 Jul 7 16:04 adjtime
|
||||
-rw-r--r--. 1 root root 1529 Jun 23 2020 aliases
|
||||
drwxr-xr-x. 1 root root 70 Jul 7 17:47 alsa
|
||||
drwxr-xr-x. 1 root root 14 Apr 23 05:58 cron.d
|
||||
drwxr-xr-x. 1 root root 0 Jan 25 2021 cron.daily
|
||||
:
|
||||
:
|
||||
|
||||
```
|
||||
|
||||
为便于阅读,此处对结果进行了修剪。用户可以使用键盘上的箭头键向上或向下滚动,进而控制显示。如果使用命令行,结果超出屏幕的话,用户可能会看不到结果的开头行。要退出 _less_ 屏幕,只需点击 _q_ 键。
|
||||
|
||||
### 标准输出(stdout)重定向 >, >>, 1>, 1>>
|
||||
|
||||
> 或 >> 符号之前的命令输出结果,会被写入到紧跟的文件名对应的文件中。> 和 1> 具有相同的效果,因为 1 就代表着标准输出。如果不显式指定 1,则默认为标准输出。>> 和 1>> 将数据附加到文件的末尾。使用 > 或 >> 时,如果文件不存在,则会创建对应文件。
|
||||
|
||||
例如,如果你想查看 ping 命令的输出,以查看它是否丢弃了数据包。与其关注控制台,不如将输出结果重定向到文件中,这样你就可以稍后再回来查看数据包是否被丢弃。下面是使用 _>_ 的重定向测试。
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l ~ > learnToScriptOutput
|
||||
|
||||
```
|
||||
|
||||
该命令会获取本应输出到终端的结果(~ 代表家目录),并将其重定向到 _learnToScriptOutput_ 文件。注意,我们并未手动创建 _learnToScriptOutput_,系统会自动创建该文件。
|
||||
|
||||
```
|
||||
|
||||
total 128
|
||||
drwxr-xr-x. 1 zexcon zexcon 268 Oct 1 16:02 Desktop
|
||||
drwxr-xr-x. 1 zexcon zexcon 80 Sep 16 08:53 Documents
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Oct 1 15:59 Downloads
|
||||
-rw-rw-r--. 1 zexcon zexcon 685 Oct 4 16:00 learnToScriptAllOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 23 Oct 4 12:42 learnToScriptInput
|
||||
-rw-rw-r--. 1 zexcon zexcon 0 Oct 4 16:42 learnToScriptOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 52 Oct 4 16:07 learnToScriptOutputError
|
||||
-rwxrw-rw-. 1 zexcon zexcon 477 Oct 4 15:01 learnToScript.sh
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Jul 7 16:04 Videos
|
||||
|
||||
```
|
||||
|
||||
### 标准错误信息(stderr)重定向 2>, 2>>
|
||||
|
||||
> 或 >> 符号之前命令的错误信息输出,会被写入到紧跟的文件名对应的文件中。2> 和 2>> 具有相同的效果,但 2>> 是将数据追加到文件末尾。你可能会想,这有什么用?不妨假象一下用户只想捕获错误信息的场景,然后你就会意识到 2> 或 2>> 的作用。数字 2 表示本应输出到终端的标准错误信息输出。现在我们试着追踪一个不存在的文件,以试试这个知识点。
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l /etc/invalidTest 2> learnToScriptOutputError
|
||||
|
||||
```
|
||||
|
||||
这会生成错误信息,并将错误信息重定向输入到 _learnToScriptOutputError_ 文件中.
|
||||
|
||||
```
|
||||
|
||||
ls: cannot access '/etc/invalidTest': No such file or directory
|
||||
|
||||
```
|
||||
|
||||
### 所有输出重定向 &>, &>>, |&
|
||||
|
||||
如果你不想将标准输出(stdout)和标准错误信息(stderr)写入不同的文件,那么在 Bash 5 中,你可以使用 &> 将 stdout 和 stderr 重定向到同一个文件,或者使用 &>> 追加到文件末尾。
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l ~ &>> learnToScriptAllOutput
|
||||
[zexcon ~]$ ls -l /etc/invalidTest &>> learnToScriptAllOutput
|
||||
|
||||
```
|
||||
|
||||
运行这些命令后,两者的输出都会进入同一个文件中,而不会区分是错误信息还是标准输出。
|
||||
|
||||
```
|
||||
|
||||
total 128
|
||||
drwxr-xr-x. 1 zexcon zexcon 268 Oct 1 16:02 Desktop
|
||||
drwxr-xr-x. 1 zexcon zexcon 80 Sep 16 08:53 Documents
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Oct 1 15:59 Downloads
|
||||
-rw-rw-r--. 1 zexcon zexcon 685 Oct 4 16:00 learnToScriptAllOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 23 Oct 4 12:42 learnToScriptInput
|
||||
-rw-rw-r--. 1 zexcon zexcon 0 Oct 4 16:42 learnToScriptOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 52 Oct 4 16:07 learnToScriptOutputError
|
||||
-rwxrw-rw-. 1 zexcon zexcon 477 Oct 4 15:01 learnToScript.sh
|
||||
drwxr-xr-x. 1 zexcon zexcon 0 Jul 7 16:04 Videos
|
||||
ls: cannot access '/etc/invalidTest': No such file or directory
|
||||
|
||||
```
|
||||
|
||||
如果你直接使用命令行操作,并希望将所有结果通过管道传输到另一个命令,可以选择使用 |& 实现。
|
||||
|
||||
```
|
||||
|
||||
[zexcon ~]$ ls -l |& grep learn
|
||||
-rw-rw-r--. 1 zexcon zexcon 1197 Oct 18 09:46 learnToScriptAllOutput
|
||||
-rw-rw-r--. 1 zexcon zexcon 343 Oct 14 10:47 learnToScriptError
|
||||
-rw-rw-r--. 1 zexcon zexcon 0 Oct 14 11:11 learnToScriptOut
|
||||
-rw-rw-r--. 1 zexcon zexcon 348 Oct 14 10:27 learnToScriptOutError
|
||||
-rwxr-x---. 1 zexcon zexcon 328 Oct 18 09:46 learnToScript.sh
|
||||
[zexcon ~]$
|
||||
|
||||
```
|
||||
|
||||
### 标准输入 (stdin)
|
||||
|
||||
在本篇和上篇文章中,我们已经多次使用过标准输入 (stdin),因为在每次使用键盘输入时,我们都在使用标准输入。为了区别通常意义上的“键盘即标准输入”,这次我们尝试在脚本中使用 _read_ 命令。下面的脚本中就使用了 _read_ 命令,字面上就像“读取标准输入”。
|
||||
|
||||
```
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
#Here we are asking a question to prompt the user for standard input. i.e.keyboard
|
||||
echo 'Please enter your name.'
|
||||
|
||||
#Here we are reading the standard input and assigning it to the variable name with the read command.
|
||||
read name
|
||||
|
||||
#We are now going back to standard output, by using echo and printing your name to the command line.
|
||||
echo "With standard input you have told me your name is: $name"
|
||||
|
||||
```
|
||||
|
||||
这个示例通过标准输出给出提示,提醒用户输入信息,然后从标准输入(键盘)获取信息,使用 _read_ 将其存储在 _name_ 变量中,并通过标准输出显示处 _name_ 中的值。
|
||||
|
||||
```
|
||||
|
||||
[zexcon@fedora ~]$ ./learnToScript.sh
|
||||
Please enter your name.
|
||||
zexcon
|
||||
With standard input you have told me your name is: zexcon
|
||||
[zexcon@fedora ~]$
|
||||
|
||||
```
|
||||
|
||||
### 在脚本中使用
|
||||
|
||||
现在我们把学到的东西放入脚本中,学习一下如何实际应用。下面是增加了几行后的新版本 learnToScript.sh 文件。它用追加的方式将标准输出、标准错误信息,以及两者混合后的信息,分别写入到三个不同文件。它将标准输出写入 learnToScriptStandardOutput,标准错误信息写入 learnToScriptStandardError,二者共同都写入 learnToScriptAllOutput 文件。
|
||||
|
||||
```
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
#As we know this article is about scripting. So let's
|
||||
#use what we learned in a script.
|
||||
|
||||
#Let's get some information from the user and add it to our scripts with stanard input and read
|
||||
|
||||
echo "What is your name? "
|
||||
read name
|
||||
|
||||
|
||||
#Here standard output directed to append a file to learnToScirptStandardOutput
|
||||
echo "$name, this will take standard output with append >> and redirect to learnToScriptStandardOutput." 1>> learnToScriptStandardOutput
|
||||
|
||||
|
||||
#Here we are taking the standard error and appending it to learnToScriptStandardError but to see this we need to #create an error.
|
||||
eco "Standard error with append >> redirect to learnToScriptStandardError." 2>> learnToScriptStandardError
|
||||
|
||||
#Here we are going to create an error and a standard output and see they go to the same place.
|
||||
echo "Standard output with append >> redirect to learnToScriptAllOutput." &>> learnToScriptAllOutput
|
||||
eco "Standard error with append >> redirect to learnToScriptAllOutput." &>> learnToScriptAllOutput
|
||||
|
||||
```
|
||||
|
||||
脚本在同一目录中创建了三个文件。命令 _echo_ 故意输入错误(LCTT 译注:缺少了字母 h)以产生错误信息。如果查看三个文件,你会在 learnToScriptStandardOutput 中看到一条信息,在 learnToScriptStandardError 中看到一条信息,在 learnToScriptAllOutput 中看到两条信息。另外,该脚本还会再次提示输入的 name 值,再将其写入 learnToScriptStandardOutput 中。
|
||||
|
||||
# 结语
|
||||
|
||||
至此你应该能够明确,可以在命令行中执行的操作,都可以在脚本中执行。在编写可能供他人使用的脚本时,文档非常重要。如果继续深入研究脚本,标准输出会显得更有意义,因为你将会控制它们的生成。在脚本中,你可以与命令行中操作时应用相同的内容。下一篇文章我们会讨论函数、循环,以及在此基础上进一步构建的结构。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/bash-shell-scripting-for-beginners-part-2/
|
||||
|
||||
作者:[Matthew Darnell][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[unigeorge](https://github.com/unigeorge)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/zexcon/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2021/10/bash_shell_scripting_pt2-816x345.jpg
|
||||
[2]: https://unsplash.com/@nbandana?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[3]: https://unsplash.com/s/photos/shell-scripting?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[4]: https://fedoramagazine.org/command-line-quick-tips-searching-with-grep/
|
Loading…
Reference in New Issue
Block a user