127返回代码从$?。

365

在UNIX中,$?返回值为127的含义是什么?

10个回答

521

当给定的命令未在你的PATH系统变量中找到且不是内置的shell命令时,/bin/sh返回值127。换句话说,系统无法理解你的命令,因为它不知道如何找到你要调用的二进制文件。


74
如果一个 Bash 脚本虽然存在,但没有 "+x" 模式,也会出现这种情况。请注意,不要改变原意并尽量使翻译通俗易懂。 - MatthewKremer
4
你可以尝试使用 which [程序名] 命令来查看操作系统正在使用哪个二进制文件。如果没有输出结果,接下来需要检查执行权限和 PATH 环境变量。 - four43
10
@cr125rider,“which”并不十分准确——它无法识别别名、shell函数、PATH查找记忆化或其他与shell状态有关的因素。最好使用type,它是一个Shell内置命令,可以识别所有这些内容。 - Charles Duffy
7
我也遇到了一个使用Windows换行符的文件,将行尾格式更正为Unix格式解决了这个问题。 - Mitkins
4
实际上,当我试图调用一个非可执行文件(不考虑其内容)时,我会得到 126(“权限被拒绝”),而不是 127;同样,尝试执行一个_目录_也会导致 126(“是一个目录”)。 - mklement0
显示剩余2条评论

76

一般而言,它的意思是:

127 - 命令未找到

但它也可能意味着命令已被找到
但是该命令所需的库未被找到


23

127 - command not found

例子: $caat 错误信息将会是:

bash:

caat: 命令未找到

现在你可以使用echo $?来检查。


12
一个 shell 中的约定是,一个成功的可执行文件应该以值 0 退出。其他任何值都可以被解释为某种失败,可能是 bash 或刚刚运行的可执行文件的一部分的失败。另请参阅 $PIPESTATUS 和 bash 手册页中的 EXIT STATUS 部分:
   For  the shell’s purposes, a command which exits with a zero exit status has succeeded.  An exit status
   of zero indicates success.  A non-zero exit status indicates failure.  When a command terminates  on  a
   fatal signal N, bash uses the value of 128+N as the exit status.
   If  a command is not found, the child process created to execute it returns a status of 127.  If a com-
   mand is found but is not executable, the return status is 126.

   If a command fails because of an error during expansion or redirection, the exit status is greater than
   zero.

   Shell  builtin  commands  return  a  status of 0 (true) if successful, and non-zero (false) if an error
   occurs while they execute.  All builtins return an exit status of 2 to indicate incorrect usage.

   Bash itself returns the exit status of the last command executed, unless  a  syntax  error  occurs,  in
   which case it exits with a non-zero value.  See also the exit builtin command below.

9

它没有特别的意义,除了最后一个退出的进程以状态码 127 退出。

然而,如果你使用 bash 作为 shell,它也用于告诉你尝试执行的命令无法执行(即找不到该命令)。如果进程以状态码 127 退出或找不到该命令,则很遗憾无法立即推断出原因。

编辑:
除了控制台上的输出,它不能立即被推断出来,但这里是 Stack Overflow,所以我假设你是在脚本中执行此操作。


3

如果您尝试使用脚本语言运行程序,您可能需要包含脚本语言的完整路径要执行的文件。例如:

exec('/usr/local/bin/node /usr/local/lib/node_modules/uglifycss/uglifycss in.css > out.css');

谢谢,这对我有用。所以我执行了 which gs 然后在我的脚本中使用了输出路径。成功了。 - chesscov77

2
这个错误有时也会具有误导性。它说文件未找到,即使文件确实存在。这可能是由于文件中包含无效的、不可读的特殊字符,这些字符可能是由您使用的编辑器引起的。这个链接可能会在这种情况下对您有所帮助。

-bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory

最好的方法是在整个文件中放置一个echo语句,并验证是否抛出了相同的错误。

0
如果IBM主机JCL在调用Unix脚本的名称末尾有一些额外的字符或数字,则可能会引发此类错误。

0
  1. 前往 C:\Program Files\Git\etc
  2. 使用记事本打开 gitconfig 文件
  3. 将 [core] autocrlf = true 修改为 [core] autocrlf = false

0

除了给出的答案外,还要注意在使用 /bin/sh 作为你的 shell 的情况下,以错误的行尾字符运行脚本文件也可能导致 127 退出代码。

例如,在基于 UNIX 的系统中以 CRLF 行尾字符运行 shell 脚本并使用 /bin/sh shell 时,可能会遇到一些错误,就像我在运行名为 my_test.sh 的脚本后遇到的以下错误:

$ ./my_test.sh
sh: 2: ./my_test.sh: not found
$ echo $?
127

作为一条注释,使用/bin/bash,我得到了126的退出代码,这与gnu.org关于bash的文档相符:

如果找不到命令,则创建用于执行它的子进程返回状态127。如果找到命令但无法执行,则返回状态为126

最后,这是在/bin/bash中运行我的脚本的结果:
arman@Debian-1100:~$ ./my_test.sh
-bash: ./my_test.sh: /bin/bash^M: bad interpreter: No such file or directory
arman@Debian-1100:~$ echo $?
126

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接