make 2>&1 | tee xxx.log 引发的思考

本文详细解释了Linux系统中stdin, stdout, 和stderr的概念及其区别,并通过实例演示了如何使用这些标准流进行数据重定向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Background of this post:

Recently, I always noticed that many guys can't distinguish the meaning among stdin, stdout, and stderr, especially, they always ask the same question as: what does "2>&1" exactly mean?

So, here I want to give a summary of this three things, and of course, I'd like to take this "2>&1" as an example.

-----------------------
Interpretation:

First of all, I want to give out the exact meaning of "2>&1":

2>&1:

0 is stdin. 1 is stdout. 2 is stderr
-------------
There's one way to remember this construct (maybe it is not entirely accurate):
-------------
First of all, 2>1 looks like a very good way to redirect stderr to stdout. but remember, it has a very harmful disadvantage: it may actually be interpreted as "redirect stderr to a file named 1".
-------------
& indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1.
-------------
>& is shell syntax for "fold a file descriptor into another", you can also interprete it as "it is the syntax to redirect a stream to another file descriptor"

So, now, have you got the meaning of what does "2>&1" exact mean? I think you do!

Yes, it's very easy to understand, but what's more, I want to go little deep into this topic.

-----------------------
Furthermore:

Following picture gives a very clear defination of what is stdin stdout and stderr:

#  In Linux everything is a file.
# Your hardware is also a file:
* 0 - Input - Keyboard (stdin)
* 1 - Output - Screen (stdout)
* 2 - Error - Screen (stderr)

# For standard output:

Clear enough?

If you still feel confused about this, you can view this page to find more infomation:
http://tldp.org/LDP/abs/html/io-redirection.html

-----------------------
Example:

Finally, I want to give you a example to end this entry(Try to understand it ^_^):

The output of a.out is the following, without the 'STDXXX: ' prefix.

STDERR: stderr output
STDOUT: more regular

./a.out 3>&1 1>&2 2>&3 3>&- | sed 's/e/E/g'
more regular
stderr output

  1. First save stdout as &3 (&1 is duped into 3).
  2. Next send stdout to stderr (&2 is duped into 1).
  3. Send stderr to &3 (stdout) (&3 is duped into 2).
  4. close &3 (&- is duped into 3)

-----------------------
Others:

If you want to know what's the difference between "cmd >file 2>&1" and "cmd >file 2>file", pls refer this post which written by r2007:
http://bbs.chinaunix.net/thread-764727-1-1.html

前置环境准备1. 相关软件包准备rhel软件包openssl软件包https://openssl-library.org/source/old/index.htmlopensshhttps://www.openssh.com/上传相关软件包2. 配置yum源mkdir /soft mkdir /mnt/filemkdir /etc/yum.repos.d/bakmv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bakcat > /etc/yum.repos.d/base.repo << EDF[base]name=bashbaseurl=file:///mnt/fileenabled=1gpgcheck=0EDFmount -o loop /soft/rhel-server-7.9-x86_64-dvd.iso /mnt/file/yum clean allyum makecache3. 查询当前系统、openssh、openssl版本cat /etc/redhat-releasessh -V4. 安装依赖包yum -y install gcc gcc-c++ glibc make autoconf openssl-devel pcre-devel pam-devel rpm-build5. 禁止selinux##重点,禁止selinux 否则重启后会登录失败##sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/configsetenforce 06. 解压软件包cd /soft/tar -zxf openssh-9.8p1.tar.gz tar -zxf openssl-1.1.1w.tar.gz 升级opensslmv /usr/bin/openssl /usr/bin/openssl.bakmv /usr/include/openssl /usr/include/openssl.bakmv /usr/lib64/libssl.so /usr/lib64/libssl.so.bakcd /soft/openssl-1.1.1w/./config --prefix=/usr --shared && make && make installopenssl version升级openssh a. 编译安装opensshservice sshd stoprpm -e `rpm -qa|grep openssh` --nodepsrpm -qa |grep opensshrm -rf /etc/ssh#编译安装opensshcd /soft/openssh-9.8p1/./configure --prefix=/usr --sysconfdir=/etc/ssh --with-ssl-dir=/usr/local/openssl --with-zlib --with-md5-passwords --with-pam --with-tcp-wrappers && make && make install b. 修改ssh配置文件cat >> /etc/ssh/sshd_config << EDFport 22PermitRootLogin yesPermitEmptyPasswords noEDFcp contrib/redhat/sshd.init /etc/init.d/sshdcp contrib/redhat/sshd.pam /etc/pam.d/sshd.pam c. ssh服务chkconfig --add sshdchkconfig sshd onchkconfig sshd --listservice sshd startservice sshd restartssh -V版本回退方法yum remove -y openssh && yum install -y openssh openssh-clients openssh-serverservice sshd restart【注】:建议清理已保存的公私钥配置文件,rm -rf /etc/ssh/ssh_host 改为离线环境,交互式脚本,扫描软件路径,列出相关软件包,交互式输入openssl版本,交互式输入openssh版本,交互式输入挂载镜像版本,每个步骤都需检查,根据检查结果交互执行下一步,显示详细日志
最新发布
08-09
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值