尝试从 c++ linux 执行命令行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14532496/
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-08-06 18:48:39  来源:igfitidea点击:

Try to execute command line codes from c++ linux

c++linuxcommand-prompt

提问by Lakshmi Narayanan

I tried the following code, to communicate with the command line from c++ code.

我尝试了以下代码,从 C++ 代码与命令行进行通信。

#include<iostream>
#include<cv.h>

int main()
{
    system("gnome-terminal");
    system("cd");
}

The gnome-terminal command is executing fine. After I close the terminal, when am expecting the cd to execute, however, is not happening. Could you please help me and point out the reason? Thanks. I was expecting the function to make the cmd go down to the home directory , but it did not. am working in linux

gnome-terminal 命令执行正常。在我关闭终端后,当我期望 cd 执行时,然而,并没有发生。你能帮我指出原因吗?谢谢。我期待该功能使 cmd 进入主目录,但事实并非如此。我在 linux 工作

I tried it even by removing gnome. simple cd is not working. am I doing something rong>?

我什至通过删除侏儒来尝试它。简单的 cd 不起作用。我在做什么? rong>?

If I try ls, it seems to be working fine!

如果我尝试 ls,它似乎工作正常!

My main aim is to open a new terminal, and execute commands on that new terminal through the present program that opened the new terminal. Could you please tell me how I can achieve this??

我的主要目标是打开一个新终端,并通过打开新终端的当前程序在该新终端上执行命令。你能告诉我我怎么能做到这一点吗??

采纳答案by Vahid Farahmand

If you want to run a program and wait for it to finish before executing next line, take a look at this page and example code here: http://www.thegeekstuff.com/2012/03/c-process-control-functions/

如果你想运行一个程序并在执行下一行之前等待它完成,看看这个页面和这里的示例代码:http: //www.thegeekstuff.com/2012/03/c-process-control-functions /

But if you want to run gnome-terminal and execute a command in newly created window, do this:

但是,如果您想运行 gnome-terminal 并在新创建的窗口中执行命令,请执行以下操作:

system("gnome-terminal -x sh -c 'cd /tmp ; ls -la'");

回答by aschepler

The systemfunction creates a shell child process to execute the specified command.

system函数创建一个shell子进程来执行指定的命令。

cdis a shell command which changes the current working directory of that shell process only.

cd是一个 shell 命令,它仅更改该 shell 进程的当前工作目录。

So the child's cdprobably works fine, but it has no effect on your C++ program, which is a different process.

所以孩子的cd可能工作正常,但它对您的 C++ 程序没有影响,这是一个不同的过程。

Instead, you probably want to look at the Linux system call chdir.

相反,您可能想查看 Linux 系统调用chdir

回答by Lakshmi Narayanan

Thanks for your help!! This command worked perfectly fine from this link

谢谢你的帮助!!此命令从此链接运行得非常好

https://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu

https://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu

    gnome-terminal -x sh -c 'command1; command2; exec bash'

and I entered the respective commands in the new window. But to change the working directory in the shell am working o, I haven't still figured that out.

我在新窗口中输入了相应的命令。但是要更改 shell 中的工作目录,我还没有弄清楚。