如何将 C 语言中获取的参数值,传递给shell脚本中使用?

hc_net 2014-01-17 03:23:15
请教:

例:在 C 代码中获取到了某个值,如何作为参数值倒到shell 脚本中调用执行
如:
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
..........
..........
sprintf(ipaddr,"%s",ip);

system("/root/run.sh");

run.sh 内容:
#/bin/bash
ipaddress=ipaddr ? //问题是如何把C获取的这个值传到这里??
............
.......
如何能将 ipaddr 这个值传递到 run.sh 脚本中当值使用?

谢谢
...全文
285 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yulinlang 2014-01-18
  • 打赏
  • 举报
回复

#include <stdlib.h>

int main()
{
  setenv("ipaddr", "192.168.0.1", 1);
  system("./run.sh");
  return 0;
}

echo $ipaddr
hc_net 2014-01-18
  • 打赏
  • 举报
回复
多谢楼上的几位朋友,但是我在 shell 这个脚本里想得到这个值比取到IP是 192.168.0.1 如: #/bin/bash iptables -t nat 192.168.0.1 ....... iptables -t nat 192.168.0.1 ....... iptables -t nat 192.168.0.1 ....... 192.168.0.1这里是变量如何传递到这里使用呢


sprintf(cmd,"/root/run.sh %s",ip);
  
system(cmd);

sprintf(cmd,"/root/run.sh %s",ip); 这个是否在run.sh 这个shell脚本把 ip 这个以 $1 来代替
hc_net 2014-01-18
  • 打赏
  • 举报
回复
多谢了几种方法都可以用了
图灵狗 2014-01-17
  • 打赏
  • 举报
回复
++
引用 1 楼 mujiok2003 的回复:
sprintf(cmd,"/root/run.sh %s",ip);
 
system(cmd);
derekrose 2014-01-17
  • 打赏
  • 举报
回复
必须把值传到字符串里
bfdeh 2014-01-17
  • 打赏
  • 举报
回复
char buf[1024] = "/root/run.sh ";
strcat(buf, ipaddr);
system(buf);
#/bin/bash ipaddress=%1
bfdeh 2014-01-17
  • 打赏
  • 举报
回复
char buf[1024] = "/root/run.sh"; strcat(buf, ipaddr); system(buf); #/bin/bash ipaddress=%1
mujiok2003 2014-01-17
  • 打赏
  • 举报
回复
sprintf(cmd,"/root/run.sh %s",ip);
 
system(cmd);

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧