How do I simplify a conditional with multiple or statements?

Josh

I am writing a shell script that checks to see if a parameter matches a string. There are around 20 of them and more may need to be added in the future.

Currently the way I have it written it's hard to read and would be cumbersome to update. I'm not very familiar with shell scripting so I'm not sure of the best way to simplify this and make it easier to manage.

if [ $4 =="CRITICAL" ] && [[ $2 == "foo" || $2 == "bar" || $2 == "foo" || $2 == "bar" || $2 == "foo" || $2 == "bar" || $2 == "foo" || $2 == "bar" || $2 == "foo" || $2 == "bar" || ]]
  VARIABLE=1
fi

Foo and bar would all be different strings in the above script.

cas
if [[ $4 == CRITICAL && $2 =~ ^(a|b|c|d|e|f|g)$ ]]; then
  VARIABLE=1
fi

BTW, unquoted variables and positional parameters are safe to use inside [[ ... ]], but not in [ ... ]. In other words, your [ $4 == "CRITICAL" ] should be [ "$4" == "CRITICAL" ].

Also, CRITICAL doesn't need to be quoted at all above . It's a fixed string, with no spaces or shell metacharacters. If it was a fixed string that did need quoting for any reason, it's best to use single quotes.

Single quotes are for fixed strings, double-quotes are for when you want to interpolate variables, command substitutions, etc into a string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Best way to simplify multiple conditional statements

From Dev

How can I simplify and enhance multiple if-statements usage?

From Java

How simplify repeat conditional Statements on different seclcet?

From Dev

How do I simplify the below statements with a loop in SAS?

From Dev

Simplify multiple if statements

From Dev

How do I simplify pushing multiple values into an array in Ruby?

From Dev

How do I simplify pushing multiple values into an array in Ruby?

From Dev

How do i simplify css with multiple id's

From Dev

Google Script to watch multiple sheets: how do I simplify this script?

From Dev

How can I simplify multiple "||"?

From Dev

How do I simplify this jQuery?

From Dev

How do I simplify this expression?

From Dev

How do i combine multiple select statements in separate columns?

From Dev

How do I execute multiple guard statements within a loop?

From Dev

How do i consolidate multiple SQL update statements in a macro?

From Dev

How do I sum multiple "IF" statements without "Sumifs"

From Dev

How do i use multiple statements to one case statement?

From Dev

How can I DRY this series of conditional statements?

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

How do I rewrite a series of conditional statements with Q promises in node.js?

From Dev

How do I insert multiple conditional comments using Jade?

From Dev

How do I simplify this div loop

From Dev

How do I simplify the following code?

From Dev

How do I simplify minterms of a truth table?

From Dev

How do I simplify my code?

From Dev

How do I simplify the following code?

From Dev

multiple conditional statements in square brackets

From Dev

Simplify a formula with lots of IF/AND/OR statements possibly by detecting conditional formatting

Related Related

  1. 1

    Best way to simplify multiple conditional statements

  2. 2

    How can I simplify and enhance multiple if-statements usage?

  3. 3

    How simplify repeat conditional Statements on different seclcet?

  4. 4

    How do I simplify the below statements with a loop in SAS?

  5. 5

    Simplify multiple if statements

  6. 6

    How do I simplify pushing multiple values into an array in Ruby?

  7. 7

    How do I simplify pushing multiple values into an array in Ruby?

  8. 8

    How do i simplify css with multiple id's

  9. 9

    Google Script to watch multiple sheets: how do I simplify this script?

  10. 10

    How can I simplify multiple "||"?

  11. 11

    How do I simplify this jQuery?

  12. 12

    How do I simplify this expression?

  13. 13

    How do i combine multiple select statements in separate columns?

  14. 14

    How do I execute multiple guard statements within a loop?

  15. 15

    How do i consolidate multiple SQL update statements in a macro?

  16. 16

    How do I sum multiple "IF" statements without "Sumifs"

  17. 17

    How do i use multiple statements to one case statement?

  18. 18

    How can I DRY this series of conditional statements?

  19. 19

    How can I write a conditional without any conditional statements or operators?

  20. 20

    How can I write a conditional without any conditional statements or operators?

  21. 21

    How do I rewrite a series of conditional statements with Q promises in node.js?

  22. 22

    How do I insert multiple conditional comments using Jade?

  23. 23

    How do I simplify this div loop

  24. 24

    How do I simplify the following code?

  25. 25

    How do I simplify minterms of a truth table?

  26. 26

    How do I simplify my code?

  27. 27

    How do I simplify the following code?

  28. 28

    multiple conditional statements in square brackets

  29. 29

    Simplify a formula with lots of IF/AND/OR statements possibly by detecting conditional formatting

HotTag

Archive