127 从 $ 返回代码?
$返回值127是什么意思?在 UNIX 中。
What is the meaning of return value 127 from $? in UNIX.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
$返回值127是什么意思?在 UNIX 中。
What is the meaning of return value 127 from $? in UNIX.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
当在
PATH
系统变量中找不到给定命令并且它不是内置 shell 命令时,/bin/sh
返回值 127。换句话说,系统不理解您的命令,因为它不知道在哪里可以找到您尝试调用的二进制文件。Value 127 is returned by
/bin/sh
when the given command is not found within yourPATH
system variable and it is not a built-in shell command. In other words, the system doesn't understand your command, because it doesn't know where to find the binary you're trying to call.一般来说,这意味着:
127 - 未找到命令
,但也可能意味着命令已找到,
但是未找到命令所需的库。
Generally it means:
127 - command not found
but it can also mean that the command is found,
but a library that is required by the command is NOT found.
127 - 未找到命令
示例:$caat
bash会出现错误消息
:
您使用
echo $?
检查127 - command not found
example: $caat
The error message will
bash:
now you check using
echo $?
shell 约定是成功的可执行文件应以值 0 退出。任何其他情况都可以解释为 bash 或刚刚运行的可执行文件的某种失败。另请参阅 bash 手册页的 $PIPESTATUS 和 EXIT STATUS 部分:
A shell convention is that a successful executable should exit with the value 0. Anything else can be interpreted as a failure of some sort, on part of bash or the executable you that just ran. See also $PIPESTATUS and the EXIT STATUS section of the bash man page:
它没有特殊含义,除了最后一个退出的进程以 127 的退出状态退出之外。
但是,bash(假设您使用 bash 作为 shell)也使用它来告诉您您尝试过的命令执行无法执行(即找不到)。遗憾的是,如果进程以状态 127 退出,或者找不到进程,则不能立即推断出这一点。
编辑:
除了控制台上的输出之外,不能立即推断,但这是堆栈溢出,所以我假设您在脚本中执行此操作。
It has no special meaning, other than that the last process to exit did so with an exit status of 127.
However, it is also used by bash (assuming you're using bash as a shell) to tell you that the command you tried to execute couldn't be executed (i.e. it couldn't be found). It's unfortunately not immediately deducible though, if the process exited with status 127, or if it couldn't found.
EDIT:
Not immediately deducible, except for the output on the console, but this is stack overflow, so I assume you're doing this in a script.
如果您尝试使用脚本语言运行程序,则可能需要包含脚本语言的完整路径和要执行的文件。例如:
If you're trying to run a program using a scripting language, you may need to include the full path of the scripting language and the file to execute. For example:
这个错误有时也是具有欺骗性的。它说即使文件确实存在,但未找到文件。这可能是因为文件中存在无效的不可读的特殊字符,这可能是由您使用的编辑器引起的。在这种情况下,此链接可能会对您有所帮助。
-bash: ./my_script: / bin/bash^M: 错误的解释器:没有这样的文件或目录
找出是否是这个问题的最佳方法是简单地在整个文件中放置一个 echo 语句,并验证是否抛出相同的错误。
This error is also at times deceiving. It says file is not found even though the files is indeed present. It could be because of invalid unreadable special characters present in the files that could be caused by the editor you are using. This link might help you in such cases.
-bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory
The best way to find out if it is this issue is to simple place an echo statement in the entire file and verify if the same error is thrown.
如果IBM大型机JCL在被调用的unix脚本名称末尾有一些额外的字符或数字,那么它可能会抛出这样的错误。
If the IBM mainframe JCL has some extra characters or numbers at the end of the name of unix script being called then it can throw such error.
除了给出的答案之外,请注意,如果您使用
/bin/sh
作为,运行带有不正确行尾字符的脚本文件也可能导致127
退出代码你的外壳。例如,如果您在基于 UNIX 的系统和
/bin/sh
shell 中运行带有 CRLF 行尾字符的 shell 脚本,则可能会遇到如下错误运行名为my_test.sh
的脚本后,我得到了:请注意,使用
/bin/bash
,我得到了126
退出代码,这符合 gnu.org 有关的文档bash :最后,这是在
/bin/bash
中运行脚本的结果:In addition to the given answers, note that running a script file with incorrect end-of-line characters could also result in
127
exit code if you use/bin/sh
as your shell.As an example, if you run a shell script with CRLF end-of-line characters in a UNIX-based system and in the
/bin/sh
shell, it is possible to encounter some errors like the following I've got after running my script namedmy_test.sh
:As a note, using
/bin/bash
, I got126
exit code, which is in accordance with gnu.org documentation about thebash
:Finally, here is the result of running my script in
/bin/bash
:[核]
自动crlf=真
到
[核]
自动crlf =假
[core]
autocrlf = true
To
[core]
autocrlf = false