bash 从c程序执行终端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4951062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:25:36  来源:igfitidea点击:

execute terminal from c program

cbashterminalsystemexec

提问by gvalero87

To run a c program you do something like this

要运行 ac 程序,您可以执行以下操作

bash> gcc test.c -o test

and then

进而

bash> ./test

How can i do to make test.c execute a terminal in another window??

如何让 test.c 在另一个窗口中执行终端?

回答by initzero

xterm -e "./test"

xterm -e "./test"

This will execute 'test' in a new xterm window. Assuming Linux of course.

这将在新的 xterm 窗口中执行“测试”。当然假设Linux。

回答by Terminal

You can fork a new process and use system() function. This will work on most of Linux distributions. Just check the terminal properties to know the command to execute a new terminal. "gnome-terminal" works for me(Ubuntu, Redhat).

您可以创建一个新进程并使用 system() 函数。这适用于大多数 Linux 发行版。只需检查终端属性即可了解执行新终端的命令。“gnome-terminal”适用于我(Ubuntu,Redhat)。

int main()
{
        if(!fork())// child process
        system("gnome-terminal");
        else
        {
                //do rest of the things here in parent process......
        }

}

After fork(), a new terminal window will open as a separate process.

在 fork() 之后,一个新的终端窗口将作为一个单独的进程打开。

回答by aioobe

Depends on which system you're on and which terminal you have in mind, but here's how to do it if you're on gnome (ubuntu for instance)

取决于您使用的系统以及您想到的终端,但是如果您使用的是 gnome(例如 ubuntu),这里是如何做到的

gnome-terminal -x sh -c "./test"

If you don't want the window to close immediately after ./testfinishes, you do

如果您不希望窗口在./test完成后立即关闭,您可以

gnome-terminal -x sh -c "./test; cat"

回答by Geoffroy

You want to open a window for a new terminal, or what do you want to do? Your question isn't really clear.

您想为新终端打开一个窗口,或者您想做什么?你的问题不是很清楚。

If you want to run some commands, you need to cope with sys calls to launch a new process.

如果要运行某些命令,则需要处理 sys 调用以启动新进程。

On Windows there's the system() function, but I'm not sure it exists on Linux or other posix systems.

在 Windows 上有 system() 函数,但我不确定它是否存在于 Linux 或其他 posix 系统上。

回答by Foo Bah

Based on your use of the word terminalim guessing you are using osx.

根据您对单词的使用,terminal我猜测您正在使用 osx。

You can use applescript to get the behavior:

您可以使用 applescript 来获取行为:

tell application "Terminal"
activate
do script with command "cd _directory_; ./test"
end tell

If you want the program to launch a window, have the program call popen to launch the command [or write to a temporary file and launch the script]

如果您希望程序启动一个窗口,请让程序调用 popen 来启动命令[或写入临时文件并启动脚本]