当前位置:网站首页>Shell_ 06_ Judgment and circulation
Shell_ 06_ Judgment and circulation
2022-07-06 16:50:00 【Coke loving w】
Shell_06_ Judgment and cycle
Judge
if
Conditional statements : Judge according to the condition and execute the corresponding command
Command format :
if [ Conditions 1 ];then
Procedures section 1
elif [ Conditions 2 ];then
Procedures section 2
else
Procedures section 3
fi
1)if and fi Always in pairs ( Represents the beginning and end of a judgment statement );
2) Judgement statements can be nested ;
3) When a judgment statement contains multiple levels of the same if or elif when , Then only the status code returned by the first condition is 0 The statement will be executed ( If the levels are different , You can run multiple )
1) Conditions established , function then Followed by the program segment ( conversely , function else Followed by the program segment )
// Judge whether it is true according to the status code returned by the condition (0 To establish , Others are failures )
2) A space must be added on the left and right sides of the condition , Otherwise, the condition cannot be recognized ;
3) You can also execute the specified command as a condition , But the running results will be displayed on the terminal
Such as : Judging statements by conditions , Judge whether the current user's home directory and the specified file exist
1) To write test5.sh Script files 
2) call test5.sh Script files 
Such as : Judging statements by conditions , Check users who have a home directory but do not exist and users who do not have a home directory
1) To write test6.sh Script files 
2) call test6.sh Script files 
case
Content judgment statement : Judge according to the variable content and execute the corresponding command
Command format :
case ${ Variable name } in
“ Variable content 1”)
Procedures section 1
;;
“ Variable content 2”)
Procedures section 2
;;
“ Variable content N”)
Procedures section N
;;
*)
;;
esac
1) The program segment of each variable content needs two semicolons “;;” Represents the end of the program paragraph ;
2)case and esac Always in pairs ( Represents the beginning and end of content judgment );
3) The variable can be : Defining variables 、 The input variable 、 Built-in variables 、 Accept variables ;
4) If the variable content contains more than one content , Use between content “|” Separate ;
Such as : Set up hello3.sh Script files are only input hello Only then respond
1) To write hello3.sh Script files 
2) call hello3.sh Script files 
Such as : Judge sentences by content , Different content shows different responses
1) To write test26.sh Script files 
2) call test26.sh Script files 
loop
Cycle category
for do done
for loop : Specify the number of cycles
Command format 1:
for Formal variable in Content 1 Content 2 Content N
do
Procedures section
done
1) Content 1~N Will bring in formal variables in turn ;
2) Content 1~N Array variables or variable forms can be used to replace ;
3) When the content contains single quotation marks or spaces , Use double quotation marks
// You can also modify IFS Variable , Modify the field separator
4) When the cycle is over , The formal variable saves the last brought in content by default ;
5) stay done Add output redirection after , You can import the cycle results into the specified file ;
Such as : adopt for Loop to realize the output of different contents of variables
1) To write test3.sh Script files ;
2) call test3.sh Script files 
Such as : establish dir_perm.sh File implementation determines the permissions of all files in the directory
1) To write dir_perm.sh Script files ;
2) call dir_perm.sh Script files 
Internal field separator (Internal Field Separator):Shell Characters used to separate each field in
1) Variable bit of internal character separator :IFS
2) The default value is : Space
3) Assignment format :IFS= character
// If there are multiple characters as separators , It can be written together during assignment
Such as : Will a newline 、 The colon 、 The format of semicolon and double quotation mark as separator is :IFS=$‘\n’:;”
Such as : adopt IFS Variable implementation for Multiple newline inputs in the loop
1) To write test7.sh Script files 
2) call test7.sh Script files 
Command format 2:
for (( Initial value ; The loop condition ; The assignment operation ))
do
Procedures section
done
1) The format is similar to C Linguistic for loop , And variables can be called ;
2) There can be multiple initial values and assignment operations , But there can only be one cycle condition ;
Such as : establish sum1_input.sh file , Calculation 1 Add to the sum of the input numbers
1) To write sum1_input.sh Script files 
2) call sum1_input.sh Script files 
Such as : stay for Define multiple initial values and assignment operations in the loop
1) To write test8.sh Script files 
2) call test8.sh Script files 
while do done
while loop : Don't exit the loop until the condition doesn't hold
Command format :
while [ Judge the condition ]
do
Procedures section
done
(1) If multiple judgment conditions are specified
1) Only judge whether to continue the cycle according to the returned status code of the last judgment condition ;
2) Each judgment condition needs to occupy a separate line ;
3) Multiple judgment conditions will be executed in turn ;
(2) stay done Add output redirection after , You can import the cycle results into the specified file ;
Such as : adopt while Cycle output value
1) To write test9.sh Script files 
2) call test9.sh Script files 
until do done
until loop : Do not exit the cycle until the condition is established
Command format :
until [ Judge the condition ]
do
Procedures section
done
(1) If multiple judgment conditions are specified
1) Only judge whether to continue the cycle according to the returned status code of the last judgment condition ;
2) Each judgment condition needs to occupy a separate line ;
3) Multiple judgment conditions will be executed in turn ;
(2) stay done Add output redirection after , You can import the cycle results into the specified file ;
Control cycle
Nested loop
Nested loop (Nested Loop): Any type of command can be called in the program segment of the loop ( Contains loops )
Such as : establish choice_what.sh Output three numbers randomly from the array , And can't repeat
1) To write choice_what.sh Script files 
2) call choice_what.sh Script files 
Such as : Through internal circulation /etc/passwd Formatted output of file
1) To write test10.sh Script files ;
2) call test10.sh Script files 
Out of the loop
Jump out of the loop into :break command 、continue command
(1)break command : Jump out of the currently executing loop , And end the cycle
1) Command format :break N
//N Specify the loop level to jump out ( If there is no , The default is 1)
Such as : adopt break command , Jump out of for loop
1) To write test11.sh Script files 
2) call test11.sh Script files 
(2)continue command : Jump out of this cycle , Start the next cycle
1) Command format :continue N
//N Specify the loop level to jump out ( If there is no , The default is 1)
Such as : adopt continue Out of the loop
1) To write test12.sh Script files 
2) call test12.sh Script files 
边栏推荐
- LeetCode 1551. Minimum operand to make all elements in the array equal
- Gridhome, a static site generator that novices must know
- LeetCode 1562. Find the latest group of size M
- ~72 horizontal and vertical alignment of text
- Continue and break jump out of multiple loops
- 第6章 DataNode
- 第2章 HFDS的Shell操作
- Spark independent cluster dynamic online and offline worker node
- Record the error reason
- ~71 abbreviation attribute of font
猜你喜欢

字节跳动技术新人培训全记录:校招萌新成长指南

第5章 NameNode和SecondaryNameNode

LeetCode 1020. Number of enclaves

第6章 Rebalance详解

LeetCode 1637. The widest vertical area between two points without any point

~82 style of table
![[unsolved]7-14 calculation diagram](/img/33/39802c0106f2bef4b9979e5ae12b83.jpg)
[unsolved]7-14 calculation diagram

Chapter 1 overview of MapReduce

SQL快速入门

100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
随机推荐
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
Two weeks' experience of intermediate software designer in the crash soft exam
第一章 MapReduce概述
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
Gridhome, a static site generator that novices must know
Solve the problem that intel12 generation core CPU single thread only runs on small cores
LeetCode 1640. Can I connect to form an array
SQL快速入门
Detailed explanation of FLV format
Chapter 5 yarn resource scheduler
Record the error reason: terminate called after throwing an instance
FLV格式详解
图像处理一百题(11-20)
~83 form introduction
LeetCode1556. Thousand separated number
Mp4 format details
音视频开发面试题
解决Intel12代酷睿CPU单线程只给小核运行的问题
Tencent interview algorithm question