Linux shell stdin & stdout & stderr All In One
Linux shell stdin
& stdout
& stderr
All In One
stdin & stdout & stderr
2>&1
二合一,输出错误 信息
# 无法复制 waring ❌
$ npx --no-install next info | pbcopy
# stderr 合并输出, 二合一 ✅
$ npx --no-install next info 2>&1 | pbcopy
https://github.com/web-full-stack/nextjs-ssr/issues/8#issuecomment-1330909413
https://www.cnblogs.com/xgqfrms/p/16934306.html#5129639
Standard streams
In computer programming, standard streams
are interconnected input and output communication channels[1] between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input
(stdin), standard output
(stdout) and standard error
(stderr). Originally I/O happened via a physically connected system console
(input via keyboard
, output via monitor
), but standard streams abstract
this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal
on which the shell is running, but can be changed with redirection
or a pipeline
. More generally, a child process
inherits the standard streams of its parent process
.
在计算机编程中,标准流
是计算机程序开始执行时与其环境之间互连的输入和输出通信通道[1]。 这三个输入/输出 (I/O) 连接称为标准输入
(stdin)、标准输出
(stdout) 和标准错误
(stderr)。 最初 I/O 通过物理连接的系统控制台
发生(通过键盘
输入,通过显示器
输出),但标准流对此进行了抽象
。 当通过交互式 shell 执行命令时,流通常连接到运行 shell 的文本终端
,但可以通过重定向
或管道
进行更改。 更一般地说,子进程
继承其父进程
的标准流。
https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)
https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)
https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)
Linux shells / Unix shells
2>&1
vs>&
Bourne
shell
# sh
$ which sh
/bin/sh
# command > file 2>&1
# 输出错误信息到 error-log.md ✅
$ rm not-exist-file > error-log.md 2>&1
Bash
(Unix shell)
# bash
$ which bash
/bin/bash
# command > file >&
# 简写,等价于 command > file 2>&1
# 输出错误信息到 error-log.md ✅
$ rm not-exist-file > error-log.md >&
https://en.wikipedia.org/wiki/Bourne_shell
https://en.wikipedia.org/wiki/Bash_(Unix_shell)
https://en.wikipedia.org/wiki/C_shell
https://en.wikipedia.org/wiki/Redirection_(computing)
Portable Operating System Interface (POSIX
)
https://en.wikipedia.org/wiki/POSIX
File descriptor
https://en.wikipedia.org/wiki/File_descriptor
demos
# 追加输出
$ man grep >> man-grep.md
# 覆盖输出
$ man grep > man-grep.md
>>
vs >
2>&1
vs>&
# bash
$ echo "hello world" > hello-world.md 2>&1
# 输出错误信息到 error-log.md ✅
$ rm hello > error-log.md 2>&1
# --force, --recursive
$ rm -f hello > error-log.md 2>&1
$ rm -rf hello > error-log.md 2>&1
Raspberry Pi
pi@raspberrypi:~/Desktop $ ./gpio.py > gpio.log.txt 2>&1
pi@raspberrypi:~/Desktop $ cat gpio.log.txt
Traceback (most recent call last):
File "/home/pi/Desktop/./gpio.py", line 10, in <module>
arg1 = sys.argv[1]
IndexError: list index out of range
pi@raspberrypi:~/Desktop $ ./gpio.py 3 > gpio.log.txt 2>&1
pi@raspberrypi:~/Desktop $ cat gpio.log.txt
arg1 = 3
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
pi@raspberrypi:~/Desktop $
https://www.cnblogs.com/xgqfrms/p/17306951.html#5166852
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16903732.html
未经授权禁止转载,违者必究!