If there's a "First World Problems" for scripting, this would be it.
I have the following code in a script I'm updating:
if [ $diffLines -eq 1 ]; then
dateLastChanged=$(stat --format '%y' /.bbdata | awk '{print $1" "$2}' | sed 's/\.[0-9]*//g')
mailx -r "Systems and Operations <sysadmin@[redacted].edu>" -s "Warning Stale BB Data" jadavis6@[redacted].edu <<EOI
Last Change: $dateLastChanged
This is an automated warning of stale data for the UNC-G Blackboard Snapshot process.
EOI
else
echo "$diffLines have changed"
fi
The script sends email without issues, but the mailx command is nested within an if statement so I appear to be left with two choices:
- Put
EOI
on a new line and break indentation patterns or - Keep with indentation but use something like an echo statement to get mailx to suck up my email.
I'm open to alternatives to heredoc, but if there's a way to get around this it's my preferred syntax.
Best Answer
You can change the here-doc operator to
<<-
. You can then indent both the here-doc and the delimiter with tabs:Note that you must use tabs, not spaces to indent the here-doc. This means the above example won't work copied (Stack Exchange replaces tabs with spaces). There can not be any quotes around the first
EOF
delimiter, else parameter expansion, command substitution, and arithmetic expansion are not in effect.