0

We have a script that runs and generates multiple files, but not every file will have content. The script writes any output to a single file, but we break out the results to other files. Files contain ping results (slow, not reachable, not accessible), so there are some instances where we can reach all so the other two files will initially be created, but by the end of the script may not have anything in them. We have lines that remove the files, but after the mail is sent, so we have files attached that are there, but empty.

Is there a way outside of writing a multi-if conditional statement that will ONLY attach files that have lines or content?

This is my line for mailx to send the email with attachments:

(cat $FINAL) | mailx -s "Application ${VER} BashScript processing results" 
-a $LOGFILE -a $INACHOSTLIST -a $FAILHOSTLIST -a $SLOWHOSTLIST

If any of these $INACHOSTLIST, $FAILHOSTLIST or $SLOWHOSTLIST are empty, I would prefer not to attach them. May seem trivial to leave them attached with nothing in them, but trying to clean up the process. Does this require that each possible combination be validated through an if statement?

Jon T
  • 135
  • 1
  • 1
  • 9
  • 1
    you could use `test -z`. `[[ -z $INACHOSTLIST || -z $FAILHOSTLIST || ... ]]`. As a side note, that subshell around the cat is not doing anything, and you don't need to pipe that at all. You can just feed FINAL with a herestring: `mailx ... <<< "$FINAL"`. (Also, you really shouldn't capitalize variable names unless it is an environment variable: https://stackoverflow.com/a/42290320/3784644) – DTSCode Aug 15 '18 at 15:59
  • What's wrong with using an if statement? I don't think there's a real question here because the only way of really doing this is using flow control, i.e. an "if" statement, either verbose or condensed to "test", "[" or "[[", either way you should focus on NOT creating empty files instead so you could just attach all existing files and not worry about it – Ulises André Fierro Aug 15 '18 at 16:20
  • The (cat $FINAL) | mailx... is putting the content of $FINAL into the body of the email. I've tried it a number of different ways (found everywhere and of my own knowledge) and nothing would drop the content into the email except that. Thank you for the note too about vars! Working from someone else's script, making updates and changes, but noted! Not against using an if statement, was asking if there was a way to validate the files for attachment when one or more than one could be empty without running out all the combinations. – Jon T Aug 15 '18 at 16:44
  • Oh, my mistake, I meant `mailx < "$FINAL` not a herestring. That works just as well (and even better) than `(cat $FINAL) | ...`. – DTSCode Aug 15 '18 at 16:52
  • Darn it, that worked for getting the content into the email. :D mailx ... < "$FINAL" Thanks @DTSCode – Jon T Aug 15 '18 at 16:53

0 Answers0