将 shell 命令输出分配给 ROOT 中的变量

Assigning shell command output to a variable in ROOT

我正在使用 gSystem 在我的 ROOT 代码中执行 shell 命令,其中 returns 和 int,如此处所示 gSystem->Exec()。但是当我尝试将输出分配给代码变量时,分配不会发生。

int low_edge = 0;
low_edge = gSystem->Exec("ls ./folder | egrep -o '[0-9]{3,3}' | head -1");

我也试过gSystem->Exec("ls ./folder | egrep -o '[0-9]{3,3}' | head -1") >> low_edge,但没有成功。

我是不是漏掉了什么明显的东西?

gSystem->Exec() 的 return 值为 0 或 -1,具体取决于命令是否成功。

你想要的是:

TString GetFromPipe(const char* command)

TString the_output=gSystem->GetFromPipe("ls ./folder | egrep -o '[0-9]{3,3}' | head -1");

应该可以,您只需将 TString 转换为 int。