c++执行shell操作

 

message("----->>>>>shelldemo/CMakeLists.txt" )

# 收集当前目录下所有的 .cpp 文件
aux_source_directory(. DIR_SRCS)

# 添加可执行文件
add_executable(shelldemo ${DIR_SRCS})

# 链接需要的库
target_link_libraries(shelldemo ${LIB_LIB})
/shelldemo/CMakeLists.txt

 

 1 #ifndef COMCFUNC_H
 2 #define COMCFUNC_H
 3 #include <iomanip>
 4 #include <iostream>
 5 #include <cstdint>  // uint64_t
 6 
 7 class ComModule {
 8 
 9 
10 public:
11     /**
12      * @brief Shell 运行脚本 内存不足时调用vpopen以保证脚本执行成功
13      * @param command 脚本命令
14      * @param opt 0 : vpopen 1:popen
15      * @return true:执行成功 false:执行失败
16      */
17     static bool Shell(const char* command, int opt = 0);
18 
19     static void Shell(const char* command, std::string &sOut);
20 
21     static void vforkShell(const char* command, std::string &sOut);
22 
23     static std::uint64_t GetTickCount();
24 };
25 
26 #endif
commodule.h
 1 #include "commodule.h"
 2 #include <sys/time.h>
 3 #include <time.h>
 4 #include <sys/mman.h>
 5 #include <fcntl.h>
 6 #include <unistd.h>
 7 #include  <cstring>  // strerror
 8 bool ComModule::Shell(const char *command, int opt)
 9 {
10     printf("start %s ",command);
11     int startTime = ComModule::GetTickCount();
12 
13     FILE *fpRead;
14     fpRead = popen(command, "r");
15     if(fpRead == nullptr) {
16         printf("err %s %d %s ", command, errno, strerror(errno));
17         return false;
18     }
19 
20     if(opt)
21     {
22         pclose(fpRead);
23     }
24 
25 
26     int endTime = ComModule::GetTickCount();
27     if(endTime - startTime > 2000)
28     {
29         printf("shell time consuming %dms, cmd: %s ", endTime - startTime, command);
30     }
31     printf("end %s ",command);
32     return true;
33 }
34 
35 void ComModule::Shell(const char* command, std::string &sOut)
36 {
37     char* result = "";
38     FILE *fpRead;
39     fpRead = popen(command, "r");
40     if(fpRead == nullptr) {
41         printf("err %s %d %s ", command, errno, strerror(errno));
42         return;
43     }
44     char buf[1024] = {0};
45     memset(buf,'\0',sizeof(buf));
46     sOut.clear();
47     while(fgets(buf,1024-1,fpRead)!=nullptr)
48     {
49         sOut.append(buf);
50     }
51 
52     pclose(fpRead);
53 }
54 
55 
56 void ComModule::vforkShell(const char* command, std::string &sOut)
57 {
58     char* result = "";
59     FILE *fpRead;
60     fpRead = popen(command, "r");
61     if(fpRead == nullptr) {
62         printf("err %s %d %s ", command, errno, strerror(errno));
63         return;
64     }
65     char buf[1024]={0};
66     memset(buf,'\0',sizeof(buf));
67     sOut.clear();
68     while(fgets(buf,1024-1,fpRead)!=nullptr)
69     {
70         sOut.append(buf);
71     }
72 
73     pclose(fpRead);
74 }
75 
76 
77 std::uint64_t ComModule::GetTickCount()
78 {
79     struct timespec ts;
80 
81     clock_gettime(CLOCK_MONOTONIC, &ts);
82 
83     return ((std::uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
84 }
commodule.cpp

 

cmake_minimum_required(VERSION 3.2)
project(cppdemos)

#set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -rdynamic -O0 -ggdb -std=c++11 -Wall -Wno-deprecated -Werror -Wno-unused-function -Wno-builtin-macro-redefined -Wno-deprecated-declarations ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -Wreturn-type -fdata-sections -ffunction-sections -fstack-protector-strong -fPIC")
## -Wno-unused-variable


include_directories(.)
include_directories(/usr/local/include)
link_directories(/usr/local/lib64)

#输出目录重定向
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

set(LIB_LIB
    pthread
    )

## 初步认识线程
add_subdirectory(shelldemo)
/CMakeLists.txt
#include <iostream>
#include "commodule.h"


void test1(){
    {
        ComModule::Shell("rm -rf  abc.log");
    }

    {
        std::string strCmd = "ls -lah ";
        std::string strOut;
        ComModule::Shell(strCmd.c_str(), strOut);   
        std::cout <<"Shell:" << strOut << std::endl;
    }

    {
        std::string strCmd = "ls -lah ";
        std::string strOut;
        ComModule::vforkShell(strCmd.c_str(), strOut);   
        std::cout <<"vforkShell:" << strOut << std::endl;
    }
}



int main(){
    test1();

    return 0;
}
shelldemo.cpp

 

posted @ 2024-11-08 15:58  He_LiangLiang  阅读(4)  评论(0编辑  收藏  举报