1、 Introduction to special process control statements

Shell Programs or other programs , It's all sequential , That is, execute the second line after the first line is executed , And so on , Execute sequentially .

Process control statement , Such as :

  • if Conditional statements , You only have the right conditions , To be able to execute , Otherwise, the program will not be executed , This will skip some execution commands .
  • for A loop is also a process control statement , Is to repeat the same code a specified number of times .

In addition to the above conditional judgment statements and loop statements , There are also special process control statements .

such as :exit sentence 、break sentence 、continue sentence .

2、exit sentence

The system has a exit command , Used to exit the login status of the current user .

But in the Shell Script ,exit Statement is used to exit the current script . in other words , stay Shell Script , As long as I meet exit sentence , Subsequent procedures are no longer executed , And just exit the script .

exit The grammar is as follows :

exit [ Return value ]
  • If exit The return value is defined after the statement , The return value after the execution of this script is the return value defined by us . By querying $? This variable , To see the return value .
  • If exit No return value is defined after the statement , The return value after the script execution is , perform exit The statement before , The return value of the last command executed .

Write a exit Example :

demand : Determine whether the input is a pure number .

#!/bin/bash
# demonstration exit The role of # Receive user input , And assign the input to the variable num
read -p "Please input a number:" -t 30 num # If the variable num The value of is a number , Then put num Replace the value of with empty , Otherwise don't replace
# Assign the replaced value to the variable y
y=$(echo $num | sed 's/[0-9]//g' ) # explain :
# It is through sed command , Put variables num Every character in the value , As long as it is 0-9 The content of , Replace with empty .
# Finally, if y The last value of is null , prove num The contents of variables are all numbers , Because they were all replaced .
# If y The last value of is not empty , prove num The contents of the variable are non numeric , namely num Impure number . # Judgment variable y If the value of is not empty , Output error message ,
# Exit script , The return value of exit is 18
if [ -n "$y" ]
then
echo "Error!Please input a number!"
exit 18
# explain :
# If the input is not a number , The above two script commands will execute ,
# exit Once the statement is executed, the script will terminate .
else
# If you do not exit the script , Then print the variable num Number in
echo "The number is:$num"

Execute the script

#  to Shell Scripts give execution permission 
[[email protected] sh]# chmod 755 exit.sh
# Execute the script
[[email protected] sh]# ./exit.sh
# Input abc
please input num: abc
# Please enter a number when the script returns
please input number, error!!!! # see $? Variable , return 18, As set in our script .
[[email protected] sh]# echo $?
18 # Execute the script again , Input 123
[[email protected] sh]# ./exit.sh
please input num:123
# The script returns the input number
# Prove that the script meets the requirements .
123

3、break sentence

Special process control statements break The function of sentences , When the program is executed to break When the sentence is , Will end the whole cycle ( That is to jump out of this cycle , Continue with the following commands ).

and continue Statement is also a statement that ends a loop , however continue Statement skips the current loop , Continue with next cycle .

Look at the diagram to explain break sentence :

for instance :

First, write a program without break Script for statement break1.sh.

[[email protected] sh]# vim sh/break1.sh

#!/bin/bash
# Output 10 Secondary variable i Value
# Cycle ten times
for((i=1;i<=10;i=i+1))
do
# Output variables i Value
echo $i
done

Execute the script to view the results :

[[email protected] sh]# chmod 755 break1.sh
[[email protected] sh]#./break1.sh
1
2
3
4
5
6
7
8
9
10

Write another plus break Script for statement break2.sh.

[[email protected] ~]# vim sh/break2.sh

#!/bin/bash
# Output 10 Secondary variable i Value
# Cycle ten times
for((i=1;i<=10;i=i+1))
do
# If the variable i The value is equal to the 4
if[ "$i" -eq 4 ]
then
# Exit the whole cycle
break
fi
# Output variables i Value
echo $i
done

Execute the script to view the results :

[[email protected] sh]# chmod 755 break2.sh
[[email protected] sh]#./break2.sh
1
2
3

Comparison of the above two scripts , It can be seen that when the execution is finished break After the statement , Directly jump out of the whole for loop .

4、continue sentence

continue Statements are also statements that end process control . If in a loop ,continue Statement will only end the current single loop ,

Draw a diagram to illustrate continue sentence :

for instance :

Write a plus directly continue Script for statement continue1.sh.

Come with the one above break Statement .

[[email protected] ~]# vim sh/continue1.sh

#!/bin/bash
# Output 10 Secondary variable i Value
# Cycle ten times
for((i=1;i<=10;i=i+1))
do
# If the variable i The value is equal to the 4
if[ "$i" -eq 4 ]
then
# Exit the whole cycle
continue
fi
# Output variables i Value
echo $i
done

Execute the script to view the results :

[[email protected] sh]# chmod 755 continue1.sh
[[email protected] sh]#./continue1.sh
1
2
3
5
6
7
8
9
10

As can be seen from the above results ,continue Statement is to skip the fourth loop , Then proceed to step 5 Secondary cycle .

This is it. continue Statement and break The difference between sentences .

『 Forget to learn again 』Shell Process control — 39、 More articles on special process control statements

  1. 『 Forget to learn again 』Shell Basics — 1、Shell Introduction to

    Catalog 1.Shell The origin of 2.Shell Two ways of executing instructions 3. What is? Shell Script 4.Shell Is a scripting language 1.Shell The origin of We are familiar with Windows Graphical interface of the system , For graphical interfaces ...

  2. 『 Forget to learn again 』Shell Basics — 10、Bash Special symbols in ( Two )

    Tips : This article is followed by an article , Mainly about () Parentheses and {} The difference and use of braces . 8.() parentheses (): Used when a series of commands are executed ,() The command in will be in the sub Shell Run in .( Together with the braces below ) 9.{} Curly braces {}: ...

  3. 『 Forget to learn again 』Shell Basics — 3、echo Introduction and use of command

    Catalog 1.echo Role of command 2.echo Basic usage of commands 3.echo Ordered -e Option usage 4.echo Some special uses of commands (1) The output character has a font color (2) The output character has a background color Talking about Shell Before the script , ...

  4. 『 Forget to learn again 』Shell Basics — 4、Bash Basic function (history command )

    Catalog 1.history History commands 2. Set the number of command history records 3. Clear history command 4. Call of history command 5. Completion of commands and files stay Linux Default... In the system Shell Namely Bourne-AgainShell( abbreviation ...

  5. 『 Forget to learn again 』Shell Basics — 9、Bash Special symbols in ( One )

    Catalog 1. Double single quotation mark 2. Double quotes 3.$ Symbol 4. The quotation marks 5.$() Symbol 6.# Symbol 7.\ Symbol 1. Double single quotation mark '': Single quotation marks . All special symbols in single quotation marks , Such as $ and "`"( The quotation marks ) Nothing special ...

  6. 『 Forget to learn again 』Shell Basics — 11、 Rules and classification of variable definition

    Catalog 1. Rules for defining variables 2. Classification of variables 1. Rules for defining variables When you define variables , There are some rules to follow Variable names can be alphabetized . Numbers and underscores , But it can't start with a number . If the variable name is 2name It's wrong . stay Bash ...

  7. 『 Forget to learn again 』Shell Basics — 2、Shell Function and classification of

    Catalog 1.Shell The role of 2.Shell The classification of 1.Shell The role of Shell In addition to being able to interpret user input commands , Pass it to the kernel , just so so : Call other programs , Passing data or parameters to other programs , And get the processing result of the program . stay ...

  8. 『 Forget to learn again 』Shell Basics — 5、Bash Basic function ( Command aliases and common shortcuts )

    Catalog 1. Alias the command (1) Format the alias command (2) The command alias takes effect permanently (3) Alias priority 2.Bash Common shortcut key 1. Alias the command Linux The command alias of the system, as we have said before , Go over here . ...

  9. 『 Forget to learn again 』Shell Basics — 6、Bash Basic function ( I / O redirection )

    Catalog 1.Bash Standard input and output of 2. Output redirection (1) Standard output redirection (2) Standard error output redirection (3) Correct output and error output are saved at the same time 3. Input redirection 1.Bash Standard input and output of We've been talking about , stay Li ...

  10. 『 Forget to learn again 』Shell Basics — 8、 Introduction to pipeline symbols

    We've already written about pipe symbols before , Today, let's briefly summarize the usage . 1. Line extraction command grep grep Role of command , Is in the specified file , Search for qualified strings . Command format : [[email protected] ~ ] ...

Random recommendation

  1. Live YUV420 and OpenCV Mat Cross conversion

    1. YUV420 -> Mat Can be used to convert the received YUV Video source to OpenCV Identifiable data Mat myuv( Frame_Height + Frame_Height / 2, Frame_W ...

  2. thinkphp newly added

    $m = M('content'); // And   $m = new Model('content') The effect is the same $date = array( 'username' => I('username', ...

  3. js date

    let date = new Date(); let year = date.getFullYear(); let money = money = date.getMonth() + 1; let d ...

  4. java.lang.OutOfMemoryError: PermGen space And Solutions ( Reprint )

    java.lang.OutOfMemoryError: PermGen space And Solutions classification : java2007-09-11 12:34 162242 Human reading   Comment on (51)  Collection   report gene ...

  5. mysqldump How to export some data : Join in --where Parameters

    mysqldump How to export some data : Join in --where Parameters mysqldump -u user name -p password Database name Table name --where=" filter " > Export file path my ...

  6. SSH web.xml File configuration

    Start a WEB When the project is , WEB The container will read its configuration file web.xml web.xml Load priority configured in :context-param -> listener -> filter -> ...

  7. Forwarding a Range of Ports in VirtualBox

    STAN SCHWERTLY MAY 9, 2012 ARTICLES 3 COMMENTS Doesn't allow forwarding a range of ports through the ...

  8. axel Source code learning (1)&mdash;&mdash; Important process details

    The process of the previous article is too simple , Basically did not touch axel At the heart of , Therefore, this article will put axel Several important main operation processes in the are shown separately , Or in accordance with main Function to expand , Skip processes like error handling and just focus on the heaviest ...

  9. CSS3 Secret : Chapter viii.

    Add pictures to the web page 1. Commonly used to deal with pictures CSS attribute : (1)     border( Frame ): Add a border to the picture . (2)     padding( fill ): Fill the space between the frame and the picture . (3)     float ...

  10. Super Jumping! Jumping! Jumping! Basics DP

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...