我有点困惑,不确定如何将 cat << EOF 样式添加到我的脚本中。非常感谢一些反馈和帮助。
这是我的脚本
!/bin/sh
#This is a disk mailer script for hdfs
DU_RESULTS_HOME=$(sudo -u hdfs hadoop fs -du -s /user/* | sort -r -k 1 -g | awk '{ suffix="KMGT"; for(i=0; $1>1024 && i < length(suffix); i++) $1/=1024; print int($1) substr(suffix, i, 1), $3; }'| head -n 22 |awk '{ print $1"\t\t\t" $2 }')
DF_RESULTS_HOME=$(sudo -u hdfs hadoop fs -df -h /user)
HOSTNAME=$(hostname -s)
MESSAGE_SUBJECT="Disk usage"
MESSAGE_SENDER="xyz"
MESSAGE_EMAIL="abc"
DU_RESULTS_HEADER_USER="Dir size User"
MESSAGE_BODY="Team:
High level Disk Usage Report for: /user
Please take a moment to clean out any old data, and write temp files to an appropriate filesystem, such as /tmp.
Thanks
if [[ --debug == "$1" ]]; then
echo debug on
printf '%s\n\n\n\n%s\n\n%s\n%s\n\n\n\n\n%s\n%s\n%s' "$MESSAGE_BODY" "$DF_RESULTS_HOME" "$DU_RESULTS_HEADER_USER" "$DU_RESULTS_HOME"
else
echo debug off
printf '%s\n\n\n\n%s\n\n%s\n%s\n\n\n\n\n%s\n%s\n%s' "$MESSAGE_BODY" "$DF_RESULTS_HOME" "$DU_RESULTS_HEADER_USER" "$DU_RESULTS_HOME" | /bin/mail -s "$MESSAGE_SUBJECT" -r "$MESSAGE_SENDER" "$MESSAGE_EMAIL"
fi
答案1
这<<
是定理。后面的任何内容都充当<<
定界符的分隔符
cat <<EOF
This is first line
So, this is 2nd
Again, another line
EOF
Enter
因此,如果您想将三行存储到变量中,请按如下所示操作:
从命令行
$ var="$(cat <<EOF
> Bingo
> Gotcha
> Enough
> EOF
> )"
Enter
$ echo "$var" #double quotes are important
Bingo
Gotcha
Enough
从脚本内部
#!/bin/bash
var="$(cat <<EOF
Bingo
Gotcha
Enough
EOF
)"
echo "$var"