PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 14, 2022

[FIXED] How to pass arguments to bash script from a cpp program

 September 14, 2022     bash, c++, command-line-arguments, exec, fork     No comments   

Issue

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.


Solution

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};


Answered By - Botje
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing