對於作業系統來說,錯誤與警告一樣嗎?

這篇 知道了系統成功與否的區別,那錯誤與警告一樣嗎?而系統是如何表達”警告”,是 0(成功),還是非0(失敗)呢?

錯誤與警告

在 *nix 的世界裡,錯誤 error 和警告 warning 是不一樣的東西,系統返回的值中,錯誤就一定是 non-zero, warning 就要看當初程式怎麼寫;這如同在撰寫C/C++的程式時,寫成是的「可以決定 main 要 return 0
或是 return 任何其他的值。

一個小案例

前幾天我在嘗試藉由 Letax linter-ChkTeX 對 Latex 文件進行lint check 。在電腦裝好chktex後,就可以讓 Latex 程式,比如說 projection.tex,進行lint check:

1
chktex projection.tex

這個時候出現了大量警告:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ChkTeX v1.7.6 - Copyright 1995-96 Jens T. Berger Thielemann.
Compiled with PCRE regex support.
Warning 8 in projection.tex line 3: Wrong length of dash may have been used.
\RequirePackage[2020-02-02]{latexrelease}
^
Warning 8 in projection.tex line 3: Wrong length of dash may have been used.
\RequirePackage[2020-02-02]{latexrelease}
^
Warning 24 in projection.tex line 79: Delete this space to maintain correct pagereferences.
\label{s:projection}
^
......
No errors printed; 121 warnings printed; No user suppressed warnings; No line suppressed warnings.
See the manual for how to suppress some or all of these warnings/errors.

然後藉由echo $?查看執行是成功與否:

1
2

可以看到系統返回非0值,將 warning 以 error 返回。在這個情境下,warning chktex的作者自己在寫程式的時候,決定 warning 將返回一個非0的值。