Shell脚本-io重定向-运算符的优先级 [英] Shell Scripting - io redirecting - precedence of operators

查看:125
本文介绍了Shell脚本-io重定向-运算符的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释这两个代码之间的区别吗?

could somebody explain the difference between these two codes?

bad_command 2>&  >> file.out 

bad_command >> file.out 2>& 

手册中说这两个代码是不同的,第一个命令会 什么都不输出到file.out.

The manual said that these two codes are different,and first command will output nothing to file.out.

所以,这是我的问题.

1/这是什么原因?

2/是否有一个文档描述了操作符优先级在Shell中的工作方式? shell如何解析并使其成为语法树.

2/ Is there is a document which describes how operator precedence works in shell? how shell parses and made it's syntax tree.

3/正确的语法和顺序是什么?

3/ What is the correct syntax and order of it?

-提前感谢-

推荐答案

两者在语法上都是错误的.我想你是说

Both are syntactically wrong. I assume you meant

bad_command 2>&1  >> file.out

bad_command >> file.out 2>&1

相反.

这些之间有区别.重定向是命令性语句,从左到右进行处理.重定向对进程的文件描述符(fds)进行操作.您可能已经听说过标准文件描述符#0(标准输入/标准输入),#1(标准输出/标准输出),#2(标准错误/标准错误).

Between these, there is a difference. Redirections are imperative statements, which are worked through from left to right. A redirection operates on a process' file descriptors (fds). You might have heard of the standard filedescriptors #0 (standard in/stdin), #1 (standard out/stdout), #2 (standard error/stderr).

第一个命令行的重定向显示为:使fd 2成为fd 1的副本,但是将fd 1更改为附加到'file.out'"(第二个重定向对fd 2无效,后者仍然是fd 2的副本. fd 1刚开始是什么)

The first commandline's redirections read: "Make fd 2 a copy of fd 1, but then change fd 1 to append to 'file.out'" (the second redirection has no effect to fd 2, which still is a copy of what fd 1 was at the beginning)

秒数显示为:将fd 1更改为追加到'file.out',然后使fd 2成为fd 1的副本"(第一个重定向对第二个重定向有效,bot fds现在相同)

The seconds ones read: "Change fd 1 to append to 'file.out', and then make fd 2 a copy of fd 1" (the first redirection has effect to the second redirection, bot fds are now the same)

这篇关于Shell脚本-io重定向-运算符的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆