0

i am trying to execute a IF condition inside a while loop, But the IF condition isn't working as variables aren't expanding! kindly guide me through the proper way to compare two variables in IF condition Note - if you can see the error log - DATE was expanding thou! problem with mdate

DATE=`date +"%Y-%m-%d"`
cat path/temp_b | while read file

 
do
    
    echo 'phase2'
    mtime=$(stat -c '%y' $Src_Dir/$file)
    echo $mtime
    mdate= echo $mtime | cut -d ' ' -f1
    echo $mdate
    echo $DATE
     if ["$mdate"=="$DATE"]; then
        "$file" > path/tempc
     else
        echo 'hi'
 
     fi
done

**Error log -

phase2

2020-05-07 05:22:28.000000000 -0400

2020-05-07

2020-07-21

./test1.ksh: line 37: [==2020-07-21]: command not found

hi**

4
  • 4
    shellcheck.net Commented Jul 21, 2020 at 23:30
  • Thank you! solved! :) Commented Jul 21, 2020 at 23:40
  • mdate= echo $mtime | cut -d ' ' -f1 is not an assignment at all. Commented Jul 22, 2020 at 4:43
  • @Alekhyavarma : If you really posted here the exact, complete error message you got, this is not run by bash either. Commented Jul 22, 2020 at 6:12

1 Answer 1

0

Change your if statement like:

if [ "$mdate" == "$DATE" ]; then

Explanation: if in bash needs to have square brackets, operator, and operand to be space-separated.

Sign up to request clarification or add additional context in comments.

2 Comments

== is not guaranteed to work. To be portable it should only be =. See pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
... Also, the OP's code has additional bugs this answer currently doesn't address.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.