127从$返回代码?

Sac*_*iya 269 unix process

返回值127的含义是什么?在UNIX中.

Old*_*ool 400

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

  • 如果bash脚本没有"+ x"模式但确实存在,也会发生这种情况. (48认同)
  • @ cr125rider,`which`不是特别准确 - 它不知道别名,shell函数,PATH查找memoization或shell状态内部的其他因素.使用`type`这个内置的shell知道所有这些东西要好得多. (10认同)
  • 这也发生在一个有Windows换行的文件中.将行结尾校正为unix格式解决了这个问题 (5认同)
  • @MatthewKremer:实际上,当我试图调用一个不可执行的文件(无论其内容如何)时,我得到"126"("Permission denied"),而不是"127"; 类似地,尝试执行_directory_也会导致`126`(`是目录`). (4认同)
  • 您可以尝试使用`which [program]`来查看操作系统使用的二进制文件.如果它为空,则下一步是检查执行位和PATH. (3认同)

oki*_*gan 56

通常它意味着:

127 - 找不到命令

但它也可能意味着命令被发现,
但被命令时所需的库中没有找到.


Ani*_*ani 13

127 - command not found

示例:$ caat错误消息将

庆典:

caat:命令未找到

现在你检查使用 echo $?

  • 非常好的例子! (2认同)

小智 9

一个shell约定是一个成功的可执行文件应该以值0退出.任何其他东西都可以被解释为某种类型的失败,部分是bash或刚刚运行的可执行文件.另请参阅bash手册页的$ PIPESTATUSEXIT 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.
Run Code Online (Sandbox Code Playgroud)
   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.
Run Code Online (Sandbox Code Playgroud)


fal*_*tro 8

它没有特殊意义,除了退出的最后一个进程,退出状态为127.

但是,它也被bash使用(假设您使用bash作为shell)告诉您无法执行您尝试执行的命令(即无法找到它).遗憾的是,如果进程以状态127退出,或者如果找不到,则不能立即进行推理.

编辑:
除了控制台上的输出外,不能立即推导,但这是堆栈溢出,所以我假设你在脚本中这样做.