C语言中system调用shell脚本的问题

在C语言中使用system()语句调用shell脚本时,C语言程序并不会等待system()语句执行完毕尤其在shell脚本有调用bash下命令时并向文件输出时,又无法用刷新标准输出流缓冲区的方式来解决乱序输出的问题.例如

C中部分

for (int i=0;i<5;i++)

{

     system("./test.sh");

}

shell中部分

cat /dev/null > test.txt

echo -e "output infomation" >> test.txt

nslookup 8.8.8.8 | grep "name =" >> test.txt #nslookup为解析与逆向解析ip地址的命令会因为网络通信而不能即时得到结果

 

在有些低版本的linux中

当这部分执行的时候我们在test.txt文件里面看到的结果有可能是

output infomation

output infomation

output infomation

output infomation

output infomation

8.8.8.8.in-addr.arpa name = google-public-dns-a.google.com

8.8.8.8.in-addr.arpa name = google-public-dns-a.google.com

8.8.8.8.in-addr.arpa name = google-public-dns-a.google.com

8.8.8.8.in-addr.arpa name = google-public-dns-a.google.com

8.8.8.8.in-addr.arpa name = google-public-dns-a.google.com

 

这种情况出现的时候

使用

(echo -e "output infomation" ; nslookup 8.8.8.8) | grep -E 'output information|name ='

就能保证顺序的输出了

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值