将变量从c程序传递给shell脚本作为参数 [英] Passing variable from c program to shell script as argument
本文介绍了将变量从c程序传递给shell脚本作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从c"程序调用 shell 脚本,并且在 c 中有一些变量,我想将它们作为参数传递给 shell 脚本.我尝试使用 system() 来调用 shell 脚本,但我作为参数传递的变量被视为字符串而不是变量.
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 the shell script but the variable I pass as argument is considered as a string rather than a variable.
推荐答案
shell script (a.sh):
shell script (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程序传递给shell脚本作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文