Linux

Mailx -E 標誌失敗

  • June 3, 2015

我正在使用帶有 -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)

.However我不能在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="support@abc.com"
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 標誌在我的伺服器上不起作用

只要輸入對於您的臨時目錄來說不是太大,就足以測試和發送。檢查文件是否具有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 “The following files has been moved” " $ 電子郵件" 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"

引用自:https://unix.stackexchange.com/questions/207293