這裏的一個終端的命令:使用C++中的外殼命令
awk '/^Mem/ {print $4}' <(free -m)
這是我的代碼:
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
我怎樣才能放置'/'符號,這個命令能夠正常工作?
貌似默認的shell - 'sh' - 不支持'<(free -m)'語法。考慮調用'bash'。 –
in terminal這個命令看起來不錯 –
你的終端shell可以是'bash',它支持*這種語法,而'popen'在'sh',更原始的低級shell中運行。試試'echo $ 0'。我剛剛嘗試在'sh'中運行您的命令,但它失敗並顯示相同的消息。 –