zsh:bash脚本动态生成字符串的比较
这与预期的效果一样:-zsh:bash脚本动态生成字符串的比较,bash,zsh,Bash,Zsh,这与预期的效果一样:- x="None of the specified ports are installed" if [ "$x" = "None of the specified ports are installed" ]; then echo 1; else echo 0; fi 我得到了1,这正是我所期望的 但这不起作用:- y="`port installed abc`" if [ "$y" = "None of the specified ports ar
x="None of the specified ports are installed"
if [ "$x" = "None of the specified ports are installed" ];
then echo 1;
else echo 0;
fi
我得到了1
,这正是我所期望的
但这不起作用:-
y="`port installed abc`"
if [ "$y" = "None of the specified ports are installed" ];
then echo 1;
else echo 0;
fi
我得到了0
,这不是我所期望的;即使
echo $y
未安装任何指定端口。
这里的主要区别在于,$y
由端口安装abc
命令动态确定。但是为什么这会影响我的比较呢?请仔细注意
None of the specified ports are installed
另一种方法是对粗心的错误不那么敏感,比如漏掉一个“.” 使用
[[]]
可以提供更强大的比较语句
也不用
y="`port installed abc`"
最好是写
y=$(port installed abc)
这只是在bash irc频道上与其他开发人员讨论的一些发现。Omy,这是一个粗心的错误。谢谢
y="`port installed abc`"
if [[ "$y" = *"None of the specified ports are installed"* ]];
then echo 1;
else echo 0;
fi
y="`port installed abc`"
y=$(port installed abc)