c - 将变量从c程序作为参数传递给shell脚本

标签 c shell variables arguments

我正在从“c”程序调用 shell 脚本,并且在 c 中有一些变量,我想将它们作为参数传递给 shell 脚本。我尝试使用 system() 调用 shell 脚本,但我作为参数传递的变量被视为字符串而不是变量。

最佳答案

shell 脚本(a.sh):

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

C 代码:

#include <stdio.h>

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<sizeof(arr)/sizeof(arr[0]);i++) {
    sprintf(cmd, "%s%c ", cmd, arr[i]); 
  }
  system(cmd);
}

关于c - 将变量从c程序作为参数传递给shell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18179346/

相关文章:

linux - 在 linux 中删除带有反斜杠的文件

c++ - 生成变量的名称

c - 该代码应该将每个单词的每个字母转换为大写。但运行代码会导致总线错误。总线错误的原因是什么?

c - 为什么对于 sqlite3_open 我们使用双指针 ** 而对于 sqlite3_prepare 我们使用指针 *

c - 使用 A Vedaldi 的 sift 实现

linux - 删除 `~` 中的 `~/my_repo/~`

python - ImportError : Entry point ('console_scripts' , 'easy_install' ) 未找到

将变量转换为函数

javascript - 构造函数变量的打印名称

c - OpenSSL “error: storage size of ‘ctx’ 未知”