Mailx -E 标志失败

Mailx -E 标志失败

我正在使用带 -E 标志的 mailx 命令。这就是 Linux 手册页中的内容。

-E     If an outgoing message does not contain any text in its first or
              only message part, do not  send  it  but  discard  it  silently,
              effectively   setting  the  skipemptybody  variable  at  program
              startup.  This is  useful  for  sending  messages  from  scripts
              started by cron(8)

。我怎么不能在 AIX 服务器上使用 -E 标志。有什么想法吗?或者我可以使用任何替代品吗?这是脚本`

#!/usr/bin/env bash
shopt -s nullglob #to make `("$src_dir"*.300)` works
src_dir="/exports/files/" #don't forget trailing slash /
dest_dir="/exports/files/arch/" #don't forget trailing slash /
err_f="/tmp/error.txt"
mv_f="/tmp/moved.log" #record moved file in case network down
email="[email protected]"
touch "$err_f" #bcoz we use >> apppend
touch "$mv_f" #bcoz we use tee -a append
if [ ! -d "$src_dir" ]; then echo|mailx -s "Error: directory $src_dir not exist" "$email" 2>>"$err_f"; exit 1; fi
if [ ! -d "$dest_dir" ]; then echo|mailx -s "Error: directory $dest_dir not exist" "$email" 2>>"$err_f"; exit 1; fi
{
f=("$src_dir"*.300)
for ((i=0; i < ${#f[@]}; i+=1)); do
        mv -f "${f[i]}" "$dest_dir"  2>>"$err_f"; #-f do not prompt
        if [ $? -eq 0 ]; then
                if [ "$i" -eq 0 ]; then echo "$(date +"%Y-%m-%d %H:%M:%S")"; echo "The following files has been moved from $src_dir to $dest_dir"; echo; fi
                echo "$((i+1))." "$(basename "${f[i]}")" 'moved'; echo;
        else
                 echo| mailx -s "Error:  $(<"$err_f")" "$email" 2>>"$err_f"; break
        fi
done
} | tee -a "$mv_f" | mailx -E -s "The following files has been moved" "$email" 2>>"$err_f"

有什么方法可以删除 -E 标志,如果我删除它,它会在没有文本时发送空消息。我想抑制它。不知怎的,-E 标志在我的服务器上不起作用

答案1

如果输入不足够,则足以进行测试和发送太大了为您的临时目录。检查test -s文件大小是否非零:

.... > "${TMPDIR:-/tmp}/mail.$$.tmp"    # Write to temporary file

test -s "${TMPDIR:-/tmp}/mail.$$.tmp" && mailx ... < "${TMPDIR:-/tmp}/mail.$$.tmp"   # Send email if non-zero
rm -f "${TMPDIR:-/tmp}/mail.$$.tmp"     # Delete temporary file

在您的特定实例中,这是最后一行的更改:

} |三通-a“$mv_f”| mailx -E -s "以下文件已被移动" "$email" 2>>"$err_f"

对此:

} | tee -a "$mv_f" > "${TMPDIR:-/tmp}/mail.$$.tmp"

test -s "${TMPDIR:-/tmp}/mail.$$.tmp" &&
    mailx -s "The following files has been moved" "$email" < "${TMPDIR:-/tmp}/mail.$$.tmp" 2>>"$err_f"
rm -f "${TMPDIR:-/tmp}/mail.$$.tmp"

相关内容