lmind lmind - 5 months ago 30
Linux Question

Storing df in variable not working

Here's the contents of the script:

#!/bin/bash
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:

#!/bin/bash
#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
$DISKREPORT
EOF

Answer

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
My
mail
body
here
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!