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

标签 linux shell echo

我知道,要查看我们最后执行的命令是否正确是 shell,我们使用“echo $?”命令。 为什么它返回 2 作为输出,这意味着什么?

最佳答案

您应该关注 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.)
....

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).

man diff

 Exit status is 0 if inputs are
       the same, 1 if different, 2 if trouble.

关于linux - 如果 "echo $?"命令返回 2 怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40233175/

相关文章:

linux - 如何将密码传递给bash脚本

python - 以 root 身份运行 shell 命令并使用 python 检测失败的密码尝试

c - 从 stdin 回显 getchar 和 '\n' char

php 脚本正在输出 php 代码,不知道为什么

echo - nmake - 如何强制 echo 命令输出制表符?

linux - 如何设置终端标题以在运行时显示当前正在运行的命令,并在完成后将其显示在括号中?

php - 更新到 v4.8.0 后 phpMyAdmin 出错 : The $cfg ['TempDir' ] (./tmp/) 无法访问

linux - 进程内的线程ID

linux - 如何通过在 linux 中传递变量从包含字符串的行之前的文件中删除所有数据

shell - 在 makefile 中使用 $RANDOM