xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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   

image

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 >&

image

  1. Bourne shell
# sh
$  which sh
/bin/sh

# command > file 2>&1

# 输出错误信息到 error-log.md ✅
$ rm not-exist-file > error-log.md 2>&1

  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

image

https://en.wikipedia.org/wiki/File_descriptor

demos

# 追加输出
$ man grep >> man-grep.md

#  覆盖输出
$ man grep > man-grep.md

>> vs >

image

image

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

image

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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-11-18 16:37  xgqfrms  阅读(114)  评论(5编辑  收藏  举报