博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
stderr 重定向_如何判断是否将其STDERR重定向到Linux上Bash脚本中的文件?
阅读量:2518 次
发布时间:2019-05-11

本文共 1360 字,大约阅读时间需要 4 分钟。

stderr 重定向

Within a Bash script, how to judge whether its STDERR is redirected to a file in Bash on ?

在Bash脚本中,如何判断其STDERR是否已重定向到上Bash中的文件?

For example,

例如,

./script.sh /tmp/log 2>&1

Can script.sh detect that its STDERR is redirected? Knowing the destination file is better.

script.sh是否可以检测到其STDERR被重定向? 了解目标文件会更好。

To test whether a script’s STDERR (or ) is redirected to a file, check by

要测试脚本的STDERR(或 )是否重定向到文件,请通过

[[ -f /dev/stderr ]]

A process’ STDOUT and STDERR are /dev/stdout and /dev/stderr and testing them with -f in Bash can judge whether they are redirected to .

进程的STDOUT和STDERR是/dev/stdout/dev/stderr ,在Bash中用-f测试它们可以判断它们是否重定向到 。

An example:

一个例子:

$ cat check-stdout-stderr.sh #!/bin/bash# Author: Eric Zhiqiang Ma[[ -f /dev/stderr ]] && echo "STDERR is a file: $(readlink -f /dev/stderr)"[[ -f /dev/stdout ]] && echo "STDOUT is a file: $(readlink -f /dev/stdout)"

Tries:

尝试:

$ ./check-stdout-stderr.sh

outputs nothing.

什么都不输出。

$ ./check-stdout-stderr.sh >/tmp/a$ cat /tmp/aSTDOUT is a file: /proc/25141/fd/pipe:[2397415]

and

$ ./check-stdout-stderr.sh >/tmp/a 2>/tmp/b$ cat /tmp/aSTDERR is a file: /tmp/bSTDOUT is a file: /proc/25159/fd/pipe:[2393867]

The interesting part is that STDOUT redirecting is implemented as a pipe (with caching, I guess).

有趣的是STDOUT重定向是通过管道实现的(我想是带有缓存的)。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自:

stderr 重定向

转载地址:http://emlwd.baihongyu.com/

你可能感兴趣的文章
javascript语法结构
查看>>
Delphi 语句块《LceMeaning》
查看>>
sqlserver数据库的分离与附加
查看>>
2.4:远程服务器关机及重启时的注意事项
查看>>
内存管理之ARC
查看>>
Myeclipse中左边的项目目录没了
查看>>
linux基础知识和常用命令
查看>>
webpack安装
查看>>
白话ASP.NET MVC之三:Controller是如何解析出来的
查看>>
mac中将jmeter源码导入idea
查看>>
bootstrap table 标题列重复
查看>>
Python shutil 模块
查看>>
ruby 字符串学习笔记1
查看>>
local mysql and postgresql
查看>>
浙大pat 1025题解
查看>>
Python列表的深浅复制
查看>>
XXE注入攻击与防御
查看>>
js获取当前域名
查看>>
bare linefeeds received in ASCII mode
查看>>
PAT1101:Quick Sort
查看>>