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

本文共 1094 字,大约阅读时间需要 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 ret26 }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 status6 print output

 

转载于:https://www.cnblogs.com/xudong-bupt/p/6218140.html

你可能感兴趣的文章
单例模式的七种写法
查看>>
extjs_08_界面布局
查看>>
卷积神经网络(CNN)代码实现(MNIST)解析
查看>>
git 在命令行与图形状态下使用详情
查看>>
爱上MVC~Web.Config的Debug和Release版本介绍
查看>>
linux操作系统中oracle数据库的密码过期问题解决
查看>>
Spring中Bean的五个作用域
查看>>
hadoop之 distcp(分布式拷贝)
查看>>
Java后端程序员1年工作经验总结
查看>>
使用Vundle管理配置Vim的插件
查看>>
JDBC连接池&DBUtils使用
查看>>
可以通过shadowserver来查看开放的mdns(用以反射放大攻击)——中国的在 https://mdns.shadowserver.org/workstation/index.html...
查看>>
IOS系统控件高度
查看>>
Flink - ResultPartition
查看>>
2017.10.09 穆瑞课KUKA机器人培训视频的感想
查看>>
Jsoup
查看>>
python中的中文编码问题
查看>>
安卓播放音频
查看>>
in linux system of ftp command
查看>>
Win API:之GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId
查看>>