简体   繁体   中英

Calling shell script from C++

A shell script test.sh is called from C++ code by the command execl("/system/bin/sh","sh","test.sh")

After the execution of shell script i need to get back the control to C++, but the shell script is just exiting not executing the next instructions in C++ code

You want to use fork to create a child process before you exec :

pid_t pid;
if((pid = fork()) > 0)
    execl("/system/bin/sh","sh","test.sh");
int status;
waitpid(pid, &status, 0); // wait for child process, test.sh, to finish

With the exec family of functions, your process becomes the newly executed process, meaning everything about the original process is lost.

What you need to use is the system function which creates a separate process and waits for it to complete and continues the execution.

Use

System("sh test.sh") 

It works for me, as earlier I was using only system("test.sh") and it created an issue, sometimes it executes properly and sometimes it returns no results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM