有人可以帮助我理解为什么第一个条件总是被执行,好像条件总是真的,但显然不是吗?
#!/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!"
当我像这样运行时
$ ./mybashscript somefilename.txt 10 42 53 14
我明白了
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!
我希望输出看起来像这样
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
我尝试使用[[EXPRESSION]]的每个组合以及使用单个' ='而不是doube' =='引号如" $ got_file" =" false"或者其他结果总是相同的,第一个if语句总是被执行。