I wrote a shell script to check which ".err" text files are empty. Some files have a specific repeated phrase, like this example file fake_error.err (blank lines intentional):
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
that I want to also remove in addition to the empty files. I wrote the following script to do so
#!/bin/bash
for file in *error.err; do
if [ ! -s $file ]
then
echo "$file is empty"
rm $file
else
# Get the unique, non-blank lines in the file, sorted and ignoring blank space
lines=$(grep -v "^$" "$file" | sort -bu "$file")
echo $lines
EXPECTED="WARNING: reaching max number of iterations"
echo $EXPECTED
if [ "$lines" = "$EXPECTED" ]
then
# Remove the file that only has iteration warnings
echo "Found reached max iterations!"
rm $file
fi
fi
done
However, the output of this script when run on the fake_error.err files is
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
from the two $echo statements in the loop, but the file itself is not deleted and the string "Found reached max iterations!" is not printed. I think the issue is in if [ "$lines" = "$EXPECTED" ] and I've tried using double brackets [[ ]] and == but none of those worked. I have no idea what the difference between the two printed statement are.
Why are the two variables not equal?