为什么在Bash中我的字符串比较总是被评估为true? [英] Why is my string comparison in Bash always evaluated as true?

查看:89
本文介绍了为什么在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" ]

这篇关于为什么在Bash中我的字符串比较总是被评估为true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆