【问题标题】:127 Return code from $?127 来自 $? 的返回码?
【发布时间】:2010-12-18 07:15:03
【问题描述】:

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

【问题讨论】:

    标签: process unix


    【解决方案1】:

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

    【讨论】:

    • 如果 bash 脚本没有模式 "+x" 但确实存在,也会发生这种情况。
    • 您可以尝试使用which [program] 来查看操作系统正在使用哪个二进制文件。如果它出现空,下一步是检查执行位和路径。
    • @cr125rider, which 不是特别准确——它不知道别名、shell 函数、PATH 查找记忆或 shell 状态内部的其他因素。使用type 会更好,这是一个内置的 shell,它知道所有这些事情。
    • 这也发生在我的一个包含 Windows 换行符的文件中。将行尾更正为 unix 格式解决了问题
    • @MatthewKremer:实际上,当我尝试调用非可执行文件(无论其内容如何)时,我得到的是126 (Permission denied),而不是127;同样,尝试执行 目录 也会导致 126 (is a directory)。
    【解决方案2】:

    一般意思是:

    127 - 找不到命令

    但也可能意味着找到了命令
    但是命令所需的库找不到

    【讨论】:

      【解决方案3】:

      127 - command not found

      示例:$caat 错误信息将

      重击:

      caat:找不到命令

      现在您使用echo $? 进行检查

      【讨论】:

        【解决方案4】:

        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.
        

        【讨论】:

          【解决方案5】:

          除了最后一个退出的进程退出状态为127之外,没有特殊含义。

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

          编辑:
          不能立即推断,除了控制台上的输出,但这是堆栈溢出,所以我假设您是在脚本中执行此操作。

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

            • 谢谢,这对我有用。所以我做了 which gs 然后在我的脚本中使用了输出路径。工作..
            【解决方案7】:

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

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

            找出是否是这个问题的最佳方法是在整个文件中简单地放置一个 echo 语句并验证是否引发了相同的错误。

            【讨论】:

              【解决方案8】:

              如果 IBM 大型机 JCL 在被调用的 unix 脚本名称的末尾有一些额外的字符或数字,那么它可能会引发此类错误。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-10-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-04-03
                • 2018-10-09
                • 1970-01-01
                • 2018-03-04
                相关资源
                最近更新 更多