Reputation: 1505
I have a script that reads a main list, call it List A
using a while loop.
List A:
east-1-1
east-1-2
east-1-3
east-1-4
east-1-5
east-1-6
east-1-7
west-1-1
west-1-10
south-1-1
south-1-2
north-1-1
What I would like to do is check it against this list:
east-1-1
east-1-3
east-1-7
west-1-10
south-1-2
As listed in my case
and for each of those items in my case
I would like to flag down with an *
case "${cluster}" in
east-1-1)
echo "${cluster}:${getCount} * ";;
east-1-3)
echo "${cluster}:${getCount} * ";;
east-1-7)
echo "${cluster}:${getCount} * ";;
west-1-10)
echo "${cluster}:${getCount} * ";;
south-1-2)
echo "${cluster}:${getCount} * ";;
esac
I am doing this within a function and using a case
statement, however I think I'm doing this wrong and would like to know if there's a better way? Should I be using an if-then-else
? Can I do this with the case
?
If I could please get some help on this approach or if there's a better way to write this.
CODE IN PROGRESS:
#!/usr/local/bin/bash
MAP=./.clusterinfo
if ! [ -f "$MAP" ]; then
echo "Cluster Map not found."
exit 1
fi
while IFS='' read -r cluster; do
#Function runQuery
runQuery()
{
getCount=$(PGPASSWORD=Abc123Pa55word psql -h myapp-"${cluster}".foobar.com -U foo -d dev -p 5439 -t -c "select count(*) from pg_database;")
case "${cluster}" in
east-1-1)
echo "${cluster}:${getCount} * ";;
east-1-3)
echo "${cluster}:${getCount} * ";;
east-1-7)
echo "${cluster}:${getCount} * ";;
west-1-10)
echo "${cluster}:${getCount} * ";;
south-1-2)
echo "${cluster}:${getCount} * ";;
esac
echo "${cluster}:${getCount}"
}
#Execute
runQuery
done < "$MAP"
Current Output:
east-1-1: 50 *
east-1-1: 50
east-1-2: 8
east-1-3: 58 *
east-1-3: 58
east-1-4: 5
east-1-5: 5
east-1-6: 4
east-1-7: 30 *
east-1-7: 30
west-1-1: 4
west-1-10: 50 *
west-1-10: 50
south-1-2: 30 *
south-1-2: 30
Expected Output:
east-1-1: 50 *
east-1-2: 8
east-1-3: 58 *
east-1-4: 5
east-1-5: 5
east-1-6: 4
east-1-7: 30 *
west-1-1: 4
west-1-10: 50 *
south-1-2: 30 *
Upvotes: 0
Views: 54
Reputation: 189327
If their action is the same, you can simplify by enumerating all the corresponding cases in the same branch.
case $cluster in
east-1-[137] | west-1-10 | south-1-2 )
echo "$cluster:$getCount * ";;
*)
echo "$cluster:$getCount";;
esac
(The default handling is copy/pasted from the currently accepted answer.)
Additionally, you might want to factor out the code which is identical in both cases.
suffix=''
case $cluster in
east-1-[137] | west-1-10 | south-1-2 )
suffix=' * ';;
esac
echo "$cluster:$getCount$suffix"
Upvotes: 2
Reputation: 3576
I think you just need to put your last echo statement in the default of the switch, rather than after the switch. That way it will get executed when the rest of your switch-statement doesn't, instead of executing every time.
So:
case "${cluster}" in
east-1-1)
echo "${cluster}:${getCount} * ";;
east-1-3)
echo "${cluster}:${getCount} * ";;
east-1-7)
echo "${cluster}:${getCount} * ";;
west-1-10)
echo "${cluster}:${getCount} * ";;
south-1-2)
echo "${cluster}:${getCount} * ";;
*)
echo "${cluster}:${getCount}";;
esac
Upvotes: 1