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 ...
随机推荐
- [POI2013]Taksówki
[POI2013]Taksówki 题目大意: ABC三地在同一条直线上,AC相距\(m(m\le10^{18})\)米,AB相距\(d\),B在AC之间.总共有\(n(n\le5\times10^5 ...
- 负载均衡介绍及Nginx简单实现
负载均衡介绍及Nginx简单实现 负载均衡 负载均衡介绍及Nginx简单实现 1. 介绍 2. 常用的开源软件 2.1 LVS 优点 缺点 2.2 Nginx 优点 缺点 3. 常用的开源反向代理软件 ...
- 快速排序的C++实现
版权声明:本文为博主原创文章,未经博主允许不得转载. 快速排序的C++实现 int Partition(int a[], int low, int high) { int x = a[high];// ...
- c# -- 解决vs使用本地iis运行项目支持局域网访问的问题(附防火墙端口开放步骤)
用vs运行项目时,有时候需要局域网内不同设备进行访问调试~ 以前解决过这个问题,这次用了部新电脑,问题又出现了,改了配置还是不行,原来还差了一步防火墙端口开放访问. 于是写了这篇分享,备忘~ 操作步骤 ...
- 接口开发-集成数据库操作(mybatis)
关于数据存储,最常用的方式就是存到数据库,此篇以MySQL数据库为例,以mybatis框架完成数据库的操作. 一.添加对应依赖 <!-- 数据库:MySQL --> <depende ...
- 使用 IntraWeb (22) - 基本控件之 TIWCalendar
TIWCalendar: 日历控件, 继承于 TIWCustomGrid, 所以它和 TIWGrid 共同属性特多. 它的 Cell 是 TIWCalendarCell 对象, 直接从 TIWGrid ...
- C#操作sqlite数据库使用SQLiteParameter传递参数
C# code public void AddIMG_ENTRY(img_entry model) { StringBuilder strSql = new StringBuilder(); strS ...
- DbContextScope,A simple and flexible way to manage your Entity Framework DbContext instances,by mehdime
DbContextScope A simple and flexible way to manage your Entity Framework DbContext instances. DbCont ...
- Entity Framework 6 (7) vs NHibernate 4: DDD perspective(纯净DDD很难很难...)
There is quite a bit of Entity Framework vs NHibernate comparisons on the web already, but all of th ...
- Unity3D实践系列08, MonoBehaviour类的各种触发事件
在脚本的生命周期中,有Awake, Start, FixedUpdate, Update, LateUpdate等方法,其实这些属于MonoBehaviour类的事件响应方法,是MonoBehavio ...