用if-else语句循环的Bash脚本

时间:2017-03-22 19:13:26

标签: bash shell variables if-statement

我尝试编写一个导入所有用户的bash脚本,并为每个帐户执行一些操作。

a="some account"
b="some account" 
#... so on

USERS = #get imported

for ACCOUNT in $USERS; do
        if ["$ACCOUNT" == "$a"] || ["$ACCOUNT" == "$b"] || ["$ACCOUNT" == "$c"] || ["$ACCOUNT" == "$d"]; then
                echo "Skipping $ACCOUNT"
        else
                echo "this"
                echo "this $ACCOUNT"
        fi
done

但似乎有一个问题。对于每个帐户,我都会关注:

  

[thisis @ anaccount:not found

我尝试删除单个括号并将整个if子句放入双括号中,但这会导致相同的错误。

我还删除了每个变量调用的双引号,但这似乎不是问题。

有人可以提供建议吗?

1 个答案:

答案 0 :(得分:2)

你只是缺少一些空间......

#!/bin/bash 
a="ACC_A"
b="ACC_B" 
c="ACC_C"
d="ACC_D"

USERS="ACC_A ACC_B ACC_C ACC_X ACC_Y ACC_Z" 

for ACCOUNT in $USERS; do
        if [ "$ACCOUNT" = "$a" ] || [ "$ACCOUNT" = "$b" ] || [ "$ACCOUNT" = "$c" ] || [ "$ACCOUNT" = "$d" ]; then
              echo "Skipping $ACCOUNT"
    else
              echo "this $ACCOUNT"
    fi
done

并快速测试:

$ bash accounts.sh 
Skipping ACC_A
Skipping ACC_B
Skipping ACC_C
this ACC_X
this ACC_Y
this ACC_Z