if/then 循环使用 `cat`

0 shell-script

团队.txt:

Bills
Jets
Dolphin
Patriots
Run Code Online (Sandbox Code Playgroud)

.

for team in `cat teams.txt`
do
    if ["$team" == "Bills"]
    then
        echo "$team hired Rex Ryan as coach"
    fi
    echo "$team Nation"
done
Run Code Online (Sandbox Code Playgroud)

我不断收到错误:

teams.sh: line 5: [Bills: command not found
Run Code Online (Sandbox Code Playgroud)

我不确定我的代码做错了什么。

小智 5

您在 [ 和 ] 周围缺少空格。它应该是这样的:

for team in `cat teams.txt`
do
    if [ "$team" == "Bills" ]
    then
        echo "$team hired Rex Ryan as coach"
    fi
    echo "$team Nation"
done
Run Code Online (Sandbox Code Playgroud)