我编写了一个 shell 脚本来检查哪些“.err”文本文件为空。有些文件具有特定的重复短语,例如此示例文件fake_error.err
(有意使用空行):
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
除了空文件之外我还想删除它。我编写了以下脚本来执行此操作
#!/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
但是,该脚本在文件上运行时的输出fake_error.err
是
WARNING: reaching max number of iterations
WARNING: reaching max number of iterations
来自循环中的两个$echo
语句,但文件本身不会被删除,并且"Found reached max iterations!"
不会打印字符串。我认为问题出在if [ "$lines" = "$EXPECTED" ]
,我尝试使用双括号[[ ]]
,==
但这些都不起作用。我不知道这两个打印的声明有什么区别。
为什么两个变量不相等?