os.system('cmd')在linux和windows系统下返回值的差异
今天,用os.system('cmd')分别在windows和linux平台上执行同一ping命令,命令执行失败时返回码不同,windows为1,而linux下返回为256,如下:
linux下:
>>> import os,sys
>>> os.system('ping -c 1 192.168.1.1 > /dev/null')
0
>>> os.system('ping -c 1 192.168.1.2 > /dev/null')
256
>>>
windows下:
>>> import os,sys
>>> os.system('ping -n 1 -w 200 192.168.1.1 > nul')
0
>>> os.system('ping -n 1 -w 200 192.168.1.2 > nul')
1
>>>
查看system函数的python在线手册如下:
os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command. If command generates any output, it will be sent to the interpreter standard output stream.
On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
On Windows, the return value is that returned by the system shell after running command. The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run; on systems using a non-native shell, consult your shell documentation.
简单总结一下:
os.system('cmd')的功能是在子shell中将cmd字符串作为作为命令执行,cmd命令执行后产生的任何输出将被发送到命令解释器的标准输出流。同时状态返回码也将输出到解释器标准输出流。但值得注意的是:返回状态码在windows和linux下的意义不同,对于windows,返回值就是命令执行后的退出状态码,而linux平台上,从上面的手册描述来看,退出状态码是经过了编码的,编码构成如下:
exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.
也就是说,linux平台上返回值(10进制)需转换为16位二进制数,也就是2个字节,高字节代表退出码。因此,将高字节对应的十进制数才是命令执行后的退出码。
就拿前面的退出码256来说:
os.system(‘cmd’)返回值为0 linux命令返回值也为0.
os.system(‘cmd')返回值为256,对应二进制数为:00000001,00000000,高八位对应十进制为1,因此 linux命令返回值 1
其余以此类推
如果脚本中一定要准确判断返回值,只需对linux平台下的返回值进行截取高8位就可以了。
os.system('cmd')在linux和windows系统下返回值的差异的更多相关文章
- linux 和windows系统下同时可用的UML建模工具(umbrello),超强
原文地址:linux 和windows系统下同时可用的UML建模工具(umbrello),超强 作者:zhangjiakouzf OPEN SOURCE 的 UML建模工具 -- umbrello ...
- 在windows系统下打包linux平台运行的go程序
在windows系统下打包linux平台运行的go程序 1.先在main.go下打包成.exe可执行程序测试代码是否正确 //cd到main.go目录 go build //打包命令 如果打包成功则表 ...
- Windows系统下远程Linux系统
Windows系统下远程Linux系统 工具:Xmanager 启动界面: 配置保存路径(win7): C:\Users\Administrator\AppData\Roaming\NetSarang ...
- Linux系统挂载Windows系统下的共享文件
声明:本文是小编借鉴大神们的经验,仅供学习使用. 第一步:在Windows系统上选择要共享的文件夹,右击“属性”-“共享”-“高级共享”-勾选“共享此文件”-设置共享名-“权限”-“添加”-“高级”- ...
- 在Linux和Windows系统上安装Nginx服务器的教程
在Linux和Windows系统上安装Nginx服务器的教程 1.在CentOS系统上安装Nginx 在 CentOS6 版本的 EPEL 源中,已经加入了 nginx 的 rpm 包,不过此 RP ...
- Linux和Windows系统的远程桌面访问知识(转载)
为新手讲解Linux和Windows系统的远程桌面访问知识 很多新手都是使用Linux和Windows双系统的,它们之间的远程桌面访问是如何连接的,我们就为新手讲解Linux和Windows系统的 ...
- windows系统下用python更新svn和Git
转载请标明出处:http://www.cnblogs.com/zblade/ 最近在思考怎么实现python的一键打包,利用python的跨平台特性,可以实现在windows和mac下均可执行的特点. ...
- windows系统下在dos命令行kill掉被占用的pid (转)
原文出自:http://www.2cto.com/os/201304/203771.html windows系统下在dos命令行kill掉被占用的pid 1.开始-->运行-->c ...
- Mac和Windows系统下Mysql数据库的导入导出
最近在构建数据库的过程中,需要将Mac os系统下的Mysql数据库导出成.sql文件,然后导入到windows系统下的Mysql中.经过学习总结出的步骤如下: 一.Mac os导出Mysql数据库 ...
随机推荐
- HDU4388-Stone Game II-Nim变形
http://acm.hdu.edu.cn/showproblem.php?pid=4388 Nim变形,对一个\(n\)个石子的堆,每次取\(k(0<k<n)\)个(注意不能全取光),同 ...
- PHP留言板制作(MySQL+PHP)
参考视频:https://www.bilibili.com/video/BV1Js411i74j?p=8 环境:phpstudy 2018 PHP 5.X PHPmyadmin ...
- C++webservice接口调用
一.WebService例子 1.准备要调用的webservice接口的wsdl地址,比如网上的查询天气接口:http://ws.webxml.com.cn/WebServices/WeatherWS ...
- 1.Redis基础命令
重要概念 redis是单线程模型,所有命令都会进入一个队列,然后依次被执行. 全局命令 >>>select dbindex #切换数据库,默认有16个库,库标识符为0-15 > ...
- JVM笔记【1】-- 运行时数据区
目录 (一)java内存区域管理 1.1 程序计数器 1.2 虚拟机栈 1.3 本地方法栈 1.4 java堆 1.5 方法区 1.5.1 运行时常量池 (二)直接内存 (一)java内存区域管理 C ...
- 201326JJ
学期(如2020-2021-1) 学号(如:20201326) <信息安全专业导论>第四周学习总结 作业信息 这个作业属于哪个课程 (https://edu.cnblogs.com/cam ...
- idea run dashbord使用
idea 中使用dashbord可以迅速开启多个服务方便进行本地测试 开启步骤 1. 打开idea菜单 view-> toolWindows ->service 选项 2. 打开底部的se ...
- web基础知识,
# web基础 网上冲浪 surfing the Internet weibo.com 域名,主机名,微博服务器的地址名 当用户在地址栏输入一个URL(uniform resource,locator ...
- CSS系列 (03):CSS三大特性
层叠性 层叠性指的是样式的优先级,当产生冲突时以优先级高的为准,优先级相同时取后面定义的属性样式. 继承性 继承性指的是子孙元素可以继承父元素的属性. 记录一下开发中常用的继承属性: 字体系列 fon ...
- 发送微信通知 java 实现
/实现类 @Service public class WeChatServiceImpl implements IWeChatService { @Override public WeChatSend ...