我使用以下命令发送电子邮件,仅当提供所有参数时它才有效。
请告知如何处理空/空白变量。例如,如果未提供 -c -b 则忽略。
mailx -a ${ATTACH} -s "$SUBJECT" -c "$CC_MAIL" -b "${BCCMAIL}" -r "${FROM}" "${TO_LIST}"
问候,维拉
答案1
引用https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
${parameter:+word}
如果范围为空或未设置,没有任何内容被替换,否则扩展单词被替换。
mailx ${ATTACH:+-a} "${ATTACH}" ${SUBJECT:+-s} "$SUBJECT" ${CC_MAIL:+-c} "$CC_MAIL" ${BCCMAIL:+-b} "${BCCMAIL}" ${FROM:+-r} "${FROM}" "${TO_LIST}"
例子:
echo mailx ${ATTACH:+-a} "${ATTACH}" ${SUBJECT:+-s} "$SUBJECT" ${CC_MAIL:+-c} "$CC_MAIL" ${BCCMAIL:+-b} "${BCCMAIL}" ${FROM:+-r} "${FROM}" "${TO_LIST}"
ATTACH=foo
echo mailx ${ATTACH:+-a} "${ATTACH}" ${SUBJECT:+-s} "$SUBJECT" ${CC_MAIL:+-c} "$CC_MAIL" ${BCCMAIL:+-b} "${BCCMAIL}" ${FROM:+-r} "${FROM}" "${TO_LIST}"
SUBJECT=bar
echo mailx ${ATTACH:+-a} "${ATTACH}" ${SUBJECT:+-s} "$SUBJECT" ${CC_MAIL:+-c} "$CC_MAIL" ${BCCMAIL:+-b} "${BCCMAIL}" ${FROM:+-r} "${FROM}" "${TO_LIST}"
印刷
mailx
mailx -a foo
mailx -a foo -s bar