从$返回值127是什么意思?在UNIX中。
当前回答
除了给出的答案之外,请注意,如果您使用/bin/sh作为shell,那么运行带有错误行尾字符的脚本文件还可能导致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
其他回答
如果试图使用脚本语言运行程序,则可能需要包含脚本语言的完整路径和要执行的文件。例如:
exec('/usr/local/bin/node /usr/local/lib/node_modules/uglifycss/uglifycss in.css > out.css');
shell惯例是,一个成功的可执行文件应该以值0退出。其他任何事情都可以解释为bash或您刚刚运行的可执行文件的某些部分出现了某种失败。参见bash手册页的$PIPESTATUS和EXIT STATUS部分:
对于shell而言,退出状态为零的命令已经成功。退出状态 为零表示成功。非零退出状态表示失败。上的命令终止时 致命信号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.
这种错误有时也具有欺骗性。它说文件没有找到,即使文件确实存在。这可能是因为文件中存在无效的不可读的特殊字符,这可能是由您正在使用的编辑器引起的。在这种情况下,这个链接可能会对您有所帮助。
^M:坏的解释器:没有这样的文件或目录
找出是否是这个问题的最好方法是在整个文件中放置一个echo语句,并验证是否抛出了相同的错误。
如果IBM大型机JCL在被调用的unix脚本的名称后面有一些额外的字符或数字,那么它就会抛出这样的错误。
127 -命令未找到
例子:$ caat 错误消息将
bash:
Caat:命令未找到
现在使用echo $?