当前位置:网站首页>Fundamentals of shell programming (Part 8: branch statements -case in)

Fundamentals of shell programming (Part 8: branch statements -case in)

2022-07-05 19:18:00 Just call me Wang Yuanwai

Preface

    When your if else When the sentence is particularly long , It is recommended to use case in

Example

#!/bin/bash

echo "please input a number"

read number

case ${number} in
    1)
        echo it is 1;;
    2)
        echo it is 2;;
    *)
        echo other number: ${number};;
esac

echo program is finish

Grammar standards

case expression in
  pattern )
    commands ;;
  pattern )
    commands ;;
   * ) 
    commands
esac

1、case And in Between expression Is an expression , Used to match each condition , If the conditions match ,) And ;; The command line between will be interpreted and executed

2、in With the first bracket ) The condition between is the condition of the first comparison

3、 Statement executed in condition , You need to end with a double semicolon ;;

4、) The previous is conditional expression ( Regular expressions )

5、* Indicates any condition , As long as there is no matching condition in front , It's defined in * The subsequent command line will execute

6、* ) The command executed in that line , Unwanted ;;

7、 Finally esac(case Write in reverse ), Express case in End of all statements !

summary

1、 A meet shell Programming standard statements

2、 When your if else …… For a very long time , Please use case in, This code is very readable !

原网站

版权声明
本文为[Just call me Wang Yuanwai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051850410781.html