The following article describes the basic syntax and includes a simple example of the BASHCASEstatement usage. TheCASEstatement is the simplest form of theIF-THEN-ELSEstatement in BASH. You may use theCASEstatement if you need theIF-THEN-ELSEstatement with manyELIFelements. With the BASHCASEstat...
case expression in pattern-1) statement ;; pattern-2) statement ;; . . . pattern-N) statement ;; *) statement ;; esac 一个case语句必须与开始case的关键字和结束与esac关键字。表达式被评估并与每个子句中的模式进行比较, 直到找到匹配项。匹配子句中的一...
当您在shell中有多个不同的选择时,Bash case语句可用于简化复杂的条件。使用case语句而不是嵌套的if语句将让您使bash脚本更易读,更易于维护。 Bash case语句与Javascript或C switch语句具有类似的概念。主要区别在于,与C switch语句不同,Bash case语句一旦找到一个并执行与该模式关联的语句,就不会继续搜索模式匹配。
Bash case statements are powerful yet easy to write. When you revisit an old Linux script you'll be glad you used a case statement instead of a long if-then-else statement. The case Statement Most programming languages have their version of a switch or case statement. These direct the flow...
字符串匹配(类似于正则表达式)可以通过 case 结构变得更加强大,我们希望使用它来让我们自己解析更复杂的数据。 下面的命令将帮助您完成 Bash 中 case 的使用。 caseEXPRESSIONinMatch_1) STATEMENTS# run this statement if in matches match_1;; Match_2) ...
case expression in pattern-1) statement ;; pattern-2) statement ;; . . . pattern-N) statement ;; *) statement ;; esac 一个case语句必须与开始case的关键字和结束与esac关键字。 表达式被评估并与每个子句中的模式进行比较, 直到找到匹配项。
This tutorial will cover the basics of the Bash case statement and show you how to use it in your shell scripts. case Statement Syntax The syntax of the Bash case statement consists of the “case” keyword followed by the value to be matched, the “in” keyword, and one or more ...
/bin/bash #case statement echo"Enter a number from 1 to 10: " readnum case$numin “1”|“3”|“5”|“7”|“9”) echo“Your input is an odd number” ;; “2”|“4”|“6”|“8”|“10”) echo“Your input is an even number”...
Bash shell case statement is similar to switch statement in C. It can be used to test simple values like integers and characters. Case statement is not a loop, it doesn’t execute a block of code for n number of times. Instead, bash shell checks the cond
Bash case语句采用以下形式: caseEXPRESSIONinPATTERN_1)STATEMENTS;;PATTERN_2)STATEMENTS;;PATTERN_N)STATEMENTS;;*)STATEMENTS;;esac 每个case语句都以case关键字后跟case表达式和关键字开头in。该语句以esac关键字结尾。 您可以使用由|运算符分隔的多个模式。)终止模式列表。