如何将用户输入与数组进行比较并执行命令?

时间:2019-05-05 14:31:18

标签: linux bash shell scripting

我试图通过提供系统名称作为输入并通过读取选项使其可变来编写脚本以登录到不同的系统。但是,当我尝试将其与定义的Array进行比较时,会抛出错误并指出未找到命令。

成功将输入用作变量,但无法将其与定义的数组正确比较。 下面是我编写的代码。

#!/bin/bash
cluster=("namico1c.mylabserver.com","namico2c.mylabserver.com")
echo "Please enter a Cluster Name to login: "
read clname
for item in ${cluster[@]};do
    echo ${item};
    if ["${clname}"="${item}"]; then
     ssh test@$clname
    else
     echo "Cluster is not correct"
    fi
done
[test@namico3c ~]$ ./test.sh
Please enter a Cluster Name to login: 
namico1c.mylabserver.com
namico1c.mylabserver.com,namico2c.mylabserver.com
./test.sh: line 7: [namico1c.mylabserver.com=namico1c.mylabserver.com,namico2c.mylabserver.com]: command not found
Cluster is not correct

1 个答案:

答案 0 :(得分:1)

替代:

#!/bin/bash
cluster=("namico1c.mylabserver.com" "namico2c.mylabserver.com")
select clname in "${cluster[@]}"; do
    ssh test@$clname
    break
done