从$返回值127是什么意思?在UNIX中。
当前回答
一般的意思是:
127 -命令未找到
但它也可以表示找到了命令, 但是没有找到命令所需要的库。
其他回答
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.
一般的意思是:
127 -命令未找到
但它也可以表示找到了命令, 但是没有找到命令所需要的库。
当给定的命令不在PATH系统变量中并且它不是内置的shell命令时,/bin/sh会返回值127。换句话说,系统不理解您的命令,因为它不知道在哪里找到您试图调用的二进制文件。
它没有特殊的含义,只是最后一个要退出的进程的退出状态是127。
然而,bash也使用它(假设您将bash用作shell)来告诉您尝试执行的命令无法执行(即无法找到它)。不幸的是,如果进程以状态127退出,或者如果它找不到,则不能立即推断。
编辑: 不能立即推断,除了控制台的输出,但这是堆栈溢出,所以我假设您在脚本中执行此操作。
除了给出的答案之外,请注意,如果您使用/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
推荐文章
- 查看PS命令的全部输出
- 确保一次只运行一个shell脚本实例的快速方法
- 如何从命令行通过mysql运行一个查询?
- 在创建守护进程时执行双fork的原因是什么?
- 对以制表符分隔的文件进行排序
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务
- 在OSX中永久设置PATH环境变量
- 如何在C程序中获取当前目录?
- 如何在Bash中逐行合并两个文件
- 如何从远程SSH会话发送数据到本地剪贴板
- 我如何得到bash完成工作与别名?
- SSH端口转发~/。ssh /配置文件?