簡體   English   中英

如何將命令的 output 捕獲到 Bourne shell 中的文件描述符?

[英]How do I capture the output of a command to a file descriptor in Bourne shell?

在 Bourne shell 中捕獲命令 output 的標准方法是使用$()語法:

output=$(mycommand)

然而,對於有很多 output 的命令,這需要 shell 將 memory 分配為一個長字符串。 我更願意找到與 Unix C function popen道德等價的東西,以獲取我可以從中read的新文件描述符:

newfd=popen(mycommand)
while read -u $newfd LINE; do
  #process output
done

這甚至可能嗎?

#!bash
ls | while read X
do 
    echo  $X is a directory entry
done

用您選擇的命令替換“ls”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM