0

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 *

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is a great answer as well. Its much more cleaner. Thanks for your feedback!
1

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

1 Comment

Well, that was it! Thank you for pointing that out! It works well now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.