简体   繁体   中英

How to pass arguments to bash script from a cpp program

I've forked a child process which then calls a bash script using execv , the way i'm passing command line arguments to the script, It does not print first argument on doing echo $1 inside the script.

std::string s = std::to_string(c_no);    
char *args[] = {(char *)s.c_str(), NULL};

pid_t pid = fork();
if(pid == 0){
    execv("./ckpnt.sh", &args[0]);
}

consider c_no to be any integer.

What is the correct way to do this?

I've already refrenced this link How to pass command line arguments from C program to the bash script? but this answer uses system system call and i try to not use that.

First argument passed to a program is its name so currently your number ends up in $0 . args should be:

char *args[] = {"./ckpnt.sh", (char *)s.c_str(), NULL};

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