Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c++;_C++_Linux_Bash_Shell_Escaping - Fatal编程技术网

C++ 在c++;

C++ 在c++;,c++,linux,bash,shell,escaping,C++,Linux,Bash,Shell,Escaping,这是一个终端命令: awk '/^Mem/ {print $4}' <(free -m) 如何放置‘/’符号,以便这个命令正确工作?< /p> < p>您的问题不是C++。您正在使用popen调用您的命令,popen在shshell中运行您的命令,该shell不支持,当popen在sh中运行时,它是更原始、低级的shell。尝试echo$0。我刚刚尝试在sh中运行您的命令,但失败了,并显示相同的消息。内存:3398 2848 550 380 209 959{print$4}对我不起作用(

这是一个终端命令:

awk '/^Mem/ {print $4}' <(free -m)

如何放置‘/’符号,以便这个命令正确工作?< /p> < p>您的问题不是C++。您正在使用

popen
调用您的命令,
popen
sh
shell中运行您的命令,该shell不支持,当
popen
sh
中运行时,它是更原始、低级的shell。尝试
echo$0
。我刚刚尝试在
sh
中运行您的命令,但失败了,并显示相同的消息。内存:3398 2848 550 380 209 959
{print$4}
对我不起作用(我只是将
Mem
更改为
ПаМ
,因为我正在运行俄语本地化)。我增加了一个新的选项来回答这个问题,我也使用它。但最好的是,我看到的是
Паааааааааа:3398 2785 613 339 212 918
好的,我知道了,我原来的答案是错误的,使用管道选项
class CSystemInfo{
public:
    std::string free_ram(){
        std::string s;
        FILE *in;
        char buff[512];
        //here is a prepared string (but with an error)
        if(!(in = popen("awk '/^Mem/ {print $4 \"\\t\"}' <(free -m)","r"))){
        return "";  
        }
        while(fgets(buff, sizeof(buff), in)!=NULL){
            cout << s;
            s=s+buff;
        }
        pclose(in);
        return s;
       }
};
    CSystemInfo info;
    std::string ram = info.free_ram();
    cout << ram;
sh: 1: Syntax error: "(" unexpected
popen("free -m | awk ...")
bash -c "awk '/^Mem/ {print $4}' <(free -m)"
popen("bash -c \"awk '/^Mem/ {print $4}' <(free -m)\"")