cmdline.h

#include <iostream> #include <mutex>

class Cmdline {

private:

Cmdline() = default;

public:

~Cmdline() = default;

Cmdline(const Cmdline& cmd) = delete;

Cmdline& operator=(const Cmdline& cmd) = delete;

public:

static Cmdline& getInstance();

void executeCMD(const std::string& cmd, std::string& result);

private:

static std::mutex m_mutex;

};

cmdline.cpp

#include "cmdline.h"

#include "stdio.h"

std::mutex Cmdline::m_mutex;

Cmdline& Cmdline::getInstance()

{

std::lock_guard<std::mutex> lck(m_mutex);

static Cmdline instance;

return instance;

}

void Cmdline::executeCMD(const std::string& cmd, std::string& result)

{

char buf_ps[1024] = {0};

std::string temp_cmd = cmd + " 2>&1"; //标准错误重定向到标准输出

FILE* ptr = nullptr;

if((ptr = popen(temp_cmd.c_str(), "r")) != nullptr)

{

while(fgets(buf_ps, 1024, ptr) != nullptr)

{

result += buf_ps;

}

pclose(ptr);

ptr = nullptr;

}

else

{

std::cout << "popen %s error!!" << std::endl;

}

}

test.cpp

#include "cmdline.h"

int main(int argc, char* argv[])

{

if(argc != 2)

{

std::cout << "error number args" << std::endl;

return -1;

}

std::string strResult = "";

std::string cmd = argv[1];

Cmdline::getInstance().executeCMD(cmd, strResult);

std::cout << "result: " << strResult << std::endl;

return 0;

}

获取标准shell 命令的输出内容的更多相关文章

  1. 异步获取CMD命令行输出内容

    当控制台命令使用process.Start(); 后可以直接显示输出内容,当然它是异步显示的不用等程序结束.代码如下: using System;using System.Collections.Ge ...

  2. Shell命令-文件及内容处理之grep(egrep)、join

    文件及内容处理 - grep(egrep).join 1. grep(egrep):文本过滤工具 grep(egrep)命令的功能说明 grep命令是Linux系统中最重要的命令之一,其功能是从文本文 ...

  3. Shell命令-文件及内容处理之sort、uniq

    文件及内容处理 - sort.unip 1. sort:对文件的文本内容排序 sort命令的功能说明 sort 命令用于将文本文件内容加以排序.sort 可针对文本文件的内容,以行为单位来排序. so ...

  4. Shell命令-文件及内容处理之head、tail

    文件及内容处理 - head.tail 1. head:显示文件内容头部 head命令的功能说明 head 命令用于显示文件头部内容,默认执行 head 命令会输出文件开头的 10 行. head命令 ...

  5. Linux下Shell命令的输出信息同时显示在屏幕和保存到日志文件中

    #直接覆盖日志文件 ls -l | tee ./t.log #将输出内容附加到日志文件 ls -l | tee -a ./t.log 使用的是tee命令

  6. linux重定向总结:如何将shell命令的输出信息自动输出到文件中保存

    在做批量实验室,例如跑批量MR的作业,我们会写好shell脚本,然后启动脚本,等所有作业执行完再去看结果,但是这些执行时的信息如何保存下来到文件中呢?下面这个命令可以完成这个任务. sh batchj ...

  7. Shell命令-文件及内容处理之more、less

    文件及内容处理 - more.less 1. more:分页显示文件内容 more命令的功能说明 more 命令类似 cat,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白 ...

  8. Shell命令-文件及内容处理之wc,tr

    文件及内容处理 - wc.tr 1. wc:统计文件的行数.单词数或字节数 wc命令的功能说明 wc 命令用于计算字数.利用 wc 指令我们可以计算文件的字节数,字数,或是列数,若不指定文件名称,或是 ...

  9. Shell命令-文件及内容处理之cat、tac

    文件及内容处理 - cat.tac 1. cat:显示文件内容 cat命令的功能说明 cat命令用于连接多个文件并且打印到屏幕输出或重定向到指定文件中 cat命令的语法格式 cat [OPTION]. ...

随机推荐

  1. JSP循环缓存列表

    两种方法: 例如,有下拉框 <select></select>其中选项需要循环显示 1.JAVA代码循环 <% for(int i=0;i<list.size(); ...

  2. webbrowser设置为相应的IE版本

    注册表路径: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATU ...

  3. [经验交流] k8s mount 文件到容器目录

    docker 的 volume 可以 mount 单个文件(比如单个配置文件)到容器目录.同时保留原目录的内容.放到 k8s 中,结果却变成了这样:k8s 的 volume 把文件mount 到容器目 ...

  4. 机器学习基石10-Logistic Regression

    注: 文章中所有的图片均来自台湾大学林轩田<机器学习基石>课程. 笔记原作者:红色石头 微信公众号:AI有道 上一节课介绍了Linear Regression线性回归,用均方误差来寻找最佳 ...

  5. Access denied for user ‘root’@‘localhost’(using password: YES)的解决方法

    https://www.jb51.net/article/133836.htm 搭建服务器之后只能看见test与infomation_schema两个库 https://www.cnblogs.com ...

  6. qml layout

    一.使用总结 1.锚点:锚点锚在父控件左边anchors.left: parent.left ,才可以设置anchors.leftMargin:20 才有作用,设置anchors.topMargin: ...

  7. jmeter知识总结(一)

    Apache JMeter是一款纯java编写负载功能测试和性能测试开源工具软件.相比Loadrunner而言,JMeter小巧轻便且免费,逐渐成为了主流的性能测试工具,是每个测试人员都必须要掌握的工 ...

  8. windows环境安装phantomjs和pyspider遇到的问题

    1. 安装phantomjs 下载地址:http://phantomjs.org/download.html 解压后将phantomjs.exe文件放到python根目录 2.安装pyspider p ...

  9. eclipse,代码中有错误,项目或者java类中却不显示红叉

    修改eclipse代码提示级别1.单个项目修改项目上右键-->properties-->java compiler-->building-->enable project sp ...

  10. python 对Excel表格的写入

    python对Excel表格写入需要导入xlrd ,和xlutils两个库 from xlrd import open_workbook from xlutils.copy import copy o ...