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...
Смотрите видео Bash script for QA Testers | Case Statement онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь SDET Adda For QA Automation 12 Май 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 431 раз и оно понравилось 13 людям.