gpt4 book ai didi

c++ - 如何通过cpp程序将参数传递给bash脚本

转载 作者:行者123 更新时间:2023-12-03 07:15:28 33 4
gpt4 key购买 nike

我 fork 了一个子进程,然后使用execv调用bash脚本,这是我将命令行参数传递给脚本的方式,在脚本内部执行echo $1时,它不会打印第一个参数。

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]);
}
c_no视为任何整数。
正确的方法是什么?
我已经引用了该链接 How to pass command line arguments from C program to the bash script?,但是此答案使用了 system系统调用,但我尝试不使用它。

最佳答案

传递给程序的第一个参数是其名称,因此当前您的电话号码以$0结尾。 args应为:

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

关于c++ - 如何通过cpp程序将参数传递给bash脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64599451/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com