Bash

讀取管道程序輸出時出現管道中斷錯誤

  • July 4, 2021

我正在做一些事情:

declare -Ft handle_format_output &>/dev/null && exit 1   # test if this function name is already present in this scope
handle_format_output() {
   case $1 in
   domain) ./scripts/idn_to_punycode.pl >>"${2}_${1}.txt" ;;
   ipv4)
       # read doesn't bother w/ pipe input as its undefined: guessing it's leading to pipe breaks
       # therefore open stdin as a separate file and read from it to close the previous pipe
       while IFS= read -r line <&3; do
           case $line in
           */*) printf "%s\n" "$line" >>"${2}_${1}_cidr.txt" ;; # cidr block
           *-*) ipcalc "$line" >>"${2}_${1}_cidr.txt" ;;        # deaggregate ip range
           *) printf "%s\n" "$line" >>"${2}_${1}.txt" ;;        # ip address
           esac
       done 3< /dev/stdin
       ;;
   ipv6) cat -s >>"${2}_${1}.txt" ;;
   esac
}

...  |
 mawk '!seen[$0]++' | # filter duplicates and blank lines
 handle_format_output "$format" "$color"

其中輸入是線性文本,可以是 Web 域、IPv4 地址、IPv4 CIDR 塊或 IPv6 地址。格式為“域”、“ipv4”或“ipv6”。顏色是“白色”或“黑色”。

無論我嘗試什麼,都會不斷拋出此錯誤:

mawk: write failure (Broken pipe)
mawk: close failed on file /dev/stdout (Broken pipe)
Error: Process completed with exit code 2.

我究竟做錯了什麼?

我想到了!一些格式數據欄位是“ivp4”而不是“ipv4”。我現在想知道為什麼會有這麼長時間的成功建構,但是在修復它們之後,正常管道現在可以工作了。

引用自:https://unix.stackexchange.com/questions/656863