为什么在Bash中我的字符串比较总是被评估为true?
问题描述:
我有一个脚本来检查文件是否最新.
I have a script checking if a file is up-to-minute.
updatedate=`ls -l file | sed -e 's/ */ /g' | cut -d' ' -f7` #cut the modification time
nowdate=`date +"%H:%M"`
echo "$updatedate $nowdate"
if [ "$updatedate"="$nowdate" ]
then
echo 'OK'
else
echo 'NOT OK'
fi
但是当我运行它时,比较总是正确的:
But when I run it, the comparison is always true:
$ ./checkfile
10:04 10:07
OK
$ ./checkfile
10:07 10:07
OK
为什么?
答
在等号的两边都需要有一个空格.
You need a space on each side of the equals sign.
if [ "$updatedate" = "$nowdate" ]