0

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

2 Answers 2

2

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

cd /home/user;mkdir tmp;ls
Sign up to request clarification or add additional context in comments.

1 Comment

That's not possible in my case
0

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;
}

2 Comments

I'm not sure.. I want to make a "web shell". A website which I can use like a remote shell. So the main() function will be executed by a webserver. So fare I use system() to execute the commands transferred from the client. But eg. cd doesnt work in this way
If you are writing a web server, you might want to consider languages like node.js, python or php. C is not ideal for a website.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.