0
USER=ssubra
KEY=.ssh/shantip_rsa
HOST1=cassandraeast1
HOST2=cassandraeast2
HOST3=cassandraeast3
HOST4=cassandraeast4
HOST5=cassandrawest1
HOST6=cassandrawest2
HOST7=cassandrawest3
HOST8=cassandrawest4

no_key_check="-o LogLevel=QUIET -o StrictHostKeyChecking=no"
SSH1="ssh -i $KEY $no_key_check $USER@$HOST1"

for num in {1..8}
do
  echo "SSH$num=ssh -i $KEY $no_key_check $USER@$HOST$num"
done

**I get output as **

My code is not reading the $HOST but instead it is reading the $num

SSH1=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@1
SSH2=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@2
SSH3=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@3
SSH4=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@4
SSH5=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@5
SSH6=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@6
SSH7=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@7
SSH8=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@8

Required Output

SSH1=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast1
SSH2=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast2
SSH3=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast3
SSH4=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast4
SSH5=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest1
SSH6=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest2
SSH7=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest3
SSH8=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest4

Its not taking as $HOST1 instead its taking 1 and leaving $HOST

shrikkanth roxor
  • 237
  • 1
  • 3
  • 8
  • 1
    Does this answer your question? [Dynamic variable names in Bash](https://stackoverflow.com/questions/16553089/dynamic-variable-names-in-bash) – a1ezh Apr 09 '20 at 16:21

2 Answers2

1

You can use bash indirect expansion. Something like this:

for num in {1..8}
do
  host="HOST$num"
  echo "SSH$num=ssh -i $KEY $no_key_check $USER@${!host}"
done
a1ezh
  • 412
  • 3
  • 10
1

I would probably want to make this a bit easier by using an array to store the different HOST values, eg:

$ HOST=(dummy cassandraeast1 cassandraeast2 cassandraeast3 cassandraeast4 cassandrawest1 cassandrawest2 cassandrawest3 cassandrawest4)

NOTE: I added 'dummy' as a place holder to fill the position with index=0; this allows you to continue to use your current range ({1..8}) without any code modifications.

$ typeset -p HOST
declare -a HOST=([0]="dummy" [1]="cassandraeast1" [2]="cassandraeast2" [3]="cassandraeast3" [4]="cassandraeast4" [5]="cassandrawest1" [6]="cassandrawest2" [7]="cassandrawest3" [8]="cassandrawest4")

NOTE: You can remove the 0 index entry if you want (though it shouldn't cause any problems for your current code):

$ unset HOST[0]
$ typeset -p HOST
declare -a HOST=([1]="cassandraeast1" [2]="cassandraeast2" [3]="cassandraeast3" [4]="cassandraeast4" [5]="cassandrawest1" [6]="cassandrawest2" [7]="cassandrawest3" [8]="cassandrawest4")

You can then modify your code as follows to reference the array values:

old: echo "SSH$num=ssh -i $KEY $no_key_check $USER@$HOST$num"
new: echo "SSH${num}=ssh -i ${KEY} ${no_key_check} ${USER}@${HOST[${num}]}"

NOTE: In this case the braces ({}) are optional around the variable names but I prefer to use them in order to make the variables names a bit more noticeable amongst the rest of the code.

Which should give you the desired output:

SSH1=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast1
SSH2=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast2
SSH3=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast3
SSH4=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandraeast4
SSH5=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest1
SSH6=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest2
SSH7=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest3
SSH8=ssh -i .ssh/shantip_rsa -o LogLevel=QUIET -o StrictHostKeyChecking=no ssubra@cassandrawest4
markp-fuso
  • 28,790
  • 4
  • 16
  • 36