简体   繁体   English

bash脚本编写-如何编写有效的case语句以处理多个条件

[英]bash scripting - how to write an effective case statement to handle multiple conditions

I have a script that reads a main list, call it List A using a while loop. 我有一个读取主列表的脚本,使用while循环将其称为List A

List A: 清单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和每个在我的项目的case我想国旗降与*

  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? 我正在函数中执行此操作并使用case语句,但是我认为我做错了这一点,想知道是否有更好的方法? Should I be using an if-then-else ? 我应该使用if-then-else吗? Can I do this with the case ? 我可以在这种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 *

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"

I think you just need to put your last echo statement in the default of the switch, rather than after the switch. 我认为您只需要将最后一个echo语句放在开关的默认位置,而不是放在开关之后。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM