Reputation: 47
I have this part of a code, and I cant figure out how to implemente what I want.
hostname_negate="SERVER03"
CLUSTER="SERVER01 SERVER02 SERVER03 SERVER04 SERVER05 SERVER06 SERVER07 SERVER08 SERVER09 SERVER10"
for SERVIDOR in $CLUSTER
do
If [ $SERVIDOR == $hostname_negate ]; then
===SOME ADMINISTRATIVE STUFF HERE ===
else
===SOME ADMINISTRATIVE STUFF HERE===
fi
done
This work if only one server is in hostname_negate... bute if I have 100 servers and 10 are in hostname_negate.... how I implement this to work?
Upvotes: 0
Views: 63
Reputation: 2263
In the long run you'll probably be better served having the lists in files instead of strings.
CLUSTER="SERVER01 SERVER02 SERVER03 SERVER04 SERVER05 SERVER06 SERVER07 SERVER08 SERVER09 SERVER10"
NEGATES="SERVER03 SERVER07 BROKENSERVER01"
# Convert from string to list in a file.
# Generally better to have these in files instead of strings.
echo "$CLUSTER" | tr ' ' '\n' > /tmp/cluster.$$
echo "$NEGATES" | tr ' ' '\n' > /tmp/negates.$$
for server in $(comm -12 <(sort /tmp/cluster.$$) <(sort /tmp/negates.$$))
do
echo "In both lists: $server"
done
for server in $(comm -23 <(sort /tmp/cluster.$$) <(sort /tmp/negates.$$))
do
echo "Unique to CLUSTER: $server"
done
for server in $(comm -13 <(sort /tmp/cluster.$$) <(sort /tmp/negates.$$))
do
echo "Unique to NEGATES: $server"
done
# Clean up.
rm /tmp/cluster.$$ /tmp/negates.$$
Results in:
in both lists: SERVER03
in both lists: SERVER07
unique to CLUSTER: SERVER01
unique to CLUSTER: SERVER02
unique to CLUSTER: SERVER04
unique to CLUSTER: SERVER05
unique to CLUSTER: SERVER06
unique to CLUSTER: SERVER08
unique to CLUSTER: SERVER09
unique to CLUSTER: SERVER10
unique to NEGATES: BROKENSERVER01
Upvotes: 1
Reputation: 7791
A loop from both the lists, but convert both of them to an array first, something like.
#!/usr/bin/env bash
hostname_negate=(
"SERVER03" "SERVER07" "SERVER10"
)
cluster=(
"SERVER01" "SERVER02" "SERVER03" "SERVER04" "SERVER05" "SERVER06" "SERVER07" "SERVER08" "SERVER09" "SERVER10"
)
for servidor in "${cluster[@]}" ; do
for host_name in "${hostname_negate[@]}"; do
if [[ $servidor == $host_name ]]; then
printf '%s %s is a match!\n' "$servidor" "$host_name"
else
printf '%s %s does not match!\n' "$servidor" "$host_name"
fi
done
done
Or if you're just trying to check if hostname_negate
is in cluster
, then
#!/usr/bin/env bash
hostname_negate=(
"SERVER03" "SERVER07" "SERVER10" "SERVER99"
)
cluster=(
"SERVER01" "SERVER02" "SERVER03"
"SERVER04" "SERVER05" "SERVER06"
"SERVER07" "SERVER08" "SERVER09"
"SERVER10" "SERVER11" "SERVER12"
"SERVER13" "SERVER14" "SERVER15"
"SERVER16" "SERVER17" "SERVER18"
"SERVER19" "SERVER20" "SERVER21"
"SERVER22" "SERVER23" "SERVER24"
)
cluster_pattern=$(IFS='|'; printf '%s' "@(${cluster[*]})")
for host_name in "${hostname_negate[@]}"; do
if [[ $host_name == $cluster_pattern ]]; then
printf '%s is in %s!\n' "$host_name" $cluster_pattern
else
printf '%s is not in %s!\n' "$host_name" $cluster_pattern
fi
done
Just change the printf's
to the Administrative stuff that you wanted to do.
Upvotes: 3