gpt4 book ai didi

string - 为什么我在 Bash 中的字符串比较总是被评估为真?

转载 作者:行者123 更新时间:2023-11-29 08:53:59 26 4
gpt4 key购买 nike

我有一个脚本来检查文件是否是最新的。

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

但是当我运行它时,比较总是正确的:

$ ./checkfile
10:04 10:07
OK

$ ./checkfile
10:07 10:07
OK

为什么?

最佳答案

你需要在等号两边各留一个空格。


if [ "$updatedate" = "$nowdate" ]

关于string - 为什么我在 Bash 中的字符串比较总是被评估为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6000603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com