用execl在xterm中执行bash命令的C程序

时间:2013-04-02 12:07:21

标签: c linux exec system-calls

我有一个在Linux上的普通终端中执行良好的命令:

xterm -e bash -c "some commands"

我想使用c程序execXX系统调用执行上面的命令。我尝试使用以下代码,但它给了我一个正常的xterm窗口。

execl("/usr/bin/xterm", "/usr/bin/xterm -e bash -c \"some commands\"", NULL);

有没有办法可以使用execXX系统调用执行上述命令?谢谢!

2 个答案:

答案 0 :(得分:4)

您需要将其称为:

execl("/usr/bin/xterm", "/usr/bin/xterm", "-e", "bash", "-c", "some commands", (void*)NULL);

约定是让第一个参数与程序的路径相同。如果参数中有空格,则与调用xterm 'something with spaces'而不是xterm something with spaces的效果相同。

答案 1 :(得分:0)

可能的切线:是否有任何理由需要在xterm内专门运行这些?如果您只想运行一些shell命令,那么在/bin/sh/bin/bash中运行它们会更自然,也可能更可靠。