我试图通过提供系统名称作为输入并通过读取选项使其可变来编写脚本以登录到不同的系统。但是,当我尝试将其与定义的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
答案 0 :(得分:1)
替代:
#!/bin/bash
cluster=("namico1c.mylabserver.com" "namico2c.mylabserver.com")
select clname in "${cluster[@]}"; do
ssh test@$clname
break
done