关于ubuntu服务器的参数太多了

时间:2014-09-12 03:17:59

标签: linux bash ubuntu

我在ubuntu服务器上收到错误“第22行:[:太多参数”,我不太确定如何修复它。只是想知道它有什么解决方案吗?这是我的代码。

if [ $? -eq 0 ]; then
   if [ $name = $ufname ]; then
      echo "Names are still the same"
   fi
fi

1 个答案:

答案 0 :(得分:2)

必须非常小心可能包含空格或其他特殊字符的变量。空白可以真正推动工作。

bash中的最佳解决方案是使用[[而不是[。它处理具有优雅和风格的空白。我建议在所有情况下切换到[[,而不要使用[[[ is better in all respects.

if [[ $? -eq 0 ]]; then
   if [[ $name = "$ufname" ]]; then
      echo "Names are still the same"
   fi
fi

使用[的唯一原因是如果可移植性是一个问题 - 就像你正在为普通sh而不是bash而写。在这种情况下,您必须坚持[,因此您应该引用您的变量。

if [ $? -eq 0 ]; then
   if [ "$name" = "$ufname" ]; then
      echo "Names are still the same"
   fi
fi
相关问题