Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use case statement in Shell programming in Linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report