Shell_Logical Statement

1. Conditional statement
2. Flow control

Conditional judgment

1. Basic grammar

[condition] (note that there must be a space before and after the condition)
Note: If the condition is not empty, it is true, [atguigu] returns true, and [] returns false.

2. Commonly used judgment conditions

(1) Comparison between two integers
= string comparison
-lt less than (less than) -le less than or equal to (less equal)
-eq equal to (equal) -gt greater than (greater than)
-ge greater than or equal to (greater equal)- ne is not equal (Not equal)
(2) Judging according to file permissions
-r has read permission (read) -w has write permission (write)
-x has execute permission (execute)
(3) judges according to file type
-f file exists and is a regular file
-e file exists (existence) -d file exists and is a directory (directory)

3. Case practice

(1) Is 23 greater than or equal to 22

[atguigu@hadoop101 datas]$ [ 23 -ge 22 ]
[atguigu@hadoop101 datas]$ echo $?
0

(2) Does helloworld.sh have write permission

[atguigu@hadoop101 datas]$ [ -w helloworld.sh ]
[atguigu@hadoop101 datas]$ echo $?
0

(3) Does the file in the /home/atguigu/cls.txt directory exist?

[atguigu@hadoop101 datas]$ [ -e /home/atguigu/cls.txt ]
[atguigu@hadoop101 datas]$ echo $?
1

(4) Multi-condition judgment (&& means that the next command is executed only when the previous command is executed successfully, || means that the next command is executed only after the previous command has failed to execute)

[atguigu@hadoop101 ~]$ [ condition ] && echo OK || echo notok
OK
[atguigu@hadoop101 datas]$ [ condition ] && [ ] || echo notok
notok

Process control (emphasis)

1 if judgment
1. Basic grammar
if [ 条件判断式 ];then 
  程序 
fi 
或者 
if [ 条件判断式 ] 
  then 
    程序 
fi

Note:
(1) [Conditional judgment], there must be a space between the brackets and the conditional judgment
(2) There must be a space after the if

2. Case practice

(1) Input a number, if it is 1, output banzhang zhen shuai, if it is 2, output cls zhen mei, if it is other, output nothing.

[atguigu@hadoop101 datas]$ touch if.sh
[atguigu@hadoop101 datas]$ vim if.sh

#!/bin/bash

if [ $1 -eq "1" ]
then
        echo "banzhang zhen shuai"
elif [ $1 -eq "2" ]
then
        echo "cls zhen mei"
fi

[atguigu@hadoop101 datas]$ chmod 777 if.sh 
[atguigu@hadoop101 datas]$ ./if.sh 1
banzhang zhen shuai
2 case statement
1. Basic grammar
case $变量名 in 
  "值1") 
    如果变量的值等于值1,则执行程序1 
    ;; 
  "值2") 
    如果变量的值等于值2,则执行程序2 
    ;; 
  …省略其他分支… 
  *) 
    如果变量的值都不是以上的值,则执行此程序 
    ;; 
esac

Note:
1) The end of the case line must be the word "in", and each pattern match must end with a right parenthesis ")".
2) The double semicolon ";;" indicates the end of the command sequence, which is equivalent to break in java.
3) The last "*)" represents the default mode, which is equivalent to default in java.

2. Case practice

(1) Input a number, if it is 1, output banzhang, if it is 2, output cls, if it is other, output renyao.

[atguigu@hadoop101 datas]$ touch case.sh
[atguigu@hadoop101 datas]$ vim case.sh

!/bin/bash

case $1 in
"1")
        echo "banzhang"
;;

"2")
        echo "cls"
;;
*)
        echo "renyao"
;;
esac

[atguigu@hadoop101 datas]$ chmod 777 case.sh
[atguigu@hadoop101 datas]$ ./case.sh 1
1
3 for loop
1. Basic Grammar 1
	for (( 初始值;循环控制条件;变量变化 )) 
  do 
    程序 
  done
2. Case practice

(1) Increase from 1 to 100

[atguigu@hadoop101 datas]$ touch for1.sh
[atguigu@hadoop101 datas]$ vim for1.sh

#!/bin/bash

s=0
for((i=0;i<=100;i++))
do
        s=$[$s+$i]
done
echo $s

[atguigu@hadoop101 datas]$ chmod 777 for1.sh 
[atguigu@hadoop101 datas]$ ./for1.sh 
“5050”
3. Basic Grammar 2
for 变量 in 值1 值2 值3… 
  do 
    程序 
  done
4. Case practice

(1) Print all input parameters

[atguigu@hadoop101 datas]$ touch for2.sh
[atguigu@hadoop101 datas]$ vim for2.sh

#!/bin/bash
#打印数字

for i in $*
    do
      echo "ban zhang love $i "
    done

[atguigu@hadoop101 datas]$ chmod 777 for2.sh 
[atguigu@hadoop101 datas]$ bash for2.sh cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd

(2) Compare ∗ and * andand @ difference
(a)∗ and * andand @ both indicate all the parameters passed to the function or script. When they are not enclosed by double quotes "", they are all with $12… 2…2 All parameters are output in the form of n.

[atguigu@hadoop101 datas]$ touch for.sh
[atguigu@hadoop101 datas]$ vim for.sh

#!/bin/bash 

for i in $*
do
      echo "ban zhang love $i "
done

for j in $@
do      
        echo "ban zhang love $j"
done

[atguigu@hadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls 
ban zhang love xz 
ban zhang love bd 
ban zhang love cls
ban zhang love xz
ban zhang love bd

(B) When they are enclosed by double quotation marks "", "$*" will take all the parameters as a whole, with "$1 2… 2…2All parameters are output in the form of n”; “$@” will separate each parameter into “$1” “2”… ”2”…”2All parameters are output in the form of " n".

[atguigu@hadoop101 datas]$ vim for.sh

#!/bin/bash 

for i in "$*" 
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次 
        do 
                echo "ban zhang love $i"
        done 

for j in "$@" 
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次 
        do 
                echo "ban zhang love $j" 
done

[atguigu@hadoop101 datas]$ chmod 777 for.sh
[atguigu@hadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd
4 while loop
1. Basic grammar
while [ 条件判断式 ] 
  do 
    程序
  done
2. Case practice

(1) Increase from 1 to 100

[atguigu@hadoop101 datas]$ touch while.sh
[atguigu@hadoop101 datas]$ vim while.sh

#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
        s=$[$s+$i]
        i=$[$i+1]
done

echo $s

[atguigu@hadoop101 datas]$ chmod 777 while.sh 
[atguigu@hadoop101 datas]$ ./while.sh 
5050

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/114498595