命名管道吞没 Linux 命令输出的第一个字段

Named pipe swallowing first field of Linux command output

我正在尝试解析 Linux df 工具的输出以用于机器状态报告。我使用几乎相同的代码来解析 ps 工具的输出并且它工作正常(所有字段都可以在读取循环中使用)但是在下面的代码中,当我从 awk (percentUsed) 输出的第一个字段丢失时从命名管道读取。

#!/bin/sh

mkfifo dfPipe0
IFS=" "
df -h | awk '{ print " "" "" " }' > dfPipe0 &
while read -r percentUsed size mountedOn fileSystem
do
  printf "%s\n" "${percentUsed} | ${size} | ${mountedOn} | ${fileSystem}"
done < dfPipe0
rm dfPipe0

示例 df + awk 输出

$ df -h | awk '{ print " "" "" " }'
Use% Size Mounted Filesystem
0% 1.9G /dev devtmpfs
- 0 /sys/kernel/security securityfs
4% 1.9G /dev/shm tmpfs
$

我编辑了我的代码以使用@Barmar 的标准管道建议,它解决了我的问题。下面的代码工作正常。

df -h | \
while read -r fileSystem size used avail percentUsed mountedOn
do  printf "%s\n" "$fileSystem | $size | $used | $avail | $percentUsed | $mountedOn"
done