Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use case statement in Shell programming in Linux

Shulou Source: shulou.com Published: 2022-05-31 22:45:28 09月30日 Update

This article mainly shows you "how to use Shell programming case sentences in Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Shell programming case statements in Linux" this article.

The case statement is suitable for applications that require multiple branches.

The format of the case branch statement is as follows:

Case $variable name in

Mode 1)

Command sequence 1

Mode 2)

Command sequence 2

*)

Sequence of commands executed by default

Esac

The structural features of the case statement are as follows:

The case line must end with the word "in" and each pattern must end with a closing parenthesis ")".

The double semicolon ";" indicates the end of the command sequence.

Square brackets can be used in matching patterns to indicate a contiguous range, such as [0-9], and vertical bar symbols "|" to indicate or.

The last "*)" indicates the default mode, and when the variable cannot be matched using the previous various patterns, the "*)" will be executed.

The sequence of commands.

Case analysis:

Write a script to determine whether a specified script is a syntax error; if there is an error, remind the user to type Q or Q ignore the error and exit any other keys to open the specified script through vim

#! / bin/bash

Read-p "please input check script- >" file

If [- f $file]; then

Sh-n $file > / dev/null 2 > & 1

If [$?-ne 0]; then

Read-p "You input $file syntax error, [Type q to exit or Type vim to edit]" answer

Case $answer in

Q | Q)

Exit 0

Vim)

Vim $file

*)

Exit 0

Esac

Fi

Else

Echo "$file not exist"

Exit 1

Fi

These are all the contents of this article entitled "how to use Shell programming case statements in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Statements patterns commands sequences programming content articles scripts errors branches variables learning help semicolons words can be passed examples case analysis situation parentheses Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Xiaomi MySQL Huawei