I recently started to use linux, so I have little knowledge about it. At least I know that every thing in linux is a file.
I would like to know how to catch a specific linux system return, for example if I choose install ruby (sudo apt-get -y install ruby), how can I know it was installed successfully?
char buffer[1024];
char *buf = malloc(4096);
char *pl;
FILE *fp;
if (strcmp(cmd, "ruby") == 0)
{
fp = popen("sudo apt-get -y install ruby", "r");
}
if (fp == NULL)
{
printf("Failed to load file\n");
exit(0);
}
while ((pl = fgets(buffer, sizeof(buffer), fp)) != NULL)
{
strcat(buf, buffer);
}
strcat(buf, "\n");
pclose(fp);
Then I am using popen to read the file opened, but it contains the same that is shown in terminal and I just want a ‘flag’ like OK or FAIL.
Sorry for my poor english.
The exit code of apt-get will tell you if it succeeded or not (0 means success). pclose(fp) will return the exit code, so you can do:
You may notice, though, that now we’re not actually reading from the pipe. There’s not really any reason to have it. So like Joachim suggested, the system() function is probably a better fit for your case.