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 theCASEstate
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语句一旦找到一个并执行与该模式关联的语句,就不会继续搜索模式匹配。
In a Bashcasestatement, the variable holds a value that is used to compare against defined patterns. When thecasestatement is executed, the script compares the input$variableagainst each pattern in the defined order until it finds a match. Once a match is found, the corresponding command associ...
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 patte...
The case statement uses the Extension variable as the expression it tries to match to a clause. #!/bin/bash forFilein$(ls) do # extract the file extension Extension=${File##*.} case"$Extension"in sh) echo" Shell script:$File" ...
字符串匹配(类似于正则表达式)可以通过 case 结构变得更加强大,我们希望使用它来让我们自己解析更复杂的数据。 下面的命令将帮助您完成 Bash 中 case 的使用。 caseEXPRESSIONinMatch_1) STATEMENTS# run this statement if in matches match_1;; Match_2) ...
主要区别在于,与C switch语句不同,Bash case语句一旦找到一个并执行与该模式关联的语句,就不会继续搜索模式匹配。 在本教程中,我们将介绍Bash案例语句的基础知识,并向您展示如何在shell脚本中使用它们。 case语法 Bash case语句采用以下形式: case EXPRESSION in PATTERN_1) STATEMENTS ;; PATTERN_2) STATEMENTS ;...
used, no subsequent matches are attempted after the first pattern match.Using ;& in place of ;; causes execution to continue with the list associated with the next set of patterns.Using ;;& in place of ;; causes the shell to test the next pattern list in the statement, ...