Search code examples
bashscriptingnamed-pipes

Named pipe swallowing first field of Linux command output


I'm trying to parse the output of the Linux df tool for use in a machine status report. I'm using almost identical code to parse the output of the ps tool and it works fine (all fields are available in read loop) but in the code below the first field output from awk (percentUsed) is missing when I read from the named pipe.

#!/bin/sh

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

Sample df + awk output

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

Solution

  • I edited my code to use the standard pipe suggestion by @Barmar and it resolves my problem. The code below works fine.

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