[Bash]syntax error near unexpected token 'then'

#!/bin/bash
clear
function test
{
   if[$1 -eq "root"]&&[$2 -eq "123456"]
      then
          echo "Right"
      else
          echo "Wrong"
   fi
}
test root 123456

上面這個程序我執行時,報這個錯誤,剛接觸shell,沒想到它的語法這麼……不說了,在網上找個緣由,貼出正確的格式,並總結要注意的幾點shell

總結:bash

1.if後要有空格code

2.[] 中括號的開頭和結尾要有空格!字符串

3. [ $1 == "root" ]  中括號中的字符串比較 應該使用 == 而不是  -eq!io

相關文章
相關標籤/搜索