编程中经常需要在程序中使用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. JDK1.8快速入门

    JDK8提供了非常多的便捷用法和语法糖,其编码效率几乎接近于C#开发,maven则是java目前为止最赞的jar包管理和build工具,这两部分内容都不算多,就合并到一起了. 愿编写java代码的过程 ...

  2. speech模块实现语音识别

    1.pip安装speech.pywin32 pip install speech pip install pywin32 2.例子 #!/usr/bin/python # coding:utf-8 f ...

  3. tesseract_ocr+pytesseract图像识别

    一.windows安装配置 其他系统安装配置参考github:https://github.com/tesseract-ocr/tesseract/wiki 下载tesseract-ocr参考:htt ...

  4. NOIP2015其余几道题

    T1: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> # ...

  5. 深入理解指针—>指针函数与函数指针的区别

    一. 在学习过程中发现这"指针函数"与"函数指针"容易搞错,所以今天,我自己想一次把它搞清楚,找了一些资料,首先它们之间的定义: 1.指针函数是指带指针的函数, ...

  6. 【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树

    3218: a + b Problem Time Limit: 20 Sec  Memory Limit: 40 MBSubmit: 1320  Solved: 498[Submit][Status] ...

  7. OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉

    Challenge Your Template 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/G Description ACM ...

  8. iOS 七牛多张图片上传

    -(void)uploadImages:(NSArray *)images atIndex:(NSInteger)index token:(NSString *)token uploadManager ...

  9. WebMvcConfigurerAdapter已经过时的问题解决

    spring 5开始已经废弃WebMvcConfigurerAdapter,替代的是WebMvcConfigurer接口. 参考: https://blog.csdn.net/lenkvin/arti ...

  10. Revit Family API 添加几何实体

    先创建一个封闭曲线createProfileLShape();再创建实体,这里需要手工画一个参考平面; ; i < nVerts; ++i)        {            Line l ...