C++/Php/Python 语言执行shell命令
编程中经常需要在程序中使用shell命令来简化程序,这里记录一下。
1. C++ 执行shell命令
#include <iostream>
#include <string>
#include <stdio.h> int exec_cmd(std::string cmd, std::string &res){
if (cmd.size() == ){ //cmd is empty
return -;
} char buffer[] = {};
std::string result = "";
FILE *pin = popen(cmd.c_str(), "r");
if (!pin) { //popen failed
return -;
} res.clear();
while(!feof(pin)){
if(fgets(buffer, sizeof(buffer), pin) != NULL){
result += buffer;
}
} res = result;
return pclose(pin); //-1:pclose failed; else shell ret
} int main(){
std::string cmd = "ls -ial";
std::string res; std::cout << "ret = " << exec_cmd(cmd, res) << std::endl;
std::cout << res << std::endl; return ;
}
2. Php执行shell命令
<?php
$cmd = "wc -l ./test.php";
exec($cmd, $output, $code); echo $code."\n";
print_r($output);
?>
3. Python执行shell命令
import commands
status, output = commands.getstatusoutput('ls -lt')
print status
print output
C++/Php/Python 语言执行shell命令的更多相关文章
- python中执行shell命令的几个方法小结(转载)
转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...
- python中执行shell命令的几个方法小结
原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...
- Android 用java语言执行Shell命令
最近项目中需要用到java语言来执行shell命令,在网上查了资料, 把自己在项目里用到的命令整理成了工具类开放给大家,希望对大家有用.功能不全,后期我会慢慢添加整合. public class Sh ...
- python中执行shell命令行read结果
+++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...
- python(6)-执行shell命令
可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen* --废弃 popen2.* --废弃 commands.* ...
- 「Python」6种python中执行shell命令方法
用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...
- Linux下C语言执行shell命令
有时候在代码中需要使用到shell命令的情况,下面就介绍一下怎么在C语言中调用shell命令: 这里使用popen来实现,关于popen的介绍,查看 http://man7.org/linux/man ...
- python中执行shell命令的几个方法
1.os.system() a=os.system("df -hT | awk 'NR==3{print $(NF-1)}'") 该命令会在页面上打印输出结果,但变量不会保留结果, ...
- python批量执行shell命令
[root@master ~]# cat a.py #!/usr/bin/python # -*- coding:UTF- -*- import subprocess def fun(): subpr ...
随机推荐
- map的基本操作函数
C++ maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定 ...
- .babelrc文件的一些简单的配置
首先现在根目录先生存.babelrc文件 这个文件是用来设置转码的规则和插件的 如果想使用es6语法,必须安装插件 npm install babel-preset-es2015 { "pr ...
- GIT(7)----强制用远程代码覆盖本地修改
清除本地修改 git reset --hard 拉代码 git pull Git Pull While Ignoring Local Changes? git pull 并强制覆盖本地修改
- OpenNI2 + NiTE2开发教程
发现了一个非常不错的关于自然交互OpeNI2+NiTE2的资源,非常感谢Heresy,这里分享链接: OpenNI 2.x 教学文章(转载自:Heresy博客,地址:https://kheresy.w ...
- Timer-triggered memory-to-memory DMA transfer demonstrator
http://www.efton.sk/STM32/bt.c // Timer-triggered memory-to-memory DMA transfer demonstrator for STM ...
- Ubuntu 14 安装Java(JRE、JDK)、Maven
JRE vs OpenJDK vs Oracle JDK JRE(Java Runtime Environment),它是你运行一个基于Java语言应用程序的所正常需要的环境.如果你不是一个程序员的话 ...
- 《Go学习笔记 . 雨痕》方法
一.定义 方法 是与对象实例绑定的特殊函数. 方法 是面向对象编程的基本概念,用于维护和展示对象的自身状态.对象是内敛的,每个实例都有各自不同的独立特征,以 属性 和 方法 来暴露对外通信接口.普通函 ...
- systemtap跟踪C
1.[root@localhost ~]# rpm -qi glibcName : glibc Relocations: (not rel ...
- SystemParametersinfo用法(二)
SystemParametersinfo用法(二) SPI_SETDOUBLECLKHEGHT:将ulParam参数的值设为双击矩形区域的高度.双击矩形区域是指双击中的第2次点击时鼠标指针必须落在的区 ...
- How AOT compares to a traditional JIT compiler
Ahead-of-Time (AOT) compilation is in contrast to Just-in-Time compilation (JIT). In a nutshell, .NE ...