"在bash中重定向标准错误stderr" 修訂間的差異
從 YTYZX有图有真相的百科
(未顯示同一使用者於中間所作的 3 次修訂) | |||
行 15: | 行 15: | ||
[[File:Stderr3.png]] | [[File:Stderr3.png]] | ||
− | 4.如需将标准输出文件stout和标准错误文件stderr同时保存到 | + | 4.如需将标准输出文件stout和标准错误文件stderr同时保存到 名为stdouterr 文件,则输入“ls /var /ytyzx > stdouterr 2>&1”。 |
备注:2>&1中间不包含任何空格。 | 备注:2>&1中间不包含任何空格。 | ||
[[File:Stderr4.png]] | [[File:Stderr4.png]] | ||
− | 5.输入“less | + | 5.输入“less stdouterr ”查看文件内容,发现已包括stdout和stderr。 |
[[File:Stderr5.png]] | [[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”
2.输入“ls /var /ytyzx > printsave”将输出结果保存到名为printsave的文件里面。 备注:如需叠加保存到已存在文件可输入“ls /var /ytyzx >> printsave”命令。
3.输入“less printsave”查看文件内容,发现不包含stderr信息(ls: cannnot access /ytyzx: No such file or directory)。
4.如需将标准输出文件stout和标准错误文件stderr同时保存到名为stdouterr文件,则输入“ls /var /ytyzx > stdouterr 2>&1”。 备注:2>&1中间不包含任何空格。
5.输入“less stdouterr”查看文件内容,发现已包括stdout和stderr。
6.亦可输入“ls /var /ytyzx &> simple”将标准输出文件stout和标准错误文件stderr同时保存到名为simple的文件中。