I am using mailx command with -E flag.This is what it says in man page for 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)

.How ever i cannot use -E flag on AIX servers.any idea ? or is there any substitute i can you use?this is the script`

#!/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 /
mv_f="/tmp/moved.log" #record moved file in case network down
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
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;
                 echo| mailx -s "Error:  $(<"$err_f")" "$email" 2>>"$err_f"; break
} | tee -a "$mv_f" | mailx -E -s "The following files has been moved" "$email" 2>>"$err_f"

is ther any way i can remove -E flag if i do remove it sends null message when there is no text.I want to supress it.Somehow -E flag doesnot work on the my servers

  • @林果皞 is there a way to change it ? – Zaira banu Jun 3 '15 at 17:51

Ightforward enough to test and send, provided the input isn't too large for your temporary directory. The test -s checks that the file has a non-zero size:

.... > "${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

In your specific instance, it's the last line that gets changed from this:

} | tee -a "$mv_f" | mailx -E -s "The following files has been moved" "$email" 2>>"$err_f"

to this:

} | 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"
  • @ roaima I donot have to use -E flag then in mailx command correct?, testing it – Zaira banu Jun 3 '15 at 20:13
  • tried didnt help,script hanging..how ever the files moved.I do see files moved to moved.log. but didnt receive any email..have to exit out of script – Zaira banu Jun 3 '15 at 20:23
  • i monitored the script progress looks like its getting hang and mailx command ./test.script >output.txt 2>&1 then other window tail -f output.txt to monitor progress... – Zaira banu Jun 3 '15 at 20:43
  • @Zairabanu oops you are correct. Script fixed x2 – roaima Jun 3 '15 at 20:48
  • genius.. :) worked like a charm //thank you so much. appreciate your help.. :) – Zaira banu Jun 4 '15 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.