【发布时间】:2016-01-14 19:19:47
【问题描述】:
故事 - 我在根植于 Android Wear 设备上获得了一个脚本文件,我想用我的 c++ 代码运行它。
首先我尝试了这个int ret = system(/system/bin/sh /full/path/a.sh),结果每次system()返回代码127 - 命令不存在错误。
我在这里找到了这个解决方法:system is returning error 127 when called from c++ in linux,我按照@Nikhilendra 所说的做:
int ret = execl("/system/bin/sh","/system/bin/sh","/full/path/a.sh",(char*)NULL)
现在我的 c++ 代码每次都在这一行崩溃,即使没有返回值,所以我无法得到任何错误代码。
非常感谢任何帮助。
EDIT1: 脚本 a.sh 本身运行正常。
EDIT2: 我的问题可以理解为system is returning error 127 when called from c++ in linux的跟进
【问题讨论】:
-
Nikhilendra 用三个参数做到了。
-
@greenapps 其实这是正确的方法,Nikhilendra 没有给出完整的参数列表。
-
execl调用将当前过程映像替换为新的过程映像。这是故意的吗? -
@MartinZabel 第一个参数是可执行文件的完整路径,其余的是被分割成片段的实际命令。见linux.die.net/man/3/execl