编程中经常需要在程序中使用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命令的更多相关文章

  1. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  2. python中执行shell命令的几个方法小结

    原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...

  3. Android 用java语言执行Shell命令

    最近项目中需要用到java语言来执行shell命令,在网上查了资料, 把自己在项目里用到的命令整理成了工具类开放给大家,希望对大家有用.功能不全,后期我会慢慢添加整合. public class Sh ...

  4. python中执行shell命令行read结果

    +++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...

  5. python(6)-执行shell命令

    可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.* ...

  6. 「Python」6种python中执行shell命令方法

    用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...

  7. Linux下C语言执行shell命令

    有时候在代码中需要使用到shell命令的情况,下面就介绍一下怎么在C语言中调用shell命令: 这里使用popen来实现,关于popen的介绍,查看 http://man7.org/linux/man ...

  8. python中执行shell命令的几个方法

    1.os.system() a=os.system("df -hT | awk 'NR==3{print $(NF-1)}'") 该命令会在页面上打印输出结果,但变量不会保留结果, ...

  9. python批量执行shell命令

    [root@master ~]# cat a.py #!/usr/bin/python # -*- coding:UTF- -*- import subprocess def fun(): subpr ...

随机推荐

  1. poj 1797 最短路变形dijkstra

    题意:题目大意:有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量 链接:点我 解题思路:其实这个求最大边可以近似于 ...

  2. 20172302『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结

    一.结对对象 姓名:周亚杰 学号:20172302 担任角色:驾驶员(周亚杰) 伙伴第二周博客地址 二.本周内容 (一)继续编写上周未完成代码 1.本周继续编写代码,使代码支持分数类计算 2.相关过程 ...

  3. 重读JavaScript高级程序设计

    不断更新中--- 第三章 基本概念 1.变量声明但未初始化值是undefined,而未声明的变量只能执行typeof操作,并且未初始化和未声明用typeof都同样返回undefined 2.Numbe ...

  4. Android笔记(一):this 的表示范围和 Context

    this 的表示范围 this 指的是它所在的直接所在的类. 例如: public class MyClass{ int num; public MyClass(int num){ this.num ...

  5. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  6. STM32F1-workarea : how to drive a WS2812 RGB LED using PWM and DMA

    how to drive a WS2812 RGB LED using PWM and DMA #include <stm32f10x.h> void Delay(__IO uint32_ ...

  7. jquery 网页局部打印总结

    最近开发过程中遇到了js局部打印的功能,在网上找相关的资料,最终找到了juery.jqprint-0.3.js 和jquery.PrintArea.js两种. 最初使用的是jquery.jqprint ...

  8. linux socket TCP UDP bind 同义IP和port

    //TCP and UDP can bind to the same IP & port. #include <sys/types.h> #include <sys/sock ...

  9. codeforces round #257 div2 C、D

    本来应该认真做这场的.思路都是正确的. C题,是先该横切完或竖切完,无法满足刀数要求.再考虑横切+竖切(竖切+横切), 由于横切+竖切(或竖切+横切)会对分割的东西产生交叉份数.从而最小的部分不会尽可 ...

  10. 对数据集“dsArea”执行查询失败。 (rsErrorExecutingCommand),Query execution failed for dataset 'dsArea'. (rsErrorExecutingCommand),Manually process the TFS data warehouse and analysis services cube

    错误提示: 处理报表时出错. (rsProcessingAborted)对数据集“dsArea”执行查询失败. (rsErrorExecutingCommand)Team System 多维数据集或者 ...