Shell I/O重定向
来源:互联网 发布:施耐德base 知乎 编辑:程序博客网 时间:2024/05/08 18:12
① 默认I/O
默认情况下,在登陆时,shell自动将标准输入设置为 “键盘”,将标准输出和标准错误设置为 “屏幕”。
1,
标准输入(stdin) :代码为 0 ,使用 < 或 << ;
对应设备号 /dev/stdin -> /proc/self/fd/0 ,0 代表:/dev/stdin 。
2,
标准输出(stdout):代码为 1,使用 > 或 >> ;
对应设备号 /dev/stdout -> /proc/self/fd/1 ,1代表:/dev/stdout 。
3,
标准错误输出(stderr):代码为 2,使用 2> 或 2>> ;
对应设备号/dev/stderr -> /proc/self/fd/2 ,2代表:/dev/stderr 。
info proc 查了一下/proc/self/ 和/proc/[number]/fd这些都是什么东西?/proc/self This directory refers to the process accessing the /proc filesystem, and is identical to the /proc directory named by the process ID of the same process.这个目录对应操作/proc文件系统的线程本身,也就是说/proc/self/和/proc/[number]/是一样的。/proc/[number]/fd This is a subdirectory containing one entry for each file which the process has open, named by its file descriptor, and which is a symbolic link to the actual file. Thus, 0,is standard input, 1,is standard output, 2,is standard error, etc.
实例查看:
root@37C:~$ ll /dev/stdin lrwxrwxrwx 1 root root 15 9月 20 14:04 /dev/stdin -> /proc/self/fd/0 root@37C:~# ll /proc/self/fd/0lrwx------ 1 root root 64 9月 21 14:12 /proc/self/fd/0 -> /dev/pts/2root@37C:~# ll /dev/pts/2 #最终链接到了字符设备。crw------- 1 py tty 136, 2 9月 21 14:13 /dev/pts/2# 使用tty查看本终端对应的设备文件。root@37C:~# tty/dev/pts/2
尝试下面的执行:
while [ 1 ]; do echo "1111" > /proc/self/fd/2; done或者while [ 1 ]; do echo "1111" > /dev/stdin; done--查看FD的关闭现象:ls -l /proc/self/fd/ 0<&- 2>&-ls -l /proc/self/fd/
②使用>和>>重定向的要点
对于Shell两大家族:
(1)将命令的输出覆盖到文件中 command > filename
例如:ls > file1 命令执行效果: 如果不存在file1,则创建,并写入ls的输出内容; 如果已存在file1,则清除其内容,写入ls的输出内容.
(2)将命令的输出追加到文件尾 command >> filename
例如:ls >> file1 命令执行效果: 如果不存在file1,则创建,并写入ls的输出内容; 如果已存在file1,则将ls的输出内容追加到文件尾.
(3)如何避免疏忽覆盖了源文件。
防范措施:
(Bash, Korn shell)家族:
With the Bourne shell family (Bash, Korn shell):
–set the noclobber shell option: $ set -o noclobber
–To unset this option, use: $ set +o noclobber
设置之后:
1): 对于 > 的影响 。
当 names 文件还没有存在时, sort > names
,将创建一个names文件.(情况和之前没设置noclobber选项一样)
当 names 文件已经存在, sort > names
,那么将报错:cannot overwrite existing file.
但是,我们还是可以强制将其覆盖,如下: $ sort >| names
2): >> 追加数据的情况:
B-shell家族运行和之前没设置的一样
文件没有则创建, 有就追加。
③ 重定向的一些技巧
1.同时将标准输出和错误输出到同个位置。
1):截断模式。 $ ls run.py error.sh > output 2>&1
2):追加模式。 $ ls run.py error.sh >> output 2>&1
3):简洁模式(只限于bash)
# 截断模式$ ls run.py error.sh &> output
# 追加模式,好像低版本的bash并不支持。$ ls run.py error.sh &>> output
4):容易犯错写法。
# 2>&1 的顺序在前。这并没错误。# 但是,标准错误会指向显示器。# 实际顺序:1.标准错误指向显示器 2.标准输出指向output。$ ls run.py error.sh 2>&1 > output
2.抛弃输出。
抛弃输出: 不希望看到某些信息,或者不希望保存某些信息的时候,将其抛弃。
可抛弃标准输出和标准错误。
/dev/null:位桶 ,发送给该文件的任何东西都会永远消失!
--抛弃:--单独抛弃:update > /dev/nullupdate 2> /dev/null--同时抛弃:update > /dev/null 2>& 1
练习:
如下几种重定向方式,看看会出什么结果,为什么?$ find /home -name lost* > all_result 1>&2 # all_result文件没有内容,所有输出都在屏幕。$ find /home -name lost* 2> all_result 1>&2 # 所有的输出都在all_result文件。$ find /home -name lost* 2>&1 1> all_result # 错误在屏幕,标准输出在文件。
- shell------ I/O 重定向
- Shell I/O重定向
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- Linux shell I/O重定向--进阶
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- Linux shell I/O重定向详解
- linux shell I/O 重定向
- shell I/O重定向 1
- shell I/O重定向 2
- Linux Shell I/O重定向
- shell i/o 输入输出重定向
- 编码解码代码
- red hat 6.5 配置yum源
- [kuangbin带你飞]专题一 简单搜索 F POJ3126
- preparedstatement对数据库的模糊查询
- 设计模式之——单例模式(Singleton)的常见应用场景
- Shell I/O重定向
- StringTokenizer和Split性能比较
- C++泛型中replace和replace_copy
- redis实现消息队列
- hdu1824 Let's go home--2-sat
- 第三周 项目四——顺序表应用(1)
- 面试笔试---C\C++查漏补缺
- CODEVS 1001 舒适的路线 题解
- 拔掉网线时Socket的检查方法