1. 标准输入重定向(STDIN,文件描述符为0):默认从键盘输入,也可以从其他文件或命令中输入
2. 标准输出重定向(STDOUT,文件描述符为1):默认输出到屏幕
3. 错误输出重定向(STDERR,文件描述符为2):默认输出到屏幕
范例:比如我们分别查看两个文件的属性信息,其中第二个文件是不存在的,虽然针对这两个文件的操作都分别会在屏幕上输出一些数据信息,但这两个操作的差异其实很大:
[root@linuxprobe ~]# touch linuxprobe [root@linuxprobe ~]# ls -l linuxprobe total 0 drwxr-xr-x. 3 root root 14 Aug 12 23:13 a [root@linuxprobe ~]# ls -l xxxxxx ls: cannot access xxxxxx: No such file or directory |
输入重定向中用到的符号及其作用
符号 |
作用 |
命令 < 文件 |
将文件作为命令的标准输入 |
命令 << 分界符 |
从标准输入中读入,直到遇见分界符才停止 |
命令 < 文件1 > 文件2 |
将文件1作为命令的标准输入并将标准输出到文件2 |
输出重定向中用到的符号及其作用
符号 |
作用 |
命令 > 文件 |
将标准输出重定向到一个文件中(清空原有文件的数据) |
命令 2> 文件 |
将错误输出重定向到一个文件中(清空原有文件的数据) |
命令 >> 文件 |
将标准输出重定向到一个文件中(追加到原有内容的后面) |
命令 2>> 文件 |
将错误输出重定向到一个文件中(追加到原有内容的后面) |
命令 >> 文件 2>&1 或 命令 &>> 文件 |
将标准输出与错误输出共同写入到文件中(追加到原有内容的后面) |
对于重定向中的标准输出模式,可以省略文件描述符1不写,而错误输出模式的文件描述符2是必须要写的。
[root@linuxprobe ~]# man bash > readme.txt [root@linuxprobe ~]# cat readme.txt BASH(1) General Commands Manual BASH(1) NAME bash - GNU Bourne-Again Shell
SYNOPSIS bash [options] [file]
COPYRIGHT Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.
DESCRIPTION Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incor‐ porates useful features from the Korn and C shells (ksh and csh).
Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-conformant by default.
………………省略部分输出信息……………… |
尝试输出重定向技术中的覆盖写入与追加写入这两种不同模式带来的变化
范例:首先通过覆盖写入模式向readme.txt文件写入一行数据(该文件中包含上一个实验的man命令信息),然后再通过追加写入模式向文件再写入一次数据
[root@linuxprobe ~]# echo "Welcome to LinuxProbe.Com" > readme.txt [root@linuxprobe ~]# echo "Quality linux learning materials" >> readme.txt [root@linuxprobe ~]# cat readme.txt Welcome to LinuxProbe.Com Quality linux learning materials |
范例:标准输出和错误输出的区别
[root@linuxprobe ~]# ls -l linuxprobe total 0 drwxr-xr-x. 3 root root 14 Aug 14 00:42 a [root@linuxprobe ~]# ls -l linuxprobe > /root/stderr.txt [root@linuxprobe ~]# ls -l linuxprobe 2> /root/stderr.txt total 0 drwxr-xr-x. 3 root root 14 Aug 14 00:42 a |
范例:把命令的报错信息写入到文件
[root@linuxprobe ~]# ls -l xxxxxx ls: cannot access xxxxxx: No such file or directory [root@linuxprobe ~]# ls -l xxxxxx > /root/stderr.txt ls: cannot access xxxxxx: No such file or directory [root@linuxprobe ~]# ls -l xxxxxx 2> /root/stderr.txt [root@linuxprobe ~]# cat /root/stderr.txt ls: cannot access xxxxxx: No such file or directory |
范例:使用输入重定向把readme.txt文件导入给wc -l命令,统计一下文件中的内容行数
[root@linuxprobe ~]# wc -l < readme.txt 2 |
同时按下键盘上的Shift+\键即可输入管道符,其执行格式为“命令A | 命令B”
把下面这两条命令合并为一条:
找出被限制登录用户的命令是grep "/sbin/nologin" /etc/passwd;
统计文本行数的命令则是wc -l。
[root@linuxp |
首页 上一页 1 2 3 4 下一页 尾页 1/4/4 | |
【大 中 小】【打印】 【繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部】 | |
上一篇:KubeSphere CI/CD+GitLab+Harbor.. | 下一篇:Linux课程学习 第二课 |