0

I am trying to write a script to login into different systems by providing system name as input and making it variable by read option. However when i try to compare it with defined Array it's throwing me error and stating command not found.

Succeeded in making use input as variable but not able to compare it properly with defined array. Below is the code i have written.

#!/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
4
  • 3
    Please paste your script there to fix the syntax errors. Commented May 5, 2019 at 14:33
  • 2
    Add some spaces, as in if [ "${clname}" = "${item}" ] ; then Commented May 5, 2019 at 14:37
  • 3
    Remove , from declaration of array cluster. Commented May 5, 2019 at 14:48
  • After Couple of changes, its working fine now. Tq @LjmDullaart and Cyrus Commented May 5, 2019 at 16:29

1 Answer 1

1

alternative:

#!/bin/bash
cluster=("namico1c.mylabserver.com" "namico2c.mylabserver.com")
select clname in "${cluster[@]}"; do
    ssh test@$clname
    break
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.