In bash you have a test specifically to see if a file exists and has a size greater than 0 bytes: -s. There is no need to call ls and then awk to extract the size.
Here's how to do that:
Code:
#!/bin/bash
FILE=/path/to/a/file
if [ -s "$FILE" ]; then
cat $FILE | mail ...
fi
Incidentally, you don't have to use the if ... then ... fi construct for this, it's much more compact and pretty to do it like this:
Code:
#!/bin//bash
FILE=/path/to/a/file
[ -s "$FILE" ] && cat "$FILE" | mail ...
Which style you use is a matter of taste really.
If you want to check multiple files, doing the same action on each, and do not much feel like copy-pasting the whole statement, you can use a function which takes a file name as an argument, and performs the test and mail action. Once you've written the function, you can call it with as many file names as you like:
Code:
#!/bin/bash
mail_if_non_zero_size () {
[ -s "$1" ] && cat "$1" | mail ...
}
mail_if_non_zero_size "/path/to/some/file"
mail_if_non_zero_size "/path/to/a/second/file"
mail_if_non_zero_size "/path/to/yet/another/file"
The thing to know is that within a shell function, arguments which are passed when the function is called get assigned to the variables 1, 2, 3, 4, ... so you can take their value with $1, $2, $3, $4, ...
The -s test is one of many file-related tests. See the "CONDITIONAL EXPRESSIONS" section of the bash manual page for a full list.