linuxI/O重定向
假设有这么一段python3程序:
import sys
print ("")
sys.stderr.write("stderr1 ")
现在想要把程序的输出以及错误信息都存在文件中。
一开始是这么做的:
python ./test.py >& > t1
结果终端依然输出了信息
stderr1
而文件t1里面的内容是
[ss@localhost test]$ cat t1
搜索之后发现,shell解析重定向是从左往右按顺序解析。也就是先解析2>&1把stderr重定向到stdout也就是输出到终端,接着解析>t1把stdout重定向到t1文件。
所以为了达成目标,需要改成
python ./test.py > t1 >&
这样就会先把stdout重定向,再重定向stderr,都输出到t1文件。
转载介绍:
文件描述符(file descriptor)
名称 类型 文件描述符 操作
标准输入 standard input <,<<
标准输出 standard output >,>>
标准错误输出 standard error output >,>>
文件描述符的存储位置位于 /proc/self/fd ,文件描述符是通过一系列软链接指向的默认输出设备,这里我们的默认设备就是模拟终端
https://www.cnblogs.com/divent/p/5773861.html
----------------
python ./test.py >& | cat >t1
似乎pipe只会接收stdout的输入,参考解释:
Note that the sequence of I/O redirections is interpreted left-to-right, but pipes are set up before the I/O redirections are interpreted.
File descriptors such as and are references to open file descriptions. The operation >& makes file descriptor aka stderr refer to the same open
file description as file descriptor aka stdout is currently referring to (see dup2() and open()).
The operation >/dev/null then changes file descriptor so that it refers to an open file description for /dev/null,
but that doesn't change the fact that file descriptor 2 refers to the open file description which file descriptor 1 was originally pointing to — namely, the pipe.
如果是:
python ./test.py | cat >t1
那么t1中只会有stdout的输出。也可以尝试:
python ./test.py >& >/dev/null | cat >t1
只接收stderr输出。
import sys
print ("")
sys.stdout.flush()
sys.stderr.write("stderr1 ")
print ("")
sys.stdout.flush()
测试stderr是否有缓冲,结果显示似乎也是有缓冲的(行缓冲)。
python3中stderr重定向到文件时,似乎是全缓冲的
"python3": sys.stderr is line buffered at both the TextIOWrapper layer and may be fully buffered at the binary BufferedWriter layer if the output is redirected to a file
https://bugs.python.org/issue13601
#!/usr/bin/python
import sys
print("stdout1",end='')
sys.stderr.write("stderr1 ")
print("stdout2 ",end='')
sys.stderr.write("stderr2 ")
sys.stderr.flush()
print ("")
#sys.stdout.flush()
sys.stderr.write("error\n")
终端直接运行输出:
stderr1 stderr2 stdout1 stdout2 123
error
缓存模式和https://www.cnblogs.com/starRebel/p/8359455.html相似,需要注意的是python中的print默认带有换行符。
linuxI/O重定向的更多相关文章
- Linux-I/O重定向和管道
Linux I/O重定向 标准输入(stdin):文件描述符0 标准输入(stdout):文件描述符1 标准错误(stderr):文件描述符2 file descriptors(FD,文件描述符 或 ...
- Http状态码之:301、302重定向
概念 301 Moved Permanently 被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一.如果可能,拥有链接编辑功能的客户端应当自动把请求的地 ...
- 前端学HTTP之重定向和负载均衡
前面的话 HTTP并不是独自运行在网上的.很多协议都会在HTTP报文的传输过程中对其数据进行管理.HTTP只关心旅程的端点(发送者和接收者),但在包含有镜像服务器.Web代理和缓存的网络世界中,HTT ...
- Web安全相关(三):开放重定向(Open Redirection)
简介 那些通过请求(如查询字符串和表单数据)指定重定向URL的Web程序可能会被篡改,而把用户重定向到外部的恶意URL.这种篡改就被称为开发重定向攻击. 场景分析 假设有一个正规网站http:// ...
- Spring MVC重定向和转发以及异常处理
SpringMVC核心技术---转发和重定向 当处理器对请求处理完毕后,向其他资源进行跳转时,有两种跳转方式:请求转发与重定向.而根据要跳转的资源类型,又可分为两类:跳转到页面与跳转到其他处理器.对于 ...
- Chrome在302重定向的时候对原请求产生2次请求的问题说明
这个问题应该确确实实是一个Chrome的BUG,我在自己的编程环境中发现,并在多个服务器,多个编程语言的运行环境,以及多个浏览器下都测试过,都看到有2次请求出现.为了证明不是自己环境的问题,我也特意去 ...
- 重定向Http status code 303 和 302
http 302 http 303 Http 302 302是一个普通的重定向代码.直观的看来是,请求者(浏览器或者模拟http请求)发起一个请求,然后服务端重定向到另一个地址.而事实上,服务端仅仅是 ...
- mvc 重定向的几种方式
在RouteConfig添加一个简单的路由 //新增路由 routes.MapRoute( name: "Article", url: "Detial/{id}" ...
- C#开发微信门户及应用(14)-在微信菜单中采用重定向获取用户数据
我曾经在系列文章中的<C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍>中介绍了微信菜单里面的重定向操作,通过这个重定向操作,我们可以获取一个code值,然后获取用户的open ...
随机推荐
- 利用trace重建控制文件
以下步骤,内容都在ora11g_ora_.trc文件中 CREATE CONTROLFILE REUSE DATABASE "ORA11G" NORESETLOGS ARCHIV ...
- HDU Humble Numbers
Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...
- react 侧栏二级菜单组件
侧边栏菜单组件 component 下新建menu文件,menu下建index.jsx和subitem.jsx index.jsx import React, { Component } from ' ...
- 在centos上用nginx访问php显示404
yum install nginx -y 可以在浏览器 访问html文件,但是访问不了php文件 后来我看见别人别人响应的参数有php,而我没有我,就觉得php配置应该有问题: 我 yum insta ...
- 【php】---mysql---基本操作及使用---【巷子】
1.数据库简介 (1).什么是数据库? 一个文件 一个文件夹 一个u盘 一个硬盘......都叫做数据库 存放数据的仓库 (2).常见的数据库? mySql sql ...
- HRBUST - 1153 意外 HRBUST - 1153 (数论)
意外 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 326(87 users) Total Accepted: 97(63 users ...
- ubuntu16.04下笔记本自带摄像头编译运行PTAM
ubuntu16.04下笔记本自带摄像头编译运行PTAM 转载请注明链接:https://i.cnblogs.com/EditPosts.aspx?postid=9014147 个人邮箱:feifan ...
- 【优先队列】POJ2010- Moo University-Financial Aid
[题目大意] 给出C头奶牛的SAT成绩和申请奖学金,选出N头牛,使得总奖学金在≤F的情况下奶牛SAT成绩的中位数最大. [思路] 假设before[i]表示前i头奶牛中n/2头奶牛奖学金总额的最小值, ...
- Android复习
第一章 1.Android系统架构:(四层) Linux内核层:为安卓设备的各种硬件提供底层的驱动. 系统运行库层:为Android系统提供主要的特性支持. 应用框架层:提供了构建应用程序可能用到的各 ...
- 洛谷P4799 世界冰球锦标赛 CEOI2015 Day2 meet-in-the-middle
正解:折半搜索 解题报告: 先放个传送门QAQ 想先说下部分分?因为包含了搜索背包两个方面就觉得顺便复习下?QwQ 第一档部分分 爆搜 就最最普通的爆搜鸭,dfs(第几场,钱),然后每次可以看可以不看 ...