我终于知道为啥"find /home -name .bashrc > list 2>list"是错误的啦

本文深入解析Linux中数据流重定向的原理,特别是标准输出和标准错误输出的区别及如何将其合并在同一文件中,避免数据交叉混乱的问题。通过实例说明了使用不同重定向语法的效果,帮助读者理解2>&1和&>的正确用法。

在《鸟哥的Linux私房菜》第三版的329-330,鸟哥介绍了数据流的重定向,里面提到一点,如何你想把正确与错误数据都写到一个文件中,这个时候用下面第一行代码是错误的,而使用第二行和第三行代码才是正确的

find /home -name .bashrc > list 2>list
find /home -name .bashrc > list 2>&1
find /home -name .bashrc &> list

至于第一行错误的原因,鸟哥说这是因为两条数据同时写入一个文件,有没有使用特殊的语法,此时两条数据可能会交叉写入该文件内,造成次序的错乱。

在刚开始学Linux的时候,我只记住了结论,就是用&> 将标准输出和标准错误输出写入到一个文件中,而2>&1要记忆4个字符,还得记住要放在文件的后面,脑容量不够,就没有记住。而这两天读到了《Linux命令行与shell脚本编程大全》第3版第15章的时候,我终于明白了为什么第一行代码会出现次序错乱,2>&1为什么这些写是正确的。

首先,你需要理解1>2>里的数字1和2指的是文件描述符(file descriptor)。 所谓的文件描述符是一个非负整数,用于标识打开的文件。在bash shell中,1表示STDOUT(标准输出),2表示STDERR(标准错误), 当然还有一个0表示的STDIN(标准输入)。我们还可以定义其他整数,但这个就比较复杂,算是高级技巧了。

其次,你要知道">"和另一个符号"<"是两个常用重定向符号。所谓的重定向,就是改变数据流的原本走向,本来输出到屏幕的输出写入到文件中,本来需要从文件读取的数据,可以从键盘上输入

那么&符号是什么含义呢?在C语言里&是一个求址符号,因此&1你可以理解成获取标准输出对象的地址,而>&1就表示把输入定向到了标准输出, >&2也就是把输出定向到了标准错误输出。那么我们就可以在脚本里面写一些标准错误输出了, 比如创建一个脚本, test.sh

#!/bin/bash
echo  "This is a first normal output" 
echo  "This is a first error" >&2
echo  "This is a second error" >&2
echo  "This is a second nromal output"

bash test.sh 1> a 2>a运行的话,由于同时对a文件进行写入操作,不同的磁盘写入速度,会有不同的结果,因为前面的输出很可能会后面的覆盖了。

cat a
This is a first error
This is This is a second nromal output

从我的输出结果可以推测出,首先是输出“This is a first normal output”,但是被后来者“This is a first error”给覆盖了。然后“This is a second error"写到一半,就被“This is a second nromal output”给覆盖了。

而以bash test.sh 1> a 2>&1的话,就会把标准错误输出定向到标准输入上,就会以此输出了。

This is a first normal output
This is a first error
This is a second error
This is a second nromal output

考虑到这个操作比较常用,每次都要写2>&1太麻烦了,于是就定义了一个&>符号方便使用。

forlinx@ok3588:~/rknn-toolkit2-master/rknn-toolkit2/examples/onnx/yolov5$ sudo find / -name "libgomp*" 2>/dev/null /home/forlinx/.local/lib/python3.8/site-packages/torch.libs/libgomp-efb3da07.so.1.0.0 /home/forlinx/.local/lib/python3.8/site-packages/torch.libs/libgomp-d22c30c5.so.1.0.0 /home/forlinx/miniconda3/conda-meta/libgomp-11.2.0-h1234567_1.json /home/forlinx/miniconda3/share/info/libgomp.info /home/forlinx/miniconda3/pkgs/_openmp_mutex-5.1-51_gnu/lib/libgomp.so.1 /home/forlinx/miniconda3/pkgs/libgcc-ng-11.2.0-h1234567_1/share/info/libgomp.info /home/forlinx/miniconda3/pkgs/libgomp-11.2.0-h1234567_1 /home/forlinx/miniconda3/pkgs/libgomp-11.2.0-h1234567_1/lib/libgomp.so.1.0.0 /home/forlinx/miniconda3/pkgs/libgomp-11.2.0-h1234567_1/lib/libgomp.so /home/forlinx/miniconda3/pkgs/libgomp-11.2.0-h1234567_1.conda /home/forlinx/miniconda3/envs/rknn152/conda-meta/libgomp-11.2.0-h1234567_1.json /home/forlinx/miniconda3/envs/rknn152/share/info/libgomp.info /home/forlinx/miniconda3/envs/rknn152/lib/libgomp.so.1.0.0 /home/forlinx/miniconda3/envs/rknn152/lib/libgomp.so.1 /home/forlinx/miniconda3/envs/rknn152/lib/libgomp.so /home/forlinx/miniconda3/lib/libgomp.so.1.0.0 /home/forlinx/miniconda3/lib/libgomp.so.1 /home/forlinx/miniconda3/lib/libgomp.so /var/lib/dpkg/info/libgomp1:arm64.symbols /var/lib/dpkg/info/libgomp1:arm64.triggers /var/lib/dpkg/info/libgomp1:arm64.shlibs /var/lib/dpkg/info/libgomp1:arm64.md5sums /var/lib/dpkg/info/libgomp1:arm64.list /usr/local/lib/python3.8/dist-packages/tensorflow_cpu_aws.libs/libgomp-cc9055c7.so.1.0.0 /usr/local/lib/python3.8/dist-packages/torch/lib/libgomp-d22c30c5.so.1 /usr/share/doc/libgomp1 /usr/lib/llvm-10/lib/libgomp.so /usr/lib/aarch64-linux-gnu/libgomp.so.1.0.0 /usr/lib/aarch64-linux-gnu/libgomp.so.1 /usr/lib/gcc/aarch64-linux-gnu/9/libgomp.spec /usr/lib/gcc/aarch64-linux-gnu/9/libgomp.a /usr/lib/gcc/aarch64-linux-gnu/9/libgomp.so /usr/lib/gcc/aarch64-linux-gnu/8/libgomp.spec /usr/lib/gcc/aarch64-linux-gnu/8/libgomp.a /usr/lib/gcc/aarch64-linux-gnu/8/libgomp.so forlinx@ok3588:~/rknn-toolkit2-master/rknn-toolkit2/examples/onnx/yolov5$
最新发布
08-09
root@ubuntu:/zlab_16t2/changqingteng# echo "/usr/local/lib" | tee /etc/ld.so.conf.d/freesasa.conf /usr/local/lib root@ubuntu:/zlab_16t2/changqingteng# ldconfig -v | grep freesasa /sbin/ldconfig.real: Can't stat /usr/local/lib/i386-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/lib/i386-linux-gnu' given more than once (from /etc/ld.so.conf.d/i386-linux-gnu.conf:4 and /etc/ld.so.conf.d/i386-linux-gnu.conf:3) /sbin/ldconfig.real: Can't stat /usr/local/lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Can't stat /lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Can't stat /usr/lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/local/lib' given more than once (from /etc/ld.so.conf.d/libc.conf:2 and /etc/ld.so.conf.d/freesasa.conf:1) /sbin/ldconfig.real: Can't stat /usr/local/lib/x86_64-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once (from /etc/ld.so.conf.d/x86_64-linux-gnu.conf:4 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib32' given more than once (from /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:3 and /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:2) /sbin/ldconfig.real: Path `/lib/x86_64-linux-gnu' given more than once (from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once (from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib' given more than once (from <builtin>:0 and <builtin>:0) /sbin/ldconfig.real: /lib/i386-linux-gnu/ld-linux.so.2 is the dynamic linker, ignoring /usr/local/lib: (from /etc/ld.so.conf.d/freesasa.conf:1) /sbin/ldconfig.real: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 is the dynamic linker, ignoring /sbin/ldconfig.real: /lib32/ld-linux.so.2 is the dynamic linker, ignoring 请分析错误原因,并给出详细解决步骤
07-27
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值