Comparing strings in bash

I have a script if the file is up to date.

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 

Why?

+6
source share
2 answers

You need space on each side of the equal sign.

 if [ "$updatedate" = "$nowdate" ] 
+15
source

You need to separate all arguments before test space. Be that as it may, you have = directly against its two operands, so test sees one argument, not the three that you intend.

+2
source

Source: https://habr.com/ru/post/888095/


All Articles