Shell control statements (3)

 There are some differences between shell scripts and the C language control statements we use, so we need to re-recognize and learn them. Therefore, next, I will introduce commonly used flow control statements. Such as: if for, while, case, until, etc.

1. If conditional judgment statement

The if condition judgment statement is divided into three categories, one is single-branch if , one is double-branch if, and the last is multi-branch if

1.1, single branch if

if [ 条件1 ];then          // 中括号里的两边必须空格, 当then与中括号同一行,必须用;分号隔开
	
   执行第一段程序
fi


Another way of writing (then and brackets are not on the same line)

if [ 条件1 ]
then

  执行第一段程序
fi

Simple scoring function example: when the score of a is greater than 60, the output has passed 

Note: if also needs to be spaced with square brackets such as if 【 】

output result

 

1.2, double branch if

if [ 条件1 ]
	then
		条件成立时,执行这段程序
	else
		条件不成立时,执行这段程序
fi

A simple scoring function example: when the score of a is greater than 60, the output is passed, otherwise it is not passed

Note: In the shell1 script, it is not recommended to use > >= < <= and you need to use shell-specific -gt -eq -lt -le, etc. 

Otherwise, it is easy to run out of errors.

Output result:

 1.3, multi-branch if

if [ 条件1 ]
then
		当条件判断式1成立时,执行程序1
elif [ 条件2 ]
then
		当条件判断式2成立时,执行程序2
。。。。。。。。。
else
	当所有条件都不成立时,执行此程序
fi

Example: When a's grade [60,70)--pass, [70,80)--B [80,90)--A [90,99)--A+ is lower than 60-fail

 

Note: && is not available, because I used && at the beginning, and I reported an error, and I succeeded with -a instead 

output result

 Two, case control statement

case  $变量名称  in

 “第一个变量内容”) 
        程序段一              #不用加;分号
 ;;                           #  ;; 相当于break;
 “第二个变量内容”) 
        程序段二
 ;; 

 *) 
     其它程序段
 ;; 
 esac 

 Compare the switch control statement in c language

 Example: When a's grade [60,70)--pass, [70,80)--C [80,90)--B [90,99)--A is lower than 60-fail

 output result

 Three, for loop statement

       There are two forms of the shell's for loop

3.1. The first form of for

      Less used in shell scripts than the second, which is more

for ((初始值;循环控制条件;变量变化))    #两个括号
do
		程序              #不用加分号;结尾
done

The difference from the for loop statement in C language

 Simple example: addition operation from 1 to 100

output result

 

 3.2. The second for form

for var in con1 con2 con3 ...
do
        程序段
done

In the first cycle, the content of $var is con1
In the second cycle, the content of $var is con2
In the third cycle, the content of $var is con3
......

Example: Find out-of-order addition operation: 1 + 3 + 5 + 9 + 15 + 455 = 488

Analysis: first loop, num=1, second loop num=3 third loop num=5 until num=455 stop 

output result

Example 2: Use a shell script to test whether the file in the current location, that is, the file under ls is a directory or an ordinary file

Analysis: `ls`, this function, obtains the file information of the current location, and then starts from the first file to the last file in order

Pass it to name in turn to determine the attributes of the corresponding file

output result

 Four, while control statement

while [ condition ]          #有三处需要   空格
do 
      程序段
done

 example:

 When num=5 is entered, an infinite loop is executed to print hello world

Five, unitl control statement

until [ condition ]
do
      程序段
done

example:

 As a result, enter num=5

 Analysis: When the conditions are met, the content in do ---done will not be executed, which is the opposite of while

Six, break and continue

6.1、break

              The break command allows breaking out of loops.
              break usually exits a loop or case statement after doing some processing

Example: When i = 3, exit the for loop


output result

 

6.2、continue

              The continue command is similar to the break command
              with only one important difference, it does not jump out of the loop, it just skips this loop step and then runs the next loop

Example: When i=3, skip this cycle and proceed to the next cycle

 output result

 

Guess you like

Origin blog.csdn.net/weixin_47783699/article/details/129179063