【发布时间】:2010-12-18 07:15:03
【问题描述】:
$返回值127是什么意思?在 UNIX 中。
【问题讨论】:
$返回值127是什么意思?在 UNIX 中。
【问题讨论】:
当在您的PATH 系统变量中找不到给定命令并且它不是内置的 shell 命令时,/bin/sh 返回值 127。换句话说,系统不理解您的命令,因为它不知道在哪里可以找到您要调用的二进制文件。
【讨论】:
which [program] 来查看操作系统正在使用哪个二进制文件。如果它出现空,下一步是检查执行位和路径。
which 不是特别准确——它不知道别名、shell 函数、PATH 查找记忆或 shell 状态内部的其他因素。使用type 会更好,这是一个内置的 shell,它知道所有这些事情。
126 (Permission denied),而不是127;同样,尝试执行 目录 也会导致 126 (is a directory)。
一般意思是:
127 - 找不到命令
但也可能意味着找到了命令,
但是命令所需的库找不到。
【讨论】:
127 - command not found
示例:$caat 错误信息将
重击:
caat:找不到命令
现在您使用echo $? 进行检查
【讨论】:
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.
【讨论】:
除了最后一个退出的进程退出状态为127之外,没有特殊含义。
但是,bash 也使用它(假设您使用 bash 作为 shell)告诉您无法执行您尝试执行的命令(即找不到它)。不幸的是,如果进程以状态 127 退出,或者如果找不到,则不能立即推断。
编辑:
不能立即推断,除了控制台上的输出,但这是堆栈溢出,所以我假设您是在脚本中执行此操作。
【讨论】:
如果您尝试使用脚本语言运行程序,您可能需要包含脚本语言的完整路径和要执行的文件。例如:
exec('/usr/local/bin/node /usr/local/lib/node_modules/uglifycss/uglifycss in.css > out.css');
【讨论】:
这个错误有时也具有欺骗性。它说即使文件确实存在,也找不到文件。这可能是由于您使用的编辑器可能导致文件中存在无效的不可读特殊字符。在这种情况下,此链接可能会对您有所帮助。
-bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory
找出是否是这个问题的最佳方法是在整个文件中简单地放置一个 echo 语句并验证是否引发了相同的错误。
【讨论】:
如果 IBM 大型机 JCL 在被调用的 unix 脚本名称的末尾有一些额外的字符或数字,那么它可能会引发此类错误。
【讨论】: