-2

I need help for bash scripting I have a folder with log files(number may vary) I have to test those files for content and if one or more of those files has content, I want to send the content by mail (I use mailx) being formatted like so:

The content of "filename" is: ------content--------- The content of "filename2" is: ------content--------- There was no content in "filename3".

Any practical way using bash to do this?

Thanks for your help.

  • This question asks for two different things: Sending emails from bash (see [here](http://stackoverflow.com/questions/5155923/sending-a-mail-from-a-linux-shell-script) for that), and testing if a file is empty (see [here](http://stackoverflow.com/questions/9964823/how-to-check-if-a-file-is-empty-in-bash-shell)). I don't know if combining two other questions into one makes a valid question, but even then, the title of the question should be updated to reflect that. – user000001 Jun 18 '16 at 08:50
  • Sorry, I should have said. Sending by mail I know how to do. My problem is with testing if file has content and then formatting this content so it can be sent. – christophe Larquey Jun 18 '16 at 09:07
  • More precisely looping through files in this folder to check which of them has content – christophe Larquey Jun 18 '16 at 09:08
  • `if [[ -s /etc/passwd ]]; then echo "file exists and is not empty"; fi` See: `help test` – Cyrus Jun 18 '16 at 09:09
  • Yes but in this exemple you are testing a file you know exists. In my case I don't know how many files are in this folder at any given time cause number varies constantly. Some appear, some disappear. So I lack the looping part. – christophe Larquey Jun 18 '16 at 09:12

1 Answers1

1
shopt -s nullglob
for i in *; do
  if [[ -s "$i" ]]; then 
    echo "file $i exists and is not empty"
  fi
done

See: man bash

Cyrus
  • 84,225
  • 14
  • 89
  • 153