编程中经常需要在程序中使用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. map的基本操作函数

    C++ maps是一种关联式容器,包含“关键字/值”对 begin()         返回指向map头部的迭代器 clear()        删除所有元素 count()         返回指定 ...

  2. iOS 一句话获取日期和星期几

    UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, )]; da ...

  3. C# Activex调用USB摄像头--附带源码

    前言 最近在整理一些自己写过的东西,也算是重新熟悉一下并且优化一下吧. 需求:获取本地USB摄像头视频显示,并且获取图片数据给底层做人脸识别. 记得当时直接采用H5已经做好了,调试好了....结果放上 ...

  4. 链表用途&&数组效率&&链表效率&&链表优缺点

    三大数据结构的实现方式 数据结构 实现方式 栈  数组/单链表 队列  数组/双端链表 优先级队列 数组/堆/有序链表 双端队列 双向链表 数组与链表实现方式的比较 数组与链表都很快 如果能精确预测栈 ...

  5. Web Service(一):初识

    1. 前言 cxf 在项目中应用好久了,一直没有写总结,现在补上. 由于cxf 属于Web Service的一个实现,所以先学习和总结一下Web Service,作为学习cxf的基础. 2. WebS ...

  6. Xamarin-Android_BaseAdapter 简单的复用

    Xamarin-Android_BaseAdapter 简单的复用 缘由: 本人是一枚 小菜 初学Xamarin-Android  正在学习ListView 控件 发现这个控件的自定义布局 用的那叫一 ...

  7. Python基础笔记(三)

    1. 循环与流程控制 (1) for myList1 = ["A", "B", "C", "D"] # 正序遍历 for ...

  8. adjustPan 没作用

    onCreate里加了这个 影响了 . getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);getWindow ...

  9. listview重新计算高度

    将xml中的ListView改用下面的ListViewForScrollView //ScrollView中嵌入ListView,让ListView全显示出来 public class ListVie ...

  10. 求有向图的强连通分量个数 之 Kosaraju算法

    代码: #include<cstdio> #include<cstring> #include<iostream> using namespace std; ][] ...