标准输入输出

Standard Input Programs accept keyboard input via standard input, or stdin. In most cases, this is the data that comes into the computer from a keyboard.
Standard Output Text-mode programs send most data to their users via standard output (aka stdout), which is normally displayed on the screen, either in a full-screen text-mode session or in a GUI window such as an xterm.
Standard Error Linux provides a second type of output stream, known as standard error, or stderr. This output stream is intended to carry high-priority information such as error messages. Ordinarily, standard error is sent to the same output device as standard output, so you can’t easily tell them apart. You can redirect one independently of the other, though, which can be handy.

重定向操作

Redirection operator Effect
> Creates a new file containing standard output. If the specified file exists, it’s overwritten.
>> Appends standard output to the existing file. If the specified file doesn’t exist, it’s created.
2> Creates a new file containing standard error. If the specified file exists, it’s overwritten.
2>> Appends standard error to the existing file. If the specified file doesn’t exist, it’s created.
&> Creates a new file containing both standard output and standard error. If the specified file exists, it’s overwritten.
< Sends the contents of the specified file to be used as standard input.
<< Accepts text on the following lines as standard input.
<> Causes the specified file to be used for both standard input and standard output.

typing someprog << EOF causes someprog to accept input until it sees a line that contains only the string EOF (without even a space following it).
用法
cat << EOF >test.sh

tee

tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。Ordinarily, tee overwrites any files whose names you specify. If you want to append data to these files, pass the -a option to tee.

  1. [root@ip-172-16-1-245 ec2-user]# tee 1.txt
  2. 333
  3. 333
  4. ^C
  5. [root@ip-172-16-1-245 ec2-user]# cat 1.txt
  6. 333

Linux标准文件描述符

文件描述符 缩写 描述
0 STDIN 标准输入
1 STDOUT 标准输出
2 STDERR 标准错误输出

STDIN 代表了标准输入,也就是我们在shell 输入的内容,我们可以把标准输入指定到一个文件,那么就会从文件读新命令。
STDOUT 代表了标准输出,正常我们会输出到屏幕,也可以把标准输出指定到一个文件,这样输出的内容都会重定向到文件

https://ryanstutorials.net/linuxtutorial/piping.php

管道

  1. 管道命令符的作用也可以用一句话来概括“把前一个命令原本要输出到屏幕的标准正常数据当作是后一个命令的标准输入”。<br />管道命令实例
  1. [root@linux ~]# cat 1.txt | wc -l
  2. 1

在修改用户密码时,通常都需要输入两次密码以进行确认,这在编写自动化脚本时将成为一个非常致命的缺陷。通过把管道符和passwd命令的—stdin参数相结合,我们可以用一条命令来完成密码重置操作:

  1. [root@linuxprobe ~]# echo "linuxprobe" | passwd --stdin root
  2. Changing password for user root.
  3. passwd: all authentication tokens updated successfully.

通配符

    • 代表匹配零个或多个字符
  • ?代表匹配单个字符
  • [0-9] 代表匹配0-9之间单个数字的字符
  • [abc] 代表匹配a、b、c三个字符中的任意一个字符

转义字符

  • 反斜杠(\):使反斜杠后面的一个变量变为单纯的字符串。
  • 单引号(’’):转义其中所有的变量为单纯的字符串。
  • 双引号(””):保留其中的变量属性,不进行转义处理。
  • 反引号(``):把其中的命令执行后返回结果。