Linux命令返回值为什么有时会和预计的不一样
2014-02-23 19:22
253 查看
我建立一个shell文件 script.sh,内容如下:
#!/bin/bash
exit 888
然后我再执行下面的语句
[root@localhost]# chmod u+x script.sh
[root@localhost]# ./script.sh
[root@localhost]# echo $? # ?为shell变量,代表最后执行的程序的返回值。用echo $解析出来
120
注:exit 不加参数表示,退出。返回值不变,即为最后一个命令的返回值。(If n is omitted, the exit status is that of the last command executed. )
exit n 加参数表示,退出。设置返回值为n。(Cause the shell to exit with a status of n.)
那么既然我们设置了exit 888,命令返回值就应该是888才对,为什么结果是120?
这是因为,Linux默认返回值限制为8bits。我们返回888,二进制为1101111000,超出了两位,从高位去掉两位即得01111000,也就是120.
同理,return的返回值也是八位,return 777的话,截取低八位就成了9,返回值为9.
return -1的话会报错,其他负数也会报错。
#!/bin/bash
exit 888
然后我再执行下面的语句
[root@localhost]# chmod u+x script.sh
[root@localhost]# ./script.sh
[root@localhost]# echo $? # ?为shell变量,代表最后执行的程序的返回值。用echo $解析出来
120
注:exit 不加参数表示,退出。返回值不变,即为最后一个命令的返回值。(If n is omitted, the exit status is that of the last command executed. )
exit n 加参数表示,退出。设置返回值为n。(Cause the shell to exit with a status of n.)
那么既然我们设置了exit 888,命令返回值就应该是888才对,为什么结果是120?
这是因为,Linux默认返回值限制为8bits。我们返回888,二进制为1101111000,超出了两位,从高位去掉两位即得01111000,也就是120.
同理,return的返回值也是八位,return 777的话,截取低八位就成了9,返回值为9.
return -1的话会报错,其他负数也会报错。
相关文章推荐
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- 基于 Linux 集群环境上 GPFS 的问题诊断
- 谁是桌面王者?Win PK Linux三大镇山之宝
- vivi下重新调整分区
- Linux VS Unix:Linux欲一统天下 Unix不死
- linux下设定环境变量
- Linux下修改MySQL编码的方法
- Linux串口通信
- 从Windows系统下访问Linux分区相关软件