lmind lmind - 1 year ago 66
Linux Question

Storing df in variable not working

Here's the contents of the script:

DISKREPORT="$(df -hl | grep -vE 'boot|shm' | column -t)"

mail -s "$HOSTNAME Disk Usage Report: $diskreport" myemail@mydomain.com

What's happening is the script hangs when I call it, as if waiting for something else to happen. What am I doing wrong?

EDIT: This is what I ended up with, thanks to your help:

#set -x
DISKREPORT=$(df -hl | grep -vE 'boot|shm' | column -t | cut -f5 --complement)

mail -s "$HOSTNAME Disk Usage Report" jsc-dl-agdl-admin@mail.nasa.gov << EOF

Answer Source

Your 'mail' command is waiting for stdin for the mail body (see here for more details). You can feed a body in using a heredoc e.g.

$ mail -s "My subject" <<EOF

Note also that DISKREPORT is defined in capitals, but you're referencing it later using lowercase, which in bash's world is a different variable!