Bash script for QA Testers | Case Statement

Published: 12 May 2023
on channel: SDET Adda For QA Automation
431
13

The case statement in Bash scripting provides a way to perform conditional branching based on pattern matching. It allows you to compare a variable or expression against multiple patterns and execute corresponding code blocks.

Here's the basic syntax of a case statement:

case expression in
pattern1)
Code to execute when expression matches pattern1
;;
pattern2)
Code to execute when expression matches pattern2
;;
pattern3|pattern4)
Code to execute when expression matches pattern3 or pattern4
;;
*)
Code to execute for all other cases (optional)
;;
esac

Let's break down the components:

case: The keyword that starts the case statement.
expression: The variable or expression being matched against patterns.
pattern1, pattern2, etc.: Patterns to match against the expression.
) and ;;: The closing brackets for each pattern block. ;; signifies the end of a code block.
*): The optional default case, used to handle any unmatched patterns.
Here's an example that demonstrates the usage of case:
fruit="apple"

case $fruit in
"apple")
echo "It's an apple."
;;
"banana" | "orange")
echo "It's a banana or orange."
;;
*)
echo "It's something else."
;;
esac
You can have multiple patterns in a single code block by separating them with the | symbol, as shown in the "banana" | "orange" pattern.

Note that pattern matching in case statements is based on shell glob patterns, which are similar to regular expressions but with a simpler syntax. You can use wildcards such as * (matches any characters) and ? (matches a single character) in the patterns.


Playlists-

Playwright with Java script [2023 latest] - End to end - By SDET Adda for QA Automation:    • Playwright with Java script [2024 lat...  

TestNG + Selenium 4 Tutorial -2022/2023 🔥🔥🔥:    • TestNG + Selenium 4 Tutorial -2024 Up...  

Selenium webdriver issues and solutions:    • Selenium webdriver issues and solutions  

Basic Linux (UNIX) Commands with Shell Scripting Tutorial for Testers [2023]:    • Linux and Bash Scripting Tutorial [20...  

Top 100 Java programs for QA testing Interviews:    • Top 100 Java programs for programming...  


Watch video Bash script for QA Testers | Case Statement online without registration, duration hours minute second in high quality. This video was added by user SDET Adda For QA Automation 12 May 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 431 once and liked it 13 people.