【问题标题】:Running a script from execl()从 execl() 运行脚本
【发布时间】:2014-03-07 12:57:30
【问题描述】:

我想用execl() 启动一个没有执行权限的脚本。从命令行完成后,效果很好:

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

但是,当我想从 C 中使用 execl 时,它只是启动另一个 shell,而不运行我的脚本。

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

我不能只运行脚本,因为我不能保证脚本是可执行的(它在嵌入式设备上,加载了 FTP 脚本)。

【问题讨论】:

    标签: c exec


    【解决方案1】:

    试试

    execl("/bin/sh", "sh", "/home/bf/playground/test.sh", (char *) NULL);
    /* exec*()-functions do not return on success, so we only get here in case of error. */
    perror("execl() failed");
    

    来自man 3 exec

    这些函数的初始参数是文件名 将被执行。

    execl()execlp() 中的 const char *arg 和后续省略号, 和 execle() 函数可以被认为是 arg0, arg1, ..., argn

    arg0 等价于程序名称arg[0]。程序的第一个st 参数是arg[1]


    另外请注意(exec*()'s man-page 的下方):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多