从运行EXECL脚本() [英] Running a script from execl()

查看:141
本文介绍了从运行EXECL脚本()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 EXECL()启动一个没有执行权的脚本。当从命令行完成的,这工作得很好:

I want to use execl() to start a script that has no execution rights. When done from the commandline, this works fine:

[bf@bf-laptop-tbwb playground]$ /bin/sh test.sh
I run !

然而,当我想用​​ EXECL 从C,它只是启动另一个外壳,不运行我的脚本。

However, when I want to use execl from C, it just starts another shell, without running my script.

int main(int argc, char **argv) {
  execl("/bin/sh", "/home/bf/playground/test.sh", NULL);    
  return 0;
}

我不能运行脚本,因为我不能保证这个脚本是可执行文件(它是一个嵌入式设备,也被加载与FTP脚本上)。

I cannot just run the script, because I have no guarantee the script is executable (it is on an embedded device, that gets loaded with FTP scripts).

推荐答案

尝试

if (-1 == execl("/bin/sh", "sh", "/home/bf/playground/test.sh", (char *) NULL))
{
  perror("execl() failed");
}

男子3 EXEC

有这些功能的初始参数是一个文件的名称
         被执行。

The initial argument for these functions is the name of a file that is to be executed.

在为const char * ARG和随后的椭圆在 EXECL() execlp()
         和 execle()函数可以被认为是的为arg0,ARG1,...,ARGN

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn.

将arg0 等同于 ARG [0] 这是程序的名字。 1 ST 参数的程序是 ARG [1]

arg0 is equivalent to arg[0] which is the program's name. The 1st argument to a program is arg[1].

另外请注意(进一步下跌 的EXEC *()的手册页):

Also please note (further down of exec*()'s man-page):

的列表
         参数必须由一个空指针被终止,并且,因为这些是
         可变参数函数,这个指针必须强制转换的(的char *)NULL

这篇关于从运行EXECL脚本()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆