Raz Maabari
Raz Maabari

Reputation: 31

Multiple & different Variables for loops BASH

I would like to run a for loop on a list of two columns, first column holds a name of a server, and the second column holds the UUID of the same server.

I would like to run the another loop on the same server name and UUID and only after this second loop completed I would like to return to the first loop but this time for the next server name & UUID.

All of these loops should be running only while a specifc condition occurs.

Any suggestions on how can I execute the following? I was thinking of doing it this way:

sudo ceph -s| grep 'osds: XX up' wc -l > ceph s
CEPH=(cat ceph s)
SERVER=(cat info/servernames)
UUID=(cat info/uuids)
AUTH= (cat info/token)
SPT=$(cat info/SPT)

while [ SCEPH -eq] 1 ]
do
set --$SERVER
for i in $UUID
do
## Poveroff #
echo "UUID: ${i}/powerState"
echo "TOKEN: ${AUTH}"
echo "name: $1"
echo "UUID: $1"
echo "SPT:$SPT"
sleep 2s


echo applying SPT
echo "TOKEN: $AUTH"
echo "NAME: $1"
echo "UUID:$i"


# shift
ping -c 1 "$1" >> /dev/null 2>&1
    if [ $? -eq 0 ]; then
        shift
    else 
        echo "$1 is down"   ###should be the same sevrername echoed in the first loop###
    fi

sleep 2m
done
done
echo

#apply spt ## again spouse to run for the same servername and UUID ##
echo "APPLY SPTI
while [ $CEPH -eq 0 ]
do
set -- $SERYER
for
i in $UUID
    do
    echo "TOKEN: SAUTH"
    echo "NAME: $1"
    echo "UUID: Si"
shift
sleep 5s
done
done

example of UUID and server files

cat uuid
VDIVUIFDVNFDHYJ
VDIVUIFDVNFDTRE
VDIVUIFDVNFBVCN
VDIVUIFDVNFDNAQ
cat serversnames
SERVER1
SERVER2
SERVER3
SERVER4

that means, SERVER1 should be getting the UUID of the first line and so on. Once all loops completed i want the next servername and uuid to run.

Any thoughts?

Upvotes: 1

Views: 151

Answers (1)

j_b
j_b

Reputation: 2020

You could read the files in the same while loop using separate file descriptors. The loop will stop when the shortest file is fully read:

#!/bin/bash

servernames="/tmp/info/servernames"
uuids="/tmp/info/uuids"


# using file descriptors
while IFS= read -r server && IFS= read -r uuid <&3; do

  echo "server: ${server} uuid: ${uuid}"

  # ping server

  # apply spti

done <${servernames} 3<${uuids}

Sample output:

server: SERVER1 uuid: VDIVUIFDVNFDHYJ
server: SERVER2 uuid: VDIVUIFDVNFDTRE
server: SERVER3 uuid: VDIVUIFDVNFBVCN
server: SERVER4 uuid: VDIVUIFDVNFDNAQ

Upvotes: 1

Related Questions