从终端传递的参数中使用 execve 执行算术运算

Performing arithmetics with execve from arguments passed by terminal

试图了解如何根据从终端传递的参数执行算术运算并在 execve 中使用它们?

./myProgram 'echo "+=$((+))"' 3 5

这应该是输出 8

what i've tried manually without argv :

pid_t pid;
char *parmList[] = {"sh", "-c", "echo [=11=]  +  1 2", NULL};
char *envParms[3] = {"1=1", "2=2", NULL};

if ((pid = fork()) == -1)
    perror("fork error");
else if (pid == 0)
{
    execve("/bin/sh", parmList, envParms);
    printf("Return not expected. Must be an execve error.n");
}

我得到 $0 是 sh ✅

但无法识别 $1 和 $2 ...我缺少什么?

您不希望在环境中传递 1 和 2。 shell 希望它们作为位置参数。所以你可以这样做:

char *parmList[] = {"sh", "-c", "echo [=10=]  +  1 2", "sh", "1", "2", NULL};