0

Can someone help me understand why the first conditional is always executed as if the condition is always true when it is clearly not?

#!/bin/bash

echo "starting..."


got_file="false"
got_sleep_time="false"
file="None"
sleeptime=0

for i in $@;
do
        if [ $got_file==false ]; then
                file="$i"
                echo "file is $file, got_file is $got_file, setting it to true."
                got_file=true
        elif [ $got_sleep_time==false ]; then
                sleep_time="$i"
                got_sleep_time=true
        else
                echo "-WE MADE IT-"
                echo "The number is $i"
                echo date
                sleep $sleep_time
                echo "--- moving on---"
        fi
done
echo "from the cli we get file=$file, sleeptime=$sleeptime s"
echo "done!"

when I run it like this

$ ./mybashscript somefilename.txt 10 42 53 14

I get this

starting...
file is somefilename.txt, got_file is false, setting it to true.
file is 10, got_file is true, setting it to true.
file is 42, got_file is true, setting it to true.
file is 53, got_file is true, setting it to true.
file is 14, got_file is true, setting it to true.
from the cli we get file=14, sleeptime=0
done!

I expect the output to look like this

file is somefilename.txt, got_file is false, setting it to true.
-WE MADE IT-
The number is 42
Mon Apr 16 21:44:21 UTC 2018
-WE MADE IT-
The number is 53
Mon Apr 16 21:44:31 UTC 2018
-WE MADE IT-
The number is 14
Mon Apr 16 21:44:41 UTC 2018
from the cli we get file=somefilename.txt, sleeptime=10 s

I tried every combination of if with [[ EXPRESSION ]] as well as using single '=' instead of doube '==' and quotes like "$got_file"="false" or whatever and the result is always the same, the first if statement is always executed.

ChumbiChubaGo
  • 968
  • 2
  • 9
  • 15
  • 2
    Running [shellcheck](https://shellcheck.net) is always a good first step. It automatically points out that you need spaces around the `==` – that other guy Apr 16 '18 at 22:07
  • @thatotherguy Can you post this up as an answer so I can choose it as solved? This is exactly what is wrong, it works now that I put spaces between the logical operator – ChumbiChubaGo Apr 16 '18 at 22:09

0 Answers0