linux运行java类重定向标准输入,Linux 标准输出(stdout)和标准错误(stderr)的重定向...

之前常常会听到这些词,还有标准输入之类,彻底不明因此。直到最近须要让python脚本里的print内容打印到日志文件里,才开始研究这究竟是什么。

原来,标准输出(stdout)指的就是在命令行里,每次你输入指令后,终端上打印出来的那些话,那些反馈。标准错误(stderr)跟标准输出差很少,只不过是程序出错时反馈的内容。标准输入(stdin)就是程序指示让你输入用户名密码之类的这种,这里很少谈输入。python

问题是,咱们很经常使用的会让一些脚本本身在后台24/7运行,这种时候脚本的输出内容到屏幕上(标准输出)也没什么意义,咱们看不到也保存不了。因此最好让它把反馈的内容所有直接写如一个文件里,咱们叫日志文件,其实就是个txt。而后咱们本身能够查看日志来看到底发生了什么。linux

这种把显示到屏幕的程序反馈,变成存到文件里的动做,咱们叫作输出重定向(stdout redirection)

在命令行里,咱们能够用符号直接把程序输出转向到某个文件或某个程序,以下:git

$ git push > log.txt

而后,理论上咱们日常git push后的反馈就会保存到log.txt这个文件里了,且屏幕上不会显示任何东西。

但其实这个仍是有问题的,由于过后咱们发现有一些存到了log.txt,还有一些话漏网显示到了屏幕上,没存进去文档里。

其实原来这些显示到屏幕上的反馈有些是stdout有些是stderr,咱们用>或>>符号重定向,只是默认重定向stdout,没有重定向stderr,因此会有漏网之鱼。对此,咱们须要了解下这个符号的设定,和怎么把stderr也包括进来,一块儿重定向过去。shell

重定向符号和语句

稍微会一点点linux命令的,都会用到cmd > file这样的语句,把命令反馈的输出到一个文件里。固然还有cmd >> file,这是把内容追加到文件里,而不是从新擦写一遍。>这个符号能够念redirect to。

实际上,重定向有不少种设置和配合,让你能够分别重定向标准输出和标准错误,或者一块儿重定向,而后还能够选择是只输出到文件里仍是同时输出大显示屏上和文件里。

这里咱们就要了解一下设置重定向的基本语法了,以下:ubuntu

> 以擦写的模式重定向至...

>> 以追加的模式重定向至...

1 表明stdout标准输出

2 表明stderr标准错误

因此,cmd > file其实是缩略了的写法,理解起来,应该是cmd &1> file,也就是只把标准输出转出去。

那么同理,只把标准错误转出去,就应该是cmd &2> file。

其中,&符号没任何实际意义,只是以致区分,表明后面的符号是要设置重定向用的,而不是某个文件的名字。app

2>&1

每次查重定向问题时,咱们总会看到这句话,通常人很难理解这究竟是在干吗。我一开始觉得是2要大于1什么的,真是笑话。

其实这是个重定向的设置,设置让2重定向到1,也就是让stderr标准错误重定向到stdout标准输出,而后两个并在一块儿再重定向。其中&没什么意思只是区分开来1是表明stdout而不是表明一个文件名。

用起来的格式是:cmd > file 2>&1。

为何设置要放在后面呢?

具体暂时还不知道,只知道是这么用,放在前面还不行只能放在后面。this

好比:spa

$ git push > log.txt 2>&1

那么这时候,屏幕上就真的不会显示任何东西了,标准输出、标准错误,所有都会存到log.txt文件里了。命令行

经常使用重定向及解释

a98328b87f4c48d3b44670f231eaa59a.gif

command > output.txt

The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.日志

command >> output.txt

The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

command 2> output.txt

The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.

command 2>> output.txt

The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

command &> output.txt

Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.

command &>> output.txt

Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file..

command | tee output.txt

The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, it gets overwritten.

command | tee -a output.txt

The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

(*)

Bash has no shorthand syntax that allows piping only StdErr to a second command, which would be needed here in combination with tee again to complete the table. If you really need something like that, please look at "How to pipe stderr, and not stdout?" on Stack Overflow for some ways how this can be done e.g. by swapping streams or using process substitution.

command |& tee output.txt

Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, it gets overwritten.

command |& tee -a output.txt

Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值