博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++/Php/Python 语言执行shell命令
阅读量:7117 次
发布时间:2019-06-28

本文共 1007 字,大约阅读时间需要 3 分钟。

编程中经常需要在程序中使用shell命令来简化程序,这里记录一下。

1. C++ 执行shell命令

1 #include 
2 #include
3 #include
4 5 int exec_cmd(std::string cmd, std::string &res){ 6 if (cmd.size() == 0){ //cmd is empty 7 return -1; 8 } 9 10 char buffer[1024] = { 0}; 11 std::string result = ""; 12 FILE *pin = popen(cmd.c_str(), "r"); 13 if (!pin) { //popen failed 14 return -1; 15 } 16 17 res.clear(); 18 while(!feof(pin)){ 19 if(fgets(buffer, sizeof(buffer), pin) != NULL){ 20 result += buffer; 21 } 22 } 23 24 res = result; 25 return pclose(pin); //-1:pclose failed; else shell ret 26 } 27 28 int main(){ 29 std::string cmd = "ls -ial"; 30 std::string res; 31 32 std::cout << "ret = " << exec_cmd(cmd, res) << std::endl; 33 std::cout << res << std::endl; 34 35 return 0; 36 }

2. Php执行shell命令

1 

3. Python执行shell命令

1 import commands2 3 status, output = commands.getstatusoutput('ls -lt') 4 5 print status 6 print output

 

from:http://www.cnblogs.com/xudong-bupt/p/6218140.html

你可能感兴趣的文章
300+Jquery, CSS, MooTools 和 JS的导航菜单资源(总有一个适合你!)
查看>>
梯度和方向导数的概念
查看>>
【008】Form窗体引用(dll)
查看>>
如何在类中调用session。
查看>>
使用xenu查找web站点死链接使用方法及结果分析 - Binbby - 博客园
查看>>
不再联系
查看>>
Subversion作为windows系统服务自启动
查看>>
png图片的读取
查看>>
T-SQL之表变量与临时表
查看>>
UVALIVE 4004
查看>>
ExtJS4.1:AJAX提交数据的三种方式,80%人都没用过第三种
查看>>
Flash图片处理 图像处理 效果 滤镜 pixelbender blender mode
查看>>
SSIS典型应用场景分析
查看>>
mysql 执行状态分析 show processlist
查看>>
【数据存储】利用IO流操作文件
查看>>
ios实例开发精品文章推荐(8.14)
查看>>
看板,敏捷的另一种实现方式
查看>>
Mfc资源消息的响应机制
查看>>
《JAVA与模式》之策略模式
查看>>
Huffman树
查看>>