Passing variable from c program to shell script as argument

后端 未结 4 384
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 01:48

I am calling a shell script from a \'c\' program and have some variables in c which I would like to pass as arguments to the shell script. I tried using the system() to call

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 02:47

    shell script (a.sh):

    # iterates over argument list and prints
    for (( i=1;$i<=$#;i=$i+1 ))
    do
         echo ${!i}  
    done
    

    C code:

    #include 
    
    int main() { 
      char arr[] = {'a', 'b', 'c', 'd', 'e'}; 
      char cmd[1024] = {0}; // change this for more length
      char *base = "bash a.sh "; // note trailine ' ' (space) 
      sprintf(cmd, "%s", base);
      int i;
      for (i=0;i

提交回复
热议问题