最新消息:20210917 已从crifan.com换到crifan.org

[整理]Linux中重定向中大于号的用户以及>2&1的含义

Linux crifan 3372浏览 0评论

折腾:

[整理]命令前面加上nohup是什么含义

期间,虽然大概明白了:

“nohup ./command.sh > output 2>&1 &

解释:前面的nohup 和后面的&我想大家都能明白了把。

主要是中间的 2>&1的意思

这个意思是把标准错误(2)重定向到标准输出中(1),而标准输出又导入文件output里面,

所以结果是标准错误和标准输出都导入文件output里面了。

至于为什么需要将标准错误重定向到标准输出的原因,那就归结为标准错误没有缓冲区,而stdout有。

这就会导致 >output 2>output 文件output被两次打开,而stdout和stderr将会竞争覆盖,这肯定不是我门想要的.

这就是为什么有人会写成:

nohup ./command.sh >output 2>output出错的原因了”

但是对于此处的:

2>&1

的含义,还是不是足够理解。

Linux 重定向 2&1

Linux redirect 2&1

Linux Shell 1>/dev/null 2>&1 含义 – IT-Homer – 博客频道 – CSDN.NET

Linux shell的标准输入、输出和错误 – 生活不止眼前的苟且 – 博客频道 – CSDN.NET

linux shell数据重定向(输入重定向与输出重定向)详细分析 – 程默 – 博客园

Linux Shell 通配符、元字符、转义符使用实例介绍 – 程默 – 博客园

->此处的&是:

重导向 file descriptor

的意思。

Shell 输入/输出重定向 | 菜鸟教程

bash – In the shell, what does ” 2>&1 ” mean? – Stack Overflow

io redirection – What does 2>&1 in this command mean? – Unix & Linux Stack Exchange

linux – Why redirect output to 2>&1 and 1>&2? – Super User

[总结]

some_command_and_para > some_log_file 2>&1

==

some_command_and_para 2> some_log_file 1>&2

数字
常见叫法
对应的设备文件
对应Linux内核源码
/usr/include/unistd.h
中的定义:
/* Standard file descriptors.  */
0
  • standard input
  • 标准输入
  • stdin
  • STDIN
#define STDIN_FILENO    0   /* Standard input.  */
1
  • standard output
  • 标准输出
  • stdout
  • STDOUT
#define STDOUT_FILENO   1   /* Standard output.  */
2
  • standard error
  • 标准错误输出
  • stderr
  • STDERR
#define STDERR_FILENO   2   /* Standard error output.  */

转载请注明:在路上 » [整理]Linux中重定向中大于号的用户以及>2&1的含义

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
99 queries in 0.201 seconds, using 23.43MB memory