1

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.

0

1 Answer 1

2

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};
Sign up to request clarification or add additional context in comments.

17 Comments

Still it doesn't print anything.
Works locally. Can you run the program under strace -Ff and see if it is passing arguments correctly?
I just checked echo "$0" is printing ./ckpnt.sh but echo "$1" is not printing anything
Make sure you're properly recompiling, and again, check with strace -Ff to see what is being passed.
the way i've passed arguments in exec line is correct ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.