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数据库 ...
随机推荐
- Spring Boot 最简单的解决跨域问题
跨域问题(CORS) CORS全称Cross-Origin Resource Sharing,意为跨域资源共享.当一个资源去访问另一个不同域名或者同域名不同端口的资源时,就会发出跨域请求.如果此时另一 ...
- K8S安装Kubesphere
准备工作 安装Helm curl -L https://git.io/get_helm.sh | bash 创建账户 cat > heml-rbac.yaml << EOF apiV ...
- Eureka 集群Demo
由于自己遇到了一些eureka集群疑问,就自己搭了一个集群demo进行测试.测试中通过两个eureka相互注册形成集群,两个服务提供者向eureka中注册服务,两个服务消费者通过feign进行服务调用 ...
- 个人微信公众号搭建Python实现 -个人公众号搭建-总结(14.3.6)
@ 目录 1.主要技术:Flask,requests 2.实现的主要功能 3.目录说明 4.运行方式 关于作者 1.主要技术:Flask,requests requirements.txt如下 req ...
- 单机编排之Docker Compose
当在宿主机启动较多的容器时候,如果都是手动操作会觉得比较麻烦而且容器出错,这个时候推荐使用docker 单机编排工具docker compose,Docker Compose 是docker容器的一种 ...
- kubernetes环境搭建 -k8s笔记(一)
一.环境准备 1.硬件及版本信息: cpu&内存:2核心,2G 网络: 每台vm主机2块网卡,一块NAT用于上网,别一块配置成 "仅主机模式",网段为192.168.100 ...
- python爬虫之解析链接
解析链接 1. urlparse() & urlunparse() urlparse() 是对url链接识别和分段的,API用法如下: urllib.parse.urlparse(urlstr ...
- slice切片函数
clc;clear all;close all; [X,Y,Z,V] = flow;x1 = min(min(min(X)));x2 = max(max(max(X)));y1 = min(min(m ...
- axios前端加密通讯的处理
axios前端加密通讯的处理 今天谈一谈前段时间,项目中遇见的前端axios加解密的处理. 先谈谈项目前景,因为安全的要求,所以我们要把前端所有的请求都得加密与服务端应用进行通讯,当然服务端的响应也是 ...
- Tensorflow2.0-mnist手写数字识别示例
Tensorflow2.0-mnist手写数字识别示例 读书不觉春已深,一寸光阴一寸金. 简介:通过CNN 卷积神经网络训练后识别出手写图片,测试图片mnist数据集中的0.1.2.4. ...