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数据库 ...
随机推荐
- VMware & centos 7 配置克隆虚拟机
VMware & centos 7 配置克隆虚拟机 之前一直使用的是centos6,买了新电脑试了一下centos7,安装过程果然采坑不少,下面是我成功安装的过程 克隆虚拟机 安装虚拟机,安装 ...
- MyBatisPlus-快速入门
一.创建Maven工程 二.pom.xml文件 引入 MyBatis Plus 的依赖, <?xml version="1.0" encoding="UTF-8&q ...
- 根据json数据和HTML模板,渲染嵌套的HTML
2020-12-22 11:53:23 星期二 场景, HTML模板是多个div嵌套, 里边有列表, 也有键值对, 与之匹配的有一个json数据, 需要根据json去渲染这个HTML DOM 示例截图 ...
- Autofac的基本使用---1、前言
Autofac的基本使用---目录 代码地址 https://github.com/catbiscuit/AutofacStudy 参考网上的大神,原博文地址 https://www.cnblogs. ...
- MySQL、DM 行转列及字段去重(Group_Concat())
最近在使用数据库迁移适配,由MySQL 库迁移到达梦数据库,其中进行行转列时,MySQL转换达梦sql语法有些问题,特记录. 在MySQL 下有Group_Concat(expr) ,在达梦及神通数 ...
- 使用OpenOffice实现文档预览
概述 使用OpenOffice将 office文档转为pdf,然后再将pdf转为图片,实现文档预览的功能. 依赖组件 OpenOffice.org或者LibreOffice JODConverter ...
- 将后端返回的数据在jsp中拼接成table列表
//先下载jquery js文件 放入项目中 jsp文件内容 <%@ page language="java" pageEncoding="UTF-8"% ...
- Fast Bokeh Effects Using Low-Rank Linear Filters
Fast Bokeh Effects Using Low-Rank Linear Filters paper地址:https://www.researchgate.net/publication/27 ...
- PP主数据-物料主数据
一.PP物料主数据:PP的物料主数据,是对应到系统的组织架构的,不同的组织层次,都有各自的主数据需要创建. (1),一般数据:一般数据是在集团层面的主数据,主要包括:物料编码.物料描述.辅助计量单位以 ...
- [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点
方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ...