简体   繁体   中英

C , Execute multiple commands in one shell-instance

Is there a way to execute shell-commands in the same shell-instance? Since, system() leaves the started shell after the command is executed.

You could always contruct your shell command as a single line command using semicolons. Such as:

cd /home/user;mkdir tmp;ls

Do you mean execute the command in the same terminal that run your program? You can achieve that with popen :

#include <stdio.h>

int main() {
    FILE *f = popen("ls", "r");
    char line[1024];
    size_t len;
    while (fgets(line, 1024, f) != NULL) {
        printf("%s", line);
    }
    pclose(f);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM