-2

I want to create a C or C++ program that automates some things for me.

The idea is as such: I run the program ./a.out and sit back and watch as it opens up three or four new terminal windows and runs various UNIX commands. Is there a way to do this?

I am on a MacBook.

  • 7
    Why not a shell script? – tkausl Jul 07 '22 at 22:58
  • Do you actually need "new terminal windows" or are you actually just wanting to run various commands via the program? – kaylum Jul 07 '22 at 23:02
  • @kaylum there is a specific reason as to why i wanted to have it open new terminal windows -- also, it looks a lot cooler. – Infection Tag Jul 07 '22 at 23:23
  • Please see [Why is "Is it possible to..." a poorly worded question?](https://softwareengineering.meta.stackexchange.com/questions/7273/why-is-is-it-possible-to-a-poorly-worded-question/7274) In general, if you have seen one program that does what you want (such as an installer), then it is possible to do it with a C program and to do it with a C++ program. (They are "general purpose" languages after all.) Not an interesting question. – JaMiT Jul 08 '22 at 01:26
  • 1
    If you have good reasons to use C for that (rather than a shell script) you can have a look at `posix_spawn(...)` to create a new terminal process and run your command. If offers better control than `system(...)` over the created process. – wohlstad Jul 08 '22 at 03:46

1 Answers1

0

As the comments point out, this is probably best accomplished by a shell script. However, you can execute shell commands from C with the system(3) standard library function. To open a terminal, just run a terminal with a particular command. For example:

system("xterm -e 'echo hello; sleep 5'");
user3188445
  • 4,062
  • 16
  • 26