shell学习笔记-文件描述符及重定向
来源:互联网 发布:淘宝网运动器材 编辑:程序博客网 时间:2024/06/11 04:30
0 ——stdin 标准输入
1 ——stdout 标准输出
2 ——stderr 标准错误
将输出文本重定向或保存到一个文件:
$ echo "This is a sample test 1" > temp.txt
$ cat temp.txt
This is a sample test 1
$ echo "This is a sample test 2" >> temp.txt
$ cat temp.txt
This is a sample test 1
This is a sample test 2
$ echo "This is a sample test 2" > temp.txt
$ cat temp.txt
This is a sample test 2
注: “ > ” 会将etdout 输出到指定的文件中,但temp.txt中的内容首先会被清空。
“>>”将标准输出追加到目标文件中的尾部。
当使用重定向操作符时,输出内容不会在终端打印,而是被导向文件。
> = 1> ; >> == 1>>
/dev/null是一个特殊的设备文件,它接受到的任何数据都会被丢弃。null设备通常也被称为“黑洞”
$ ls + > out.txt
ls: cannot access +: No such file or directory
$ ls + 2> out.txt
$ cat out.txt
ls: cannot access +: No such file or directory
$ cmd 2>&1 output.txt -->等同于 cmd &> output.txt 将stderr转换成stdout,使得stderr和stdout都被重定向到同一个文件
bash: cmd: command not found...
Similar command is: 'mcd'
$ cat out.txt
ls: cannot access +: No such file or directory
$ cmd 2> /dev/null
$ cmd
bash: cmd: command not found...
Similar command is: 'mcd'
tee既可以将数据重定向到文件,还可以提供一份重定向数据的副本作为后续命令的stdin:
command |tee file1 file2
$ ls -tl
total 2
----------. 1 allen allen 47 Apr 27 01:07 out.txt
-rw-rw-r--. 1 allen allen 24 Apr 27 00:59 temp.txt
$ cat out.txt temp.txt
cat: out.txt: Permission denied
This is a sample test 2
$ cat out.txt temp.txt |tee a.txt |cat -n
cat: out.txt: Permission denied
1 This is a sample test 2
$ cat a.txt
This is a sample test 2
$
标准输出至a.txt,另外一份传递给 cat.
$ echo who is this |tee - "-"作为命令的文件名
who is this
who is this
补充:
借助重定向,我们可以用cat和管道|来指定我们自己的文件描述符:
$cat file |cmd
$cmd1 |cmd
将文件重定向到命令
$ cmd < file
- shell学习笔记-文件描述符及重定向
- Shell学习之文件的描述符和重定向
- Shell文件描述符和重定向
- 文件描述符及其重定向的学习笔记
- 04 Linux Shell 文件描述符 及 stdin stdout stderr 重定向
- Linux Shell 文件描述符 及 stdin stdout stderr 重定向
- C语言及shell描述符重定向
- shell学习整理(5)-玩转文件描述符及重定向
- shell: I/O 重定向,文件描述符
- linux shell基础1.4--文件描述符和重定向
- shell——文件描述符和重定向
- 重定向,文件描述符
- linux shell 编程笔记 - 文件重定向
- linux文件描述符和重定向
- 重定向 关闭文件描述符等
- Linux --- 文件描述符和重定向
- 文件描述符与重定向
- freopen,文件描述符重定向
- 早期练习正则与字符串的一些记录
- UIImage和UIColor的转换
- python中文乱码问题
- [BZOJ3694]最短路(并查集)
- spark-submit使用及说明
- shell学习笔记-文件描述符及重定向
- 判断js对象是否拥有某一个属性的js代码
- 用Navicat进行数据结构同步操作
- 已知圆心,半径,角度,求圆上的点坐标
- Github
- bzoj 1576: [Usaco2009 Jan]安全路经Travel(dijkstra堆优化+并查集)
- IOS UIViewController如何透明 与安卓的Activiy的区别,新手学习
- mysql truncate/delete/drop 删除表数据
- poj 2187(凸包+旋转卡壳)