2009-11-19 146 views

回答

273

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

+24

如果bash脚本没有模式“+ x”但确实存在,也会发生这种情况。 – MatthewKremer 2014-03-04 20:48:02

+0

您可以尝试使用'which [程序]'来查看操作系统正在使用哪个二进制文件。如果它变空,下一步是检查执行位和PATH。 – cr125rider 2014-06-12 16:33:58

+7

@ cr125rider,''不是特别准确 - 它不知道别名,shell函数,PATH查找记忆或shell状态内部的其他因素。使用'type'更好,这是一个了解所有这些事情的shell内建函数。 – 2014-09-11 22:45:13

6

它没有特殊的意义,比过去的进程退出与127

但是,它也被用于庆典的退出状态这样做(假设你使用bash作为一个壳)等告诉你你试图执行的命令不能执行(即找不到它)。不幸的是,如果进程退出状态为127,或者无法找到,那么不会立即推论出来。

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

6

shell约定是成功的可执行文件应该以0值退出。其他任何情况都可以解释为某种类型的故障,或者是您刚刚运行的bash或可执行文件的一部分。另见$ PIPESTATUS和bash的手册页的退出状态部分:

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

127 - command not found

例如:$ CAAT 错误消息将

bash:

CAAT:找不到命令

现在你检查使用echo $?

+0

非常好的例子! – Victor 2018-02-02 14:57:55

30

一般它是指:

127 - 找不到

但命令时,它也意味着命令发现,
但是需要命令未找到

1

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

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

谢谢,这对我很有用。所以我做了哪些gs,然后在我的脚本中使用了输出路径。工作.. – Juan 2017-08-02 16:14:56

0

这个错误也有时候是骗人的。它说,即使文件确实存在,文件也没有找到。这可能是由于您使用的编辑器可能导致文件中存在无效的不可读特殊字符。在这种情况下,这个链接可能会帮助你。

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

,以找出是否是此问题的最好的办法是简单的地方,在整个文件中的echo语句,如果同样的错误被抛出验证。