Bash - 字符串比较不起作用
Bash - String comparison does not work
我正在尝试比较 .sh 文件中的两个变量,但它从来没有起作用 ;-(
我如何比较它们?
curDate=2014-03-09
nextDate=2014-04-17
if [ “$nextDate” = “$curDate” ]; then
echo $curDate = $nextDate
else
echo $curDate != $nextDate
fi
应该是字符串的所有内容最好用引号引起来:
echo "$curDate != $nextDate"
而不是
$curDate != $nextDate
也考虑引用日期分配。
尝试运行这个:
var=asd das
for thing in $var; do
echo $thing
done
然后 var="asd das"
除此之外 - 在 if [ “$nextDate” = “$curDate” ]; then
中使用 "
而不是 “
我找到了解决方案。
在 Mac OSX 中,此解决方案有效:
#!/bin/bash
a=2014-03-09
b=2014-03-09
if [[ $a == $b ]] ; then
echo Found.
else
echo not found
fi
我正在尝试比较 .sh 文件中的两个变量,但它从来没有起作用 ;-(
我如何比较它们?
curDate=2014-03-09
nextDate=2014-04-17
if [ “$nextDate” = “$curDate” ]; then
echo $curDate = $nextDate
else
echo $curDate != $nextDate
fi
应该是字符串的所有内容最好用引号引起来:
echo "$curDate != $nextDate"
而不是
$curDate != $nextDate
也考虑引用日期分配。
尝试运行这个:
var=asd das
for thing in $var; do
echo $thing
done
然后 var="asd das"
除此之外 - 在 if [ “$nextDate” = “$curDate” ]; then
"
而不是 “
我找到了解决方案。 在 Mac OSX 中,此解决方案有效:
#!/bin/bash
a=2014-03-09
b=2014-03-09
if [[ $a == $b ]] ; then
echo Found.
else
echo not found
fi