0

I am getting successful output when i run below script. What i am trying here is, i should connect to all below VM's listed and when script ssh to wasadmds03 then it should print output as product detail, similarly for other VM's and also it should execute the command "cat /source/package.json | grep version" on all VM's.

I am getting the output what i need - but is there a way to trim the repeated "if then" statement? I have used "if then" statement four times, imagine if i want to do the similar exercise for 50 VM's is there a way to write this program in a shorter way?

wasadmds03 - product detail
wasadmds04 - product list
wasadmls03 - Cart
wasadmls05 - checkout
  • Script

**

#!/bin/bash
for i in `cat /tmp/ms_test`
do
        if [ $i = wasadmds03 ]
        then
            echo "Product Detail"
            ssh $i "cat /source/package.json | grep version"
        fi
        if [ $i = wasadmds04 ]
        then
            echo "Product list"
            ssh $i "cat /source/package.json | grep version"
        fi
        if [ $i = wasadmls03 ]
        then
            echo "Merchant"
            ssh $i "cat /source/package.json | grep version"
        fi
        if [ $i = wasadmls05 ]
        then
            echo "Account Details"
            ssh $i "cat /source/package.json | grep version"
        fi
done

1 Answers1

3

So the issue seems to be that you'd like to output a specific label depending on the host name read from the file.

We can certainly compact the code somewhat. If we store the text label that should be different for each host in an associative array, we can end up with just

printf '%s\n' "${labels[$remote]}"
ssh -n "$remote" 'cat /source/package.json' | jq -r .version

... inside the loop.

Here, I've taken the liberty of renaming the i loop variable to remote which is much more descriptive. I'm also using printf instead of echo (see "Why is printf better than echo?"), and I chose to parse that JSON data file using a real JSON parser, jq, rather than with grep (this also means that I'm just guessing that there is a top-level version key in the JSON data).

ssh must be called with ssh -n so that it doesn't read the input that the loop we're using iterates over.

The labels array may be created like so:

declare -A labels
labels=(
    ['wasadmds03']='Product Detail'
    ['wasadmds04']='Product List'
    ['wasadmls03']='Merchant'
    ['wasadmls05']='Account Details'
)

Note that if you have more remote host names, these should be listed in this array in a similar way. You will need to list each one since you don't provide this data in any other way.

This, together with properly reading the remote host names from /tmp/ms_test (see "Understanding "IFS= read -r line"") gives us the following script:

#!/bin/bash

declare -A labels
labels=(
    ['wasadmds03']='Product Detail'
    ['wasadmds04']='Product List'
    ['wasadmls03']='Merchant'
    ['wasadmls05']='Account Details'
)

while IFS= read -r remote; do
    printf '%s\n' "${labels[$remote]}"
    ssh -n "$remote" 'cat /source/package.json' | jq -r .version
done </tmp/ms_test
Kusalananda
  • 333,661