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

A case study of conditional branching structure in shell programming

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

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of a case study of the branch structure of shell programming conditions. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

First, let's look at the basic structure of the shell script, which is as follows:

#! / bin/bash Code

Next, let's write the simplest shell script.

#! / bin/bashecho 'hello world'

When the above program is executed, hello world characters will be printed on the screen.

Next, let's take a look at the conditional branch structure. The statements of the shell script about the conditional branch are if and case.

If

Like other programming languages, the if statements and conditional branches of shell programs are also divided into single branch, double branch and multi-branch.

# single branch if condition; then. Fi# bifurcation if condition; then... Else... Fi# multi-branch if condition; then... Elif condition; then... Else... Fi

First of all, let's use a simple example to practice, write a shell script, the script function is that when the user enters a score, the program through the score to output different comments, failing, good, excellent, and so on.

First, we need to prompt the user to enter a number within three digits. When the user input is not in the correct format, you need to tell the user to re-enter the score, and then exit the program. The code is as follows:

Read-p "Please enter a score, with a score range of 0100:" scoreif [- z `echo $score | egrep'^ [0-9] + $'`]; then echo "entered an incorrect grade format" fi

In the above code, we use the single branch structure of if. Next, we need to use multiple branches to print out different comments according to the results.

If ((score > = 90)); then echo 'excellent' elif ((score > = 80)); then echo 'good' elif ((score > = 70)); then echo 'average' elif ((score > = 60)); then echo 'pass' else echo 'fail' fi

The above code is very simple. Let's post the complete code below. The complete code is as follows:

#! / bin/bashread-p "Please enter a score with a range of 0100:" scoreif [- z `echo $score | egrep'^ [0-9] + $'`]; then echo "entered incorrectly" fiif ((score > = 90)); then echo 'excellent' elif ((score > = 80)); then echo 'good' elif ((score > = 70)); then echo 'General' elif ((score > = 60)) Then echo 'pass' else echo 'fail' fi

Case

Next, let's take a look at another conditional branch statement, case, whose basic structure is as follows:

Case $variable in "content 1") Code Block 1;; "content 2") Code Block 2;;... *) Code block n;; esac

The meaning of the above is that when the "variable value" is equal to "content 1", code block 1 is executed, when it is equal to "content 2", code block 2 is executed, and if the previous one is not satisfied, code block n is executed.

Next, let's take a simple example to see how case is used.

#! / bin/bashcase $1 in "start") echo "this code is start";; "stop") echo "this code is stop";; "restart") echo "this code is restart";; *) echo "Usage ${0} {start | stop | restart}";; esac

The meaning of the above code is that when the user input parameter is start, the program prints this code is start, when the input parameter is stop, output this code is stop, when the input parameter is restart, output this code is restart, otherwise enter "Usage script file name {start | stop | restart}".

Thank you for reading! This is the end of the case study on the branch structure of shell programming conditions. I hope the above content can be helpful to you so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

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