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数据库 ...
随机推荐
- Redis数据持久化(RDB、AOF)
1. 简介 Redis作为内存型数据库,数据都保存在内存中,如果重启或意外宕机后,数据会全部丢失.因此,Redis提供了完善的持久化机制,将内存中的数据持久化到磁盘上,避免了完整性和安全性的问题, ...
- hydra-microservice 中文手册(3W字预警)
Hydras 是什么? Hydra 是一个 NodeJS 包(技术栈不是重点,思想!思想!思想!),它有助于构建分布式应用程序,比如微服务. Hydra 提供服务发现(service discover ...
- 实验4 汇编应用编程和c语言程序反汇编分析
1. 实验任务1 教材「实验9 根据材料编程」(P187-189)编程:在屏幕中间分别显示绿色.绿底红色.白底蓝色的字符串'welcome to masm!'. 解题思路:根据学习的知识,我知道该页在 ...
- 二、Electron + Webpack + Vue 搭建开发环境及打包安装
目录 Webpack + Vue 搭建开发环境及打包安装 ------- 打包渲染进程 Electron + Webpack 搭建开发环境及打包安装 ------- 打包主进程 Electron + ...
- Durid的特点
Durid的特点 1.为什么会有Durid? 创建Druid的最初意图主要是为了解决查询延迟问题,当时试图使用Hadoop来实现交互式查询分析,但是很难满足实时分析的需要.而Druid提供了以交互方式 ...
- C# 学习第七天
P96 面向对象的概念 ①先有的 面向过程 --------> 然后才衍生出面向对象的思想 ②面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作 比如说 把大象塞进冰箱去 ③面向过 ...
- 高可用K8S构建3master+3node+keepalived+haproxy
视频地址:https://www.bilibili.com/video/BV1w4411y7Go?p=66 所需安装包在视频评论区 安装准备 系统: CentOS-7-x86_64-Minimal-1 ...
- Matplotlib处理csv文件
csv模块包含在python标准库中,可用于分析CSV文件中的数据行,让我们能够快速提取感兴趣的值.首先绘制一个简单的折线图: 1 #!usr/bin/env python 2 #*-*Coding= ...
- iOS常见遍历方法汇总
一.for循环 NSArray *iosArray = @[@"L", @"O", @"V", @"E", @" ...
- sql 查询条件为拼接字符串 不能使用IN 使用patindex查询结果集
题目: 求组织机构ID在('5dc8de20-9f2f-465e-afcc-f69abecaee50','63549b63-1e0d-4269-98f4-013869d7f211','f7316bf3 ...