【发布时间】:2015-07-09 16:35:31
【问题描述】:
我正在尝试运行命令 ls /home/aidan/Pictures/Wallpapers/*/*.{jpg,JPG,png,PNG} 来获取壁纸列表,它在终端中运行良好,但是当我从 C++ 运行它时,它告诉我 ls: cannot access /home/aidan/Pictures/Wallpapers/*/*.{jpg,JPG,png,PNG}: No such file or directory。有谁知道怎么回事?
我用来运行它的命令:
std::string exec(std::string command) {
const char *cmd = command.c_str();
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
【问题讨论】:
-
显然 - 它不会扩展
{expression}并将其视为文件名。 -
也许您应该使用
glob函数而不是shell 命令?见stackoverflow.com/questions/8401777/… -
/bin/sh是否可以进行扩展?也许popen()正在运行一个不为您执行{…}扩展的shell。您可能需要运行bash -c "ls /home/aidan/Pictures/Wallpapers/*/*.{jpg,JPG,png,PNG}"而不仅仅是ls命令。 -
@JonathanLeffler
exec("/bin/sh -c \"ls /home/aidan/Pictures/Wallpapers/*/*.{jpg,JPG,png,PNG}\""给出了同样的错误。