C語言中system呼叫shell指令碼的問題

2021-05-21 23:31:31 字數 1023 閱讀 2812

在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 ='

就能保證順序的輸出了

C語言中的system函式

在linux c程式設計時,system函式有時是很好用的。例如在c中需要呼叫openssl時可以這樣呼叫 int cy system const char command,int status otherwise,ret cmd返回資訊 status ret if wifexited status...

C語言中的SYSTEM函式

system函式 是可以呼叫一些dos命令,比如 system cls 清屏,等於在dos上使用cls命令 下面列出常用的dos命令,都可以用system函式呼叫 assoc 顯示或修改副檔名關聯。at 計畫在計算機上執行的命令和程式。attrib 顯示或更改檔案屬性。break 設定或清除擴充套件...

C語言中的system函式引數詳解

函式名 system 功 能 發出乙個dos命令 用 法 int system char command system函式已經被收錄在標準c庫中,可以直接呼叫 system 函式用於向作業系統傳遞控制台命令列,以windows系統為例,通過system 函式執行命令和在dos視窗中執行命令的效果是一...