Reputation: 369
This standard check to see if a file is non-empty can be easily broken:
if [ -s "$FILE" ]; then
echo "contains stuff"
else
echo "empty"
fi
Like if I just put a newline in there it will count as something being there. How can I ensure that if something is there it is at least a valid character? I need to ensure the text in a file is in a format like this:
test
test
\n
and not something like this:
\n
\n
Upvotes: 1
Views: 81
Reputation: 113844
Replace
if [ -s "$FILE" ]; then
with
if grep -q . "$FILE"; then
grep . filename
returns true (exit code 0) if any line in the file matches .
(which, in regex, means any character). Newlines aren't matched. -q
tells grep to be quiet so grep -q . filename
performs the same test but silently, producing no output.
Upvotes: 3