shell输入输出重定向
2017-02-24 20:39
288 查看
给memcached添加日志的时候用到数据流重定向,所以打算总结一下。
每个linux命令运行都会打开三个文件:
1、标准输入文件(stdin):文件描述符为0,若不重定向会默认读取stdin文件。
2、标准输出文件(stdout):文件描述符为1,若不重定向会默认将输出信息写入stdout文件。
3、标准错误文件(stderr):文件描述符为2,若不重定向会默认将错误信息写入stderr文件。
实例
1、command > file 输出重定向--覆盖
2、command >> file 输出重定向--追加
3、command < file 输入重定向
4、文件描述符n > file
只将1输出到out文件, 则2直接显示在控制台上
只将2输出到out文件中,则1直接显示在控制台上
5、 n >& m (m,n为文件描述符)
6、n <& m(待补充)
7、<< tag 将 tag期间的内容作为输入(下例的tab为EOF)
命令 | 说明 |
---|---|
command > file | 将输出重定向到 file。 |
command < file | 将输入重定向到 file。 |
command >> file | 将输出以追加的方式重定向到 file。 |
n > file | 将文件描述符为 n 的文件以覆盖的方式重定向到 file。 |
n >> file | 将文件描述符为 n 的文件以追加的方式重定向到 file。 |
n >& m | 将输出文件 m 和 n 合并。 |
n <& m | 将输入文件 m 和 n 合并。 |
<< tag | 将开始标记 tag 和结束标记 tag 之间的内容作为输入。 |
1、标准输入文件(stdin):文件描述符为0,若不重定向会默认读取stdin文件。
2、标准输出文件(stdout):文件描述符为1,若不重定向会默认将输出信息写入stdout文件。
3、标准错误文件(stderr):文件描述符为2,若不重定向会默认将错误信息写入stderr文件。
实例
1、command > file 输出重定向--覆盖
[root@localhost script]# vim testOut.sh echo "1" > out echo "2" > out ~ ~ "testOut.sh" [新] 2L, 30C 已写入 [root@localhost script]# sh testOut.sh [root@localhost script]# cat out 2
2、command >> file 输出重定向--追加
[root@localhost script]# vim testOut1.sh echo "1" >> out1 echo "2" >> out1 ~ ~ "testOut1.sh" 2L, 32C 已写入 [root@localhost script]# sh testOut1.sh [root@localhost script]# cat out1 1 2
3、command < file 输入重定向
[root@localhost script]# cat out 1 2 [root@localhost script]# wc -l < out # 将out的内容输入给 wc -l命令作为数据源,统计out的行数 2
4、文件描述符n > file
[root@localhost script]# cat test.sh #!/bin/bash echo "suc" echo1 "fail"
只将1输出到out文件, 则2直接显示在控制台上
[root@localhost script]# sh test.sh 1>out test.sh: line 3: echo1: command not found [root@localhost script]# cat out suc
只将2输出到out文件中,则1直接显示在控制台上
[root@localhost script]# sh test.sh 2>out suc [root@localhost script]# cat out test.sh: line 3: echo1: command not found
5、 n >& m (m,n为文件描述符)
[root@localhost script]# cat test.sh #!/bin/bash echo "suc" echo1 "fail"
[root@localhost script]# sh test.sh > out 2>&1
[root@localhost script]# cat out
suc
test.sh: line 3: echo1: command not found
6、n <& m(待补充)
7、<< tag 将 tag期间的内容作为输入(下例的tab为EOF)
[root@localhost script]# wc -l << EOF > JFDLK > JKFLSD > FJKSDF > EOF 3
相关文章推荐
- shell基础,命令历史,别名,通配符,输入输出重定向
- 【转】shell pipe与输入输出重定向的区别
- 8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向
- Shell基础--Bash基本功能---输入输出重定向
- 五周第三次课 2017.11.15 shell介绍、命令历史、命令补全和别名、通配符、输入输出重定向
- Linux之Shell学习(十)Shell输入输出重定向、文件包含
- Shell输入输出重定向:Shell Here Document,/dev/null文件
- 20.Shell输入输出重定向
- 二十二、Shell输入输出重定向:Shell Here Document,/dev/null文件
- Shell输入输出重定向
- shell脚本学习11——输入输出重定向
- 8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向
- shell介绍和通配符及输入输出重定向
- 五周第三次课(11月15日) 8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向
- shell i/o 输入输出重定向
- Shell输入输出重定向:Shell Here Document,/dev/null文件
- shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向
- shell之路【第四篇】输入输出重定向
- shell之输入输出重定向
- Shell输入输出重定向:Shell Here Document,/dev/null文件