“在bash中重定向标准错误stderr”的版本间的差异

来自YTYZX有图有真相的百科
跳转至: 导航搜索
 
(未显示同一用户的5个中间版本)
第1行: 第1行:
  一般 情况下,每 个 Unix/Linux 命令运行时都会打开三个文件:
+
  默认 情况下,每 个Unix/Linux命令运行时都会打开三个文件:
 
 
 
  标准输入文件stdin的文件描述符为0,Unix程序默认从stdin读取数据。
 
  标准输入文件stdin的文件描述符为0,Unix程序默认从stdin读取数据。
 
  标准输出文件stdout的文件描述符为1,Unix程序默认向stdout输出数据。
 
  标准输出文件stdout的文件描述符为1,Unix程序默认向stdout输出数据。
 
  标准错误文件stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。
 
  标准错误文件stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。
  
2>&1
+
1.输入“ls /var /ytyzx”查看/var目录和/ytyzx目录(/ytyzx目录不存在)。
 +
   备注:因为/ytyzx目录不存在,所以报错“ls: cannnot access /ytyzx: No such file or directory”
 +
[[File:Stderr1.png]]
 +
 
 +
2.输入“ls /var /ytyzx > printsave”将输出结果保存到名为printsave的文件里面。
 +
   备注:如需叠加保存到已存在文件可输入“ls /var /ytyzx >> printsave”命令。
 +
[[File:Stderr2.png]]
 +
 
 +
3.输入“less printsave”查看文件内容,发现不包含stderr信息(ls: cannnot access /ytyzx: No such file or directory)。
 +
[[File:Stderr3.png]]
 +
 
 +
4.如需将标准输出文件stout和标准错误文件stderr同时保存到名为stdouterr文件,则输入“ls /var /ytyzx > stdouterr 2>&1 ”。
 +
   备注:2>&1中间不包含任何空格。
 +
[[File:Stderr4.png]]
 +
 
 +
5.输入“less stdouterr”查看文件内容,发现已包括stdout和stderr。
 +
[[File:Stderr5.png]]
 +
 
 +
6.亦可输入“ls /var /ytyzx &> simple”将标准输出文件stout和标准错误文件stderr同时保存到名为simple的文件中。
 +
[[File:Stderr6.png]]

2019年3月15日 (五) 18:44的最新版本

默认情况下,每个Unix/Linux命令运行时都会打开三个文件:

标准输入文件stdin的文件描述符为0,Unix程序默认从stdin读取数据。
标准输出文件stdout的文件描述符为1,Unix程序默认向stdout输出数据。
标准错误文件stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。
1.输入“ls /var /ytyzx”查看/var目录和/ytyzx目录(/ytyzx目录不存在)。
  备注:因为/ytyzx目录不存在,所以报错“ls: cannnot access /ytyzx: No such file or directory”

Stderr1.png

2.输入“ls /var /ytyzx > printsave”将输出结果保存到名为printsave的文件里面。
  备注:如需叠加保存到已存在文件可输入“ls /var /ytyzx >> printsave”命令。

Stderr2.png

3.输入“less printsave”查看文件内容,发现不包含stderr信息(ls: cannnot access /ytyzx: No such file or directory)。

Stderr3.png

4.如需将标准输出文件stout和标准错误文件stderr同时保存到名为stdouterr文件,则输入“ls /var /ytyzx > stdouterr 2>&1”。
  备注:2>&1中间不包含任何空格。

Stderr4.png

5.输入“less stdouterr”查看文件内容,发现已包括stdout和stderr。

Stderr5.png

6.亦可输入“ls /var /ytyzx &> simple”将标准输出文件stout和标准错误文件stderr同时保存到名为simple的文件中。

Stderr6.png