![Mailx -E フラグが失敗しました。](https://linux33.com/image/66248/Mailx%20-E%20%E3%83%95%E3%83%A9%E3%82%B0%E3%81%8C%E5%A4%B1%E6%95%97%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82.png)
-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
特定のインスタンスでは、最後の行が変更されます。
} | tee -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"