127 从 $ 返回代码?

发布于 2024-08-12 00:13:29 字数 29 浏览 3 评论 0原文

$返回值127是什么意思?在 UNIX 中。

What is the meaning of return value 127 from $? in UNIX.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

暖阳 2024-08-19 00:13:29

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

Value 127 is returned by /bin/sh when the given command is not found within your PATH 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.

我的黑色迷你裙 2024-08-19 00:13:29

一般来说,这意味着:

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.

白首有我共你 2024-08-19 00:13:29

127 - 未找到命令

示例:$caat
bash会出现错误消息

caat:找不到命令

您使用 echo $? 检查

127 - command not found

example: $caat
The error message will

bash:

caat: command not found

now you check using echo $?

淡忘如思 2024-08-19 00:13:29

shell 约定是成功的可执行文件应以值 0 退出。任何其他情况都可以解释为 bash 或刚刚运行的可执行文件的某种失败。另请参阅 bash 手册页的 $PIPESTATUSEXIT STATUS 部分:

 出于 shell 的目的,以零退出状态退出的命令已成功。退出状态
   为零表示成功。非零退出状态表示失败。当命令终止于
   fatal 信号 N,bash 使用 128+N 的值作为退出状态。
   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.

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:

   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.
烟─花易冷 2024-08-19 00:13:29

它没有特殊含义,除了最后一个退出的进程以 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.

束缚m 2024-08-19 00:13:29

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

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

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:

exec('/usr/local/bin/node /usr/local/lib/node_modules/uglifycss/uglifycss in.css > out.css');
风透绣罗衣 2024-08-19 00:13:29

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

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

妥活 2024-08-19 00:13:29

如果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.

不念旧人 2024-08-19 00:13:29

除了给出的答案之外,请注意,如果您使用 /bin/sh 作为,运行带有不正确行尾字符的脚本文件也可能导致 127 退出代码你的外壳。

例如,如果您在基于 UNIX 的系统和 /bin/sh shell 中运行带有 CRLF 行尾字符的 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

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 named my_test.sh :

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

As a note, using /bin/bash, I got 126 exit code, which is in accordance with gnu.org documentation about the bash :

If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.

Finally, here is the result of running my script in /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
谁人与我共长歌 2024-08-19 00:13:29
  1. 转到 C:\Program Files\Git\etc
  2. 打开 gitconfig
  3. 用记事本更改
    [核]
    自动crlf=真

    [核]
    自动crlf =假
  1. go to C:\Program Files\Git\etc
  2. open gitconfig with notepad
  3. change
    [core]
    autocrlf = true
    To
    [core]
    autocrlf = false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文