Nikhilendra
Nikhilendra

Reputation: 51

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

Upvotes: 1

Views: 6368

Answers (3)

Paul Evans
Paul Evans

Reputation: 27567

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

Upvotes: 5

Bharat Ingle
Bharat Ingle

Reputation: 1

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.

Upvotes: -1

perreal
perreal

Reputation: 97918

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.

Upvotes: 1

Related Questions