如果"echo $?"怎么办?命令返回2?

zee*_*gon 2 linux shell echo

我知道,要看到我们上次执行的命令是正确的是shell,我们使用" echo $?"命令.为什么它返回2作为输出,它意味着什么?

Mus*_*GRU 5

您应该在echo $?之前关注最后一个命令.打开该命令手册页.你可以找到退出代码的含义.

例如;

man grep;
...
EXIT STATUS
The exit status is 0 if selected lines are found, and 1 if not found.  
If an error occurred the exit status is 2.  
(Note: POSIX error handling code should check for '2' or greater.)
....
Run Code Online (Sandbox Code Playgroud)

要么

man ls;

   Exit status:
       0      if OK,

       1      if minor problems (e.g., cannot access subdirectory),

       2      if serious trouble (e.g., cannot access command-line argument).
Run Code Online (Sandbox Code Playgroud)

要么

man diff

 Exit status is 0 if inputs are
       the same, 1 if different, 2 if trouble.
Run Code Online (Sandbox Code Playgroud)